diff --git a/lib/pact_broker/app.rb b/lib/pact_broker/app.rb index 8daec289b..ef901d494 100644 --- a/lib/pact_broker/app.rb +++ b/lib/pact_broker/app.rb @@ -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 @@ -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 diff --git a/lib/rack/pact_broker/convert_404_to_hal.rb b/lib/rack/pact_broker/convert_404_to_hal.rb new file mode 100644 index 000000000..098547516 --- /dev/null +++ b/lib/rack/pact_broker/convert_404_to_hal.rb @@ -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 diff --git a/spec/lib/pact_broker/app_spec.rb b/spec/lib/pact_broker/app_spec.rb index 63ff8f3b9..5b862e114 100644 --- a/spec/lib/pact_broker/app_spec.rb +++ b/spec/lib/pact_broker/app_spec.rb @@ -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