-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom provider header): Allow a --custom-provider-header to be …
…specified This header will be added to the replayed requests AND the provider state setup request. It is intended to allow valid authorization credentials to be used in the situation where authorization cannot be turned off in the provider when verifying pacts.
- Loading branch information
Showing
7 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Pact | ||
module ProviderVerifier | ||
class AddHeaderMiddlware | ||
|
||
def initialize app, headers | ||
@app = app | ||
@headers = headers | ||
end | ||
|
||
def call env | ||
@app.call(add_headers(env)) | ||
end | ||
|
||
def add_headers env | ||
new_env = env.dup | ||
@headers.each_pair do | key, value | | ||
header_name = "HTTP_#{key.upcase.gsub("-", "_")}" | ||
warn_about_header_replacement key, new_env[header_name], value | ||
new_env[header_name] = value | ||
end | ||
new_env | ||
end | ||
|
||
def warn_about_header_replacement header_name, existing_value, new_value | ||
if existing_value.nil? | ||
$stderr.puts "WARN: Adding header '#{header_name}: #{new_value}' to replayed request. This header did not exist in the pact, and hence this test cannot confirm that the request will work in real life." | ||
else | ||
$stdout.puts "INFO: Replacing header '#{header_name}: #{existing_value}' with '#{header_name}: #{new_value}'" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
describe "pact-provider-verifier with basic auth" do | ||
before(:all) do | ||
@pipe = IO.popen("USE_BASIC_AUTH=true bundle exec rackup -p 4567 spec/support/config.ru") | ||
sleep 2 | ||
end | ||
|
||
context "with --custom-provider-header specified" do | ||
|
||
subject { `bundle exec bin/pact-provider-verifier --custom-provider-header "Authorization: Basic cGFjdDpwYWN0" -a 1.0.100 --provider-base-url http://localhost:4567 --pact-urls ./test/me-they.json --provider_states_setup_url http://localhost:4567/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 "2 interactions, 0 failures" | ||
end | ||
end | ||
|
||
after(:all) do | ||
Process.kill 'KILL', @pipe.pid | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
spec/lib/pact/provider_verifier/add_header_middlware_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'pact/provider_verifier/add_header_middlware' | ||
|
||
module Pact | ||
module ProviderVerifier | ||
describe AddHeaderMiddlware do | ||
describe "call" do | ||
let(:target_app) { double(:app, call: nil) } | ||
let(:middlware) { AddHeaderMiddlware.new(target_app, headers) } | ||
let(:headers) { {'Foo-Bar' => 'foo'} } | ||
|
||
before do | ||
allow($stdout).to receive(:puts) | ||
allow($stderr).to receive(:puts) | ||
end | ||
|
||
it "keeps the existing headers" do | ||
expect(target_app).to receive(:call) do | env | | ||
expect(env['MOO']).to eq 'bar' | ||
end | ||
middlware.call('MOO' => 'bar') | ||
end | ||
|
||
it "adds the headers to the env" do | ||
expect(target_app).to receive(:call) do | env | | ||
expect(env['HTTP_FOO_BAR']).to eq 'foo' | ||
end | ||
middlware.call({'HTTP_FOO_BAR' => 'ick'}) | ||
end | ||
|
||
context "when the specified header does not already exist" do | ||
it "warns the user" do | ||
expect($stderr).to receive(:puts).with(/WARN: Adding header 'Foo-Bar: foo'/) | ||
middlware.call({}) | ||
end | ||
end | ||
|
||
context "when the specified header already exists" do | ||
it "notifies the user" do | ||
expect($stdout).to receive(:puts).with(/INFO: Replacing header 'Foo-Bar: wiffle' with 'Foo-Bar: foo'/) | ||
middlware.call({'HTTP_FOO_BAR' => 'wiffle' }) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
require_relative 'provider' | ||
|
||
if ENV['USE_BASIC_AUTH'] == 'true' | ||
use Rack::Auth::Basic, "Restricted Area" do |username, password| | ||
username == 'pact' and password == 'pact' | ||
end | ||
end | ||
|
||
run Provider |