Skip to content

Commit 89fe1c4

Browse files
hhhonzikYOU54F
authored andcommitted
feat(generators): Pass provider state results back so we can use ProviderState generator
1 parent b024db9 commit 89fe1c4

File tree

5 files changed

+157
-1
lines changed

5 files changed

+157
-1
lines changed

lib/pact/provider_verifier/set_up_provider_state.rb

Lines changed: 6 additions & 1 deletion
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
@@ -49,11 +54,11 @@ def post_to_provider_state
4954
# https://github.com/pact-foundation/pact-go/issues/42
5055
# eg. https://ci.appveyor.com/project/mefellows/pact-go/build/25#L1202
5156

57+
5258
faraday.request :retry, max: 2, interval: 0.05,
5359
interval_randomness: 0.5, backoff_factor: 2,
5460
methods:[:post],
5561
exceptions: [Faraday::ConnectionFailed]
56-
5762
faraday.response :logger if verbose
5863
faraday.adapter Faraday.default_adapter
5964
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.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
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'pact/provider_verifier/set_up_provider_state'
2+
require 'webmock/rspec'
3+
4+
module Pact
5+
module ProviderVerifier
6+
7+
describe SetUpProviderState do
8+
9+
let(:provider_states_setup_url) { 'http://foo' }
10+
let(:provider_state) { 'some state' }
11+
let(:consumer) { 'Foo' }
12+
let(:options) { { params: params } }
13+
let(:params) { { "foo" => "bar" }}
14+
subject { SetUpProviderState.call(provider_state, consumer, options) }
15+
16+
before do
17+
ENV['PROVIDER_STATES_SETUP_URL'] = provider_states_setup_url
18+
stub_request(:post, provider_states_setup_url)
19+
allow($stdout).to receive(:puts)
20+
allow($stderr).to receive(:puts)
21+
end
22+
23+
it "makes a HTTP request to the configured URL with a JSON body containing the consumer and provider state names" do
24+
subject
25+
expect(WebMock).to have_requested(:post, provider_states_setup_url).
26+
with(body: {consumer: consumer, state: provider_state, states: [provider_state], params: params}, headers: {'Content-Type' => "application/json"})
27+
end
28+
29+
context "when an error is returned from the request to the setup URL" do
30+
before do
31+
stub_request(:post, provider_states_setup_url).to_return(status: 500, body: "Some error")
32+
end
33+
34+
it "raises an error" do
35+
expect { subject }.to raise_error(SetUpProviderStateError, /500.*Some error/)
36+
end
37+
end
38+
39+
context "when the provider_states_setup_url is nil" do
40+
before do
41+
ENV['PROVIDER_STATES_SETUP_URL'] = nil
42+
end
43+
44+
it "does not make a HTTP request" do
45+
subject
46+
expect(WebMock).to_not have_requested(:post, provider_states_setup_url)
47+
end
48+
49+
context "when a provider_state is present" do
50+
it "logs a warning" do
51+
expect($stderr).to receive(:puts).with("WARN: Skipping set up for provider state 'some state' for consumer 'Foo' as there is no --provider-states-setup-url specified.")
52+
subject
53+
end
54+
end
55+
56+
context "when a provider_state is not present" do
57+
let(:provider_state) { nil }
58+
59+
it "does not log a warning" do
60+
expect($stderr).to_not receive(:puts)
61+
subject
62+
end
63+
end
64+
end
65+
end
66+
end
67+
end
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
9+
end
10+
11+
get '/book/1' do
12+
json :id => 1, :name => 'Unexpected Book'
13+
end
14+
15+
get '/book/2' do
16+
json :id => 2, :name => 'Injected Book'
17+
end
18+
19+
end

0 commit comments

Comments
 (0)