File tree 3 files changed +39
-1
lines changed
3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 12
12
require 'rack/pact_broker/ui_authentication'
13
13
require 'rack/pact_broker/configurable_make_it_later'
14
14
require 'rack/pact_broker/no_auth'
15
+ require 'rack/pact_broker/convert_404_to_hal'
15
16
require 'sucker_punch'
16
17
17
18
module PactBroker
@@ -145,6 +146,7 @@ def build_api
145
146
require 'pact_broker/api'
146
147
builder = ::Rack ::Builder . new
147
148
builder . use @make_it_later_api_auth
149
+ builder . use Rack ::PactBroker ::Convert404ToHal
148
150
builder . use Rack ::PactBroker ::DatabaseTransaction , configuration . database_connection
149
151
builder . run PactBroker ::API
150
152
builder
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -276,11 +276,27 @@ class TestAuth2 < TestAuth1; end
276
276
PactBroker ::Database . truncate
277
277
end
278
278
279
- subject { put path , pact_content , { 'CONTENT_TYPE' => 'application/json' } ; last_response }
279
+ subject { put path , pact_content , { 'CONTENT_TYPE' => 'application/json' } ; last_response }
280
280
281
281
it "wraps the API with a database transaction" do
282
282
expect { subject } . to_not change { PactBroker ::Domain ::Pacticipant . count }
283
283
end
284
284
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
285
301
end
286
302
end
You can’t perform that action at this time.
0 commit comments