Skip to content

Commit

Permalink
fix(content-type): convert 404 content-type to application/hal+json #235
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Sep 19, 2018
1 parent 1c8e199 commit 83958db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/pact_broker/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'rack/pact_broker/ui_authentication'
require 'rack/pact_broker/configurable_make_it_later'
require 'rack/pact_broker/no_auth'
require 'rack/pact_broker/convert_404_to_hal'
require 'sucker_punch'

module PactBroker
Expand Down Expand Up @@ -145,6 +146,7 @@ def build_api
require 'pact_broker/api'
builder = ::Rack::Builder.new
builder.use @make_it_later_api_auth
builder.use Rack::PactBroker::Convert404ToHal
builder.use Rack::PactBroker::DatabaseTransaction, configuration.database_connection
builder.run PactBroker::API
builder
Expand Down
20 changes: 20 additions & 0 deletions lib/rack/pact_broker/convert_404_to_hal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Rack
module PactBroker
class Convert404ToHal

def initialize app
@app = app
end

def call env
response = @app.call(env)

if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html/)
[404, { 'Content-Type' => 'application/hal+json'},[]]
else
response
end
end
end
end
end
18 changes: 17 additions & 1 deletion spec/lib/pact_broker/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,27 @@ class TestAuth2 < TestAuth1; end
PactBroker::Database.truncate
end

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

it "wraps the API with a database transaction" do
expect { subject }.to_not change { PactBroker::Domain::Pacticipant.count }
end
end

describe "when resource is not found" do
subject { get("/does/not/exist", nil, { 'CONTENT_TYPE' => 'application/hal+json' }) }

it "returns a Content-Type of application/hal+json" do
expect(subject.headers['Content-Type']).to eq 'application/hal+json'
end

it "returns a JSON body" do
expect(subject.body).to eq ""
end

it "returns a 404" do
expect(subject.status).to eq 404
end
end
end
end

0 comments on commit 83958db

Please sign in to comment.