3
3
require 'rack/test'
4
4
5
5
module PactBroker ::Api
6
-
7
6
module Resources
8
-
9
7
describe LatestPact do
10
-
11
8
include Rack ::Test ::Methods
12
-
13
- let ( :app ) { PactBroker ::API }
14
-
15
9
describe "GET" do
16
-
17
10
context "Accept: text/html" do
18
11
19
- let ( :path ) { "/pacts/provider/provider_name/consumer/consumer_name/latest" }
12
+ let ( :path ) { "/pacts/provider/provider_name/consumer/consumer_name/latest/prod " }
20
13
let ( :json_content ) { 'json_content' }
21
- let ( :pact ) { double ( "pact" , json_content : json_content ) }
14
+ let ( :pact ) { double ( "pact" , json_content : json_content , consumer_version_number : '1' ) }
22
15
let ( :html ) { 'html' }
23
16
let ( :pact_id_params ) { { provider_name : "provider_name" , consumer_name : "consumer_name" } }
24
17
let ( :html_options ) { { base_url : 'http://example.org' , badge_url : "http://example.org#{ path } /badge.svg" } }
18
+ let ( :metadata ) { double ( 'metadata' ) }
19
+ let ( :accept ) { "text/html" }
25
20
26
21
before do
27
22
allow ( PactBroker ::Pacts ::Service ) . to receive ( :find_latest_pact ) . and_return ( pact )
28
23
allow ( PactBroker . configuration . html_pact_renderer ) . to receive ( :call ) . and_return ( html )
29
24
end
30
25
31
- subject { get path , { } , { 'HTTP_ACCEPT' => "text/html" } }
26
+ subject { get ( path , nil , 'HTTP_ACCEPT' => accept ) }
32
27
33
28
it "find the pact" do
34
29
expect ( PactBroker ::Pacts ::Service ) . to receive ( :find_latest_pact ) . with ( hash_including ( pact_id_params ) )
@@ -40,6 +35,7 @@ module Resources
40
35
subject
41
36
end
42
37
38
+
43
39
it "returns a HTML body" do
44
40
subject
45
41
expect ( last_response . body ) . to eq html
@@ -50,10 +46,34 @@ module Resources
50
46
expect ( last_response . headers [ 'Content-Type' ] ) . to eq 'text/html;charset=utf-8'
51
47
end
52
48
49
+ context "when Accept is application/hal+json" do
50
+ let ( :accept ) { "application/hal+json" }
51
+ let ( :decorator ) { instance_double ( PactBroker ::Api ::Decorators ::PactDecorator , to_json : pact_json ) }
52
+ let ( :pact_json ) { { some : 'json' } . to_json }
53
+
54
+ before do
55
+ allow ( PactBroker ::Api ::Decorators ::PactDecorator ) . to receive ( :new ) . and_return ( decorator )
56
+ allow ( PactBroker ::Pacts ::Metadata ) . to receive ( :build_metadata_for_latest_pact ) . and_return ( metadata )
57
+ allow_any_instance_of ( LatestPact ) . to receive ( :encode_webhook_metadata ) . and_return ( 'encoded metadata' )
58
+ end
59
+
60
+ it "builds the metadata" do
61
+ expect ( PactBroker ::Pacts ::Metadata ) . to receive ( :build_metadata_for_latest_pact ) . with ( pact , hash_including ( tag : 'prod' ) )
62
+ subject
63
+ end
64
+
65
+ it "encodes the metadata" do
66
+ expect_any_instance_of ( LatestPact ) . to receive ( :encode_webhook_metadata ) . with ( metadata )
67
+ subject
68
+ end
69
+
70
+ it "renders the pact in JSON" do
71
+ expect ( decorator ) . to receive ( :to_json ) . with ( user_options : hash_including ( metadata : 'encoded metadata' ) )
72
+ expect ( subject . body ) . to eq pact_json
73
+ end
74
+ end
53
75
end
54
76
end
55
-
56
77
end
57
78
end
58
-
59
79
end
0 commit comments