Skip to content

Commit 019c639

Browse files
authored
Merge pull request #125 from pact-foundation/feat/v3_generators
Feat/v3 generators
2 parents b5d4b9f + 98cedb5 commit 019c639

7 files changed

+174
-2
lines changed

lib/pact/provider_verifier/set_up_provider_state.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ def call
2828
log_request
2929
response = post_to_provider_state
3030
check_for_error response
31+
begin
32+
JSON.parse(response.body)
33+
rescue
34+
{}
35+
end
3136
end
3237

3338
private
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
22+
system("taskkill /im #{@pipe.pid} /f /t >nul 2>&1")
23+
else
24+
Process.kill 'KILL', @pipe.pid
25+
end
26+
end
27+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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-path.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+
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
22+
system("taskkill /im #{@pipe.pid} /f /t >nul 2>&1")
23+
else
24+
Process.kill 'KILL', @pipe.pid
25+
end
26+
end
27+
end

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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"provider": {
3+
"name": "Foo"
4+
},
5+
"consumer": {
6+
"name": "Bar"
7+
},
8+
"interactions": [
9+
{
10+
"description": "returns book detail",
11+
"request": {
12+
"method": "GET",
13+
"path": "/book/1",
14+
"generators": {
15+
"path": {
16+
"type": "ProviderState",
17+
"expression": "/book/${id}"
18+
}
19+
}
20+
},
21+
"response": {
22+
"status": 200,
23+
"body": {
24+
"name": "Injected Book"
25+
}
26+
},
27+
"providerStates": [
28+
{
29+
"name": "returns book detail"
30+
}
31+
]
32+
}
33+
],
34+
"metadata": {
35+
"pactSpecification": {
36+
"version": "3.0.0"
37+
},
38+
"pact-jvm": {
39+
"version": "4.0.5"
40+
}
41+
}
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'sinatra'
2+
require 'sinatra/base'
3+
require 'sinatra/json'
4+
require 'json'
5+
6+
class ProviderWithStateGenerator < Sinatra::Base
7+
post '/provider_state' do
8+
json :id => 2, :accessToken => 'INJECTED_TOKEN'
9+
end
10+
11+
get '/book/1' do
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'
15+
end
16+
17+
get '/book/2' do
18+
json :id => 2, :name => 'Injected Book'
19+
end
20+
21+
get '/requires_auth' do
22+
if request.env['HTTP_AUTHORIZATION'] != "Bearer INJECTED_TOKEN"
23+
status 403
24+
end
25+
end
26+
27+
end

0 commit comments

Comments
 (0)