Skip to content

Commit 78a42d7

Browse files
sltYOU54F
authored andcommitted
feat(generators): More test cases
1 parent 89fe1c4 commit 78a42d7

8 files changed

+83
-75
lines changed

lib/pact/provider_verifier/set_up_provider_state.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def post_to_provider_state
5454
# https://github.com/pact-foundation/pact-go/issues/42
5555
# eg. https://ci.appveyor.com/project/mefellows/pact-go/build/25#L1202
5656

57-
5857
faraday.request :retry, max: 2, interval: 0.05,
5958
interval_randomness: 0.5, backoff_factor: 2,
6059
methods:[:post],
6160
exceptions: [Faraday::ConnectionFailed]
61+
6262
faraday.response :logger if verbose
6363
faraday.adapter Faraday.default_adapter
6464
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'json'
2+
3+
describe "pact-provider-verifier with a provider state injected to a pact file" do
4+
before(:all) do
5+
@pipe = IO.popen("bundle exec rackup -p 5837 spec/support/provider_with_state_generator.rb")
6+
sleep 2
7+
end
8+
9+
subject { `bundle exec bin/pact-provider-verifier spec/support/pacts/pact-with-provider-state-in-headers.json -a 1 --provider-base-url http://localhost:5837/ --provider-states-setup-url http://localhost:5837/provider_state -v` }
10+
11+
it "exits with a 0 exit code" do
12+
subject
13+
expect($?).to eq 0
14+
end
15+
16+
it "the output contains a success message" do
17+
expect(subject).to include "1 interaction, 0 failures"
18+
end
19+
20+
after(:all) do
21+
Process.kill 'KILL', @pipe.pid
22+
end
23+
end

spec/integration_with_provider_state_spec.rb renamed to spec/integration_with_provider_state_in_path.spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
sleep 2
77
end
88

9-
subject { `bundle exec bin/pact-provider-verifier spec/support/pacts/pact-with-provider-state.json -a 1 --provider-base-url http://localhost:5837/ --provider-states-setup-url http://localhost:5837/provider_state -v` }
9+
subject { `bundle exec bin/pact-provider-verifier spec/support/pacts/pact-with-provider-state-in-path.json -a 1 --provider-base-url http://localhost:5837/ --provider-states-setup-url http://localhost:5837/provider_state -v` }
1010

1111
it "exits with a 0 exit code" do
1212
subject

spec/lib/pact/provider_verifier/set_up_provider_state_inject_parameters_spec.rb

Lines changed: 0 additions & 67 deletions
This file was deleted.

spec/lib/pact/provider_verifier/set_up_provider_state_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ module ProviderVerifier
1515

1616
before do
1717
ENV['PROVIDER_STATES_SETUP_URL'] = provider_states_setup_url
18-
stub_request(:post, provider_states_setup_url)
18+
stub_request(:post, provider_states_setup_url).to_return(status: 200, body: '{"id":2}')
1919
allow($stdout).to receive(:puts)
2020
allow($stderr).to receive(:puts)
2121
end
2222

2323
it "makes a HTTP request to the configured URL with a JSON body containing the consumer and provider state names" do
24-
subject
24+
expect(subject).to eq({"id" => 2})
2525
expect(WebMock).to have_requested(:post, provider_states_setup_url).
2626
with(body: {consumer: consumer, state: provider_state, states: [provider_state], params: params}, headers: {'Content-Type' => "application/json"})
2727
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"provider": {
3+
"name": "Foo"
4+
},
5+
"consumer": {
6+
"name": "Bar"
7+
},
8+
"interactions": [
9+
{
10+
"description": "requires access token",
11+
"request": {
12+
"method": "GET",
13+
"path": "/requires_auth",
14+
"headers": {
15+
"Authorization": "Bearer EXAMPLE_TOKEN"
16+
},
17+
"generators": {
18+
"header": {
19+
"$.Authorization": {
20+
"expression": "Bearer ${accessToken}",
21+
"type": "ProviderState"
22+
}
23+
}
24+
}
25+
},
26+
"response": {
27+
"status": 200
28+
},
29+
"providerStates": [
30+
{
31+
"name": "returns access token"
32+
}
33+
]
34+
}
35+
],
36+
"metadata": {
37+
"pactSpecification": {
38+
"version": "3.0.0"
39+
},
40+
"pact-jvm": {
41+
"version": "4.0.5"
42+
}
43+
}
44+
}

spec/support/pacts/pact-with-provider-state.json renamed to spec/support/pacts/pact-with-provider-state-in-path.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
"version": "4.0.5"
4040
}
4141
}
42-
}
42+
}

spec/support/provider_with_state_generator.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55

66
class ProviderWithStateGenerator < Sinatra::Base
77
post '/provider_state' do
8-
json :id => 2
8+
json :id => 2, :accessToken => 'INJECTED_TOKEN'
99
end
1010

1111
get '/book/1' do
12-
json :id => 1, :name => 'Unexpected Book'
12+
# Return 404 so that if the provider state is not injected the contract will fail
13+
status 404
14+
json :id => 1, :name => 'Book not found'
1315
end
1416

1517
get '/book/2' do
1618
json :id => 2, :name => 'Injected Book'
1719
end
18-
20+
21+
get '/requires_auth' do
22+
if request.env['HTTP_AUTHORIZATION'] != "Bearer INJECTED_TOKEN"
23+
status 403
24+
end
25+
end
26+
1927
end

0 commit comments

Comments
 (0)