Skip to content

Commit 83958db

Browse files
committed
fix(content-type): convert 404 content-type to application/hal+json pact-foundation#235
1 parent 1c8e199 commit 83958db

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/pact_broker/app.rb

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'rack/pact_broker/ui_authentication'
1313
require 'rack/pact_broker/configurable_make_it_later'
1414
require 'rack/pact_broker/no_auth'
15+
require 'rack/pact_broker/convert_404_to_hal'
1516
require 'sucker_punch'
1617

1718
module PactBroker
@@ -145,6 +146,7 @@ def build_api
145146
require 'pact_broker/api'
146147
builder = ::Rack::Builder.new
147148
builder.use @make_it_later_api_auth
149+
builder.use Rack::PactBroker::Convert404ToHal
148150
builder.use Rack::PactBroker::DatabaseTransaction, configuration.database_connection
149151
builder.run PactBroker::API
150152
builder
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Rack
2+
module PactBroker
3+
class Convert404ToHal
4+
5+
def initialize app
6+
@app = app
7+
end
8+
9+
def call env
10+
response = @app.call(env)
11+
12+
if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html/)
13+
[404, { 'Content-Type' => 'application/hal+json'},[]]
14+
else
15+
response
16+
end
17+
end
18+
end
19+
end
20+
end

spec/lib/pact_broker/app_spec.rb

+17-1
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,27 @@ class TestAuth2 < TestAuth1; end
276276
PactBroker::Database.truncate
277277
end
278278

279-
subject { put path, pact_content, {'CONTENT_TYPE' => 'application/json' }; last_response }
279+
subject { put path, pact_content, { 'CONTENT_TYPE' => 'application/json' }; last_response }
280280

281281
it "wraps the API with a database transaction" do
282282
expect { subject }.to_not change { PactBroker::Domain::Pacticipant.count }
283283
end
284284
end
285+
286+
describe "when resource is not found" do
287+
subject { get("/does/not/exist", nil, { 'CONTENT_TYPE' => 'application/hal+json' }) }
288+
289+
it "returns a Content-Type of application/hal+json" do
290+
expect(subject.headers['Content-Type']).to eq 'application/hal+json'
291+
end
292+
293+
it "returns a JSON body" do
294+
expect(subject.body).to eq ""
295+
end
296+
297+
it "returns a 404" do
298+
expect(subject.status).to eq 404
299+
end
300+
end
285301
end
286302
end

0 commit comments

Comments
 (0)