Skip to content

Commit

Permalink
Merge pull request #125 from pact-foundation/feat/v3_generators
Browse files Browse the repository at this point in the history
Feat/v3 generators
  • Loading branch information
YOU54F authored Nov 29, 2024
2 parents b5d4b9f + 98cedb5 commit 019c639
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/pact/provider_verifier/set_up_provider_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def call
log_request
response = post_to_provider_state
check_for_error response
begin
JSON.parse(response.body)
rescue
{}
end
end

private
Expand Down
27 changes: 27 additions & 0 deletions spec/integration_with_provider_state_in_headers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'json'

describe "pact-provider-verifier with a provider state injected to a pact file" do
before(:all) do
@pipe = IO.popen("bundle exec rackup -p 5837 spec/support/provider_with_state_generator.rb")
sleep 2
end

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` }

it "exits with a 0 exit code" do
subject
expect($?).to eq 0
end

it "the output contains a success message" do
expect(subject).to include "1 interaction, 0 failures"
end

after(:all) do
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system("taskkill /im #{@pipe.pid} /f /t >nul 2>&1")
else
Process.kill 'KILL', @pipe.pid
end
end
end
27 changes: 27 additions & 0 deletions spec/integration_with_provider_state_in_path.spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'json'

describe "pact-provider-verifier with a provider state injected to a pact file" do
before(:all) do
@pipe = IO.popen("bundle exec rackup -p 5837 spec/support/provider_with_state_generator.rb")
sleep 2
end

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` }

it "exits with a 0 exit code" do
subject
expect($?).to eq 0
end

it "the output contains a success message" do
expect(subject).to include "1 interaction, 0 failures"
end

after(:all) do
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system("taskkill /im #{@pipe.pid} /f /t >nul 2>&1")
else
Process.kill 'KILL', @pipe.pid
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/pact/provider_verifier/set_up_provider_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ module ProviderVerifier

before do
ENV['PROVIDER_STATES_SETUP_URL'] = provider_states_setup_url
stub_request(:post, provider_states_setup_url)
stub_request(:post, provider_states_setup_url).to_return(status: 200, body: '{"id":2}')
allow($stdout).to receive(:puts)
allow($stderr).to receive(:puts)
end

it "makes a HTTP request to the configured URL with a JSON body containing the consumer and provider state names" do
subject
expect(subject).to eq({"id" => 2})
expect(WebMock).to have_requested(:post, provider_states_setup_url).
with(body: {consumer: consumer, state: provider_state, states: [provider_state], params: params}, headers: {'Content-Type' => "application/json"})
end
Expand Down
44 changes: 44 additions & 0 deletions spec/support/pacts/pact-with-provider-state-in-headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"provider": {
"name": "Foo"
},
"consumer": {
"name": "Bar"
},
"interactions": [
{
"description": "requires access token",
"request": {
"method": "GET",
"path": "/requires_auth",
"headers": {
"Authorization": "Bearer EXAMPLE_TOKEN"
},
"generators": {
"header": {
"$.Authorization": {
"expression": "Bearer ${accessToken}",
"type": "ProviderState"
}
}
}
},
"response": {
"status": 200
},
"providerStates": [
{
"name": "returns access token"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "4.0.5"
}
}
}
42 changes: 42 additions & 0 deletions spec/support/pacts/pact-with-provider-state-in-path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"provider": {
"name": "Foo"
},
"consumer": {
"name": "Bar"
},
"interactions": [
{
"description": "returns book detail",
"request": {
"method": "GET",
"path": "/book/1",
"generators": {
"path": {
"type": "ProviderState",
"expression": "/book/${id}"
}
}
},
"response": {
"status": 200,
"body": {
"name": "Injected Book"
}
},
"providerStates": [
{
"name": "returns book detail"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "4.0.5"
}
}
}
27 changes: 27 additions & 0 deletions spec/support/provider_with_state_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'sinatra'
require 'sinatra/base'
require 'sinatra/json'
require 'json'

class ProviderWithStateGenerator < Sinatra::Base
post '/provider_state' do
json :id => 2, :accessToken => 'INJECTED_TOKEN'
end

get '/book/1' do
# Return 404 so that if the provider state is not injected the contract will fail
status 404
json :id => 1, :name => 'Book not found'
end

get '/book/2' do
json :id => 2, :name => 'Injected Book'
end

get '/requires_auth' do
if request.env['HTTP_AUTHORIZATION'] != "Bearer INJECTED_TOKEN"
status 403
end
end

end

0 comments on commit 019c639

Please sign in to comment.