Skip to content

Commit

Permalink
Added basic authentication support for Pact Broker URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed May 15, 2016
1 parent d36ae19 commit c5dc292
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 46 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cd examples
1. Create an API and a corresponding Docker image for it
1. Publish Pacts to the Pact broker (or create local ones)
1. Run the CLI tool for your OS, passing the appropriate flags:
* `--pact_urls` - a comma delimited list of pact file urls
* `--pact_urls` - a comma delimited list of local Pact file urls or Pact Broker URLs.
* `--provider_base_url` - the base url of the pact provider (i.e. your API)
1.

Expand All @@ -63,13 +63,21 @@ Execute pact provider verification against a provider which implements the follo

consumer=web&state=customer%20is%20logged%20in

The following environment variables required:
The following flags are required:

* `pact_urls` - a comma delimited list of pact file urls
* `provider_base_url` - the base url of the pact provider
* `provider_states_url` - the full url of the endpoint which returns provider states by consumer
* `provider_states_setup_url` - the full url of the endpoint which sets the active pact consumer and provider state
* `--pact-urls` - a comma delimited list of pact file urls
* `--provider-base-url` - the base url of the pact provider
* `--provider-states-url` - the full url of the endpoint which returns provider states by consumer
* `--provider-states-setup-url` - the full url of the endpoint which sets the active pact consumer and provider state

### Using the Pact Broker with Basic authentication

The following flags are required to use basic authentication with a Pact Broker:

* `--broker-user` - the Username for Pact Broker basic authentication.
* `--broker-password` - the Password for Pact Broker basic authentication.

NOTE: the `http://user:password@host` format for basic HTTP auth is not supported.

## Contributing

Expand Down
38 changes: 0 additions & 38 deletions lib/pact/provider_verifier/Rakefile

This file was deleted.

9 changes: 8 additions & 1 deletion lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def verify_pacts
proxy_pact_helper = File.expand_path(File.join(File.dirname(__FILE__), "pact_helper.rb"))
ENV['provider_states_url'] = @options.provider_states_url
ENV['provider_states_setup_url'] = @options.provider_states_setup_url
ENV['PACT_BROKER_USERNAME'] = @options.broker_username if @options.broker_username
ENV['PACT_BROKER_PASSWORD'] = @options.broker_password if @options.broker_password
provider_base_url = @options.provider_base_url

Pact.service_provider "Running Provider Application" do
Expand All @@ -56,7 +58,9 @@ def verify_pacts
options = {
:pact_helper => proxy_pact_helper,
:pact_uri => pact_url,
:backtrace => false
:backtrace => false,
:pact_broker_username => @options.broker_username,
:pact_broker_password => @options.broker_password
}
Cli::RunPactVerification.call(options)
rescue SystemExit => e
Expand Down Expand Up @@ -84,6 +88,9 @@ def get_json(path)
def get_json_from_server(path)
url = URI.parse(path)
conn = Faraday.new("http://#{url.host}:#{url.port}") do |c|
if ENV['PACT_BROKER_USERNAME'] && ENV['PACT_BROKER_PASSWORD']
c.use Faraday::Request::BasicAuthentication, ENV['PACT_BROKER_USERNAME'], ENV['PACT_BROKER_PASSWORD']
end
c.use FaradayMiddleware::ParseJson
c.use Faraday::Adapter::NetHttp
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pact/provider_verifier/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class CLI < Thor
method_option :provider_base_url, aliases: "-h", desc: "Provide host URL", :required => true
method_option :provider_states_url, aliases: "-s", desc: "Base URL to retrieve the provider states from", :required => false
method_option :provider_states_setup_url, aliases: "-c", desc: "Base URL to setup the provider states at", :required => false
method_option :broker_username, aliases: "-n", desc: "Pact Broker username", :required => false
method_option :broker_password, aliases: "-p", desc: "Pact Broker password", :required => false

def verify
app = Pact::ProviderVerifier::App.new(options)
Expand Down
3 changes: 3 additions & 0 deletions lib/pact/provider_verifier/pact_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def set_up_state provider_state
puts "Setting up provider state '#{provider_state}' for consumer '#{ENV['pact_consumer']}' using provider state server at #{ENV['provider_states_setup_url']}"

conn = Faraday.new(:url => ENV['provider_states_setup_url']) do |faraday|
if ENV['PACT_BROKER_USERNAME'] && ENV['PACT_BROKER_PASSWORD']
faraday.use Faraday::Request::BasicAuthentication, ENV['PACT_BROKER_USERNAME'], ENV['PACT_BROKER_PASSWORD']
end
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
end
conn.post do |req|
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/provider_verifier/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pact
module ProviderVerifier
VERSION = "0.0.3"
VERSION = "0.0.4"
end
end

0 comments on commit c5dc292

Please sign in to comment.