Skip to content

Commit 76236d8

Browse files
mefellowsbethesque
andcommitted
feat: add token, username and password options to stub service (pact-foundation#118)
Co-authored-by: Beth Skurrie <bethesque@users.noreply.github.com>
1 parent d7d6e35 commit 76236d8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/pact/mock_service/app.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def self.new *args
1717
class App
1818
def initialize options = {}
1919
logger = Logger.from_options(options)
20+
@options = options
2021
stubbing = options[:stub_pactfile_paths] && options[:stub_pactfile_paths].any?
2122
@name = options.fetch(:name, "MockService")
2223
@session = Session.new(options.merge(logger: logger, warn_on_too_many_interactions: !stubbing))
@@ -40,7 +41,7 @@ def shutdown
4041
def setup_stub stub_pactfile_paths
4142
interactions = stub_pactfile_paths.collect do | pactfile_path |
4243
$stdout.puts "INFO: Loading interactions from #{pactfile_path}"
43-
hash_interactions = JSON.parse(Pact::PactFile.read(pactfile_path))['interactions']
44+
hash_interactions = JSON.parse(Pact::PactFile.read(pactfile_path, pactfile_options))['interactions']
4445
hash_interactions.collect { | hash | Interaction.from_hash(hash) }
4546
end.flatten
4647
@session.set_expected_interactions interactions
@@ -57,6 +58,20 @@ def write_pact_if_configured
5758
def to_s
5859
"#{@name} #{super.to_s}"
5960
end
61+
62+
private
63+
64+
def pactfile_options
65+
{
66+
:token => broker_token,
67+
:username => @options[:broker_username],
68+
:password => @options[:broker_password],
69+
}
70+
end
71+
72+
def broker_token
73+
@options[:broker_token] || ENV['PACT_BROKER_TOKEN']
74+
end
6075
end
6176

6277
# Can't write to a file in a TRAP, might deadlock

lib/pact/mock_service/run.rb

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def service_options
5858
unique_pact_file_names: options[:unique_pact_file_names],
5959
consumer: options[:consumer],
6060
provider: options[:provider],
61+
broker_token: options[:broker_token],
62+
broker_username: options[:broker_username],
63+
broker_password: options[:broker_password],
6164
cors_enabled: options[:cors],
6265
pact_specification_version: options[:pact_specification_version] || Pact::SpecificationVersion::NIL_VERSION.to_s,
6366
pactfile_write_mode: options[:pact_file_write_mode],

lib/pact/stub_service/cli.rb

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class CLI < Pact::MockService::CLI::CustomThor
2727
method_option :port, aliases: "-p", desc: "Port on which to run the service"
2828
method_option :host, aliases: "-h", desc: "Host on which to bind the service", default: 'localhost'
2929
method_option :log, aliases: "-l", desc: "File to which to log output"
30+
method_option :broker_username, aliases: "-n", desc: "Pact Broker basic auth username", :required => false
31+
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password", :required => false
32+
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token", :required => false
3033
method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG"
3134
method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses"
3235
method_option :ssl, desc: "Use a self-signed SSL cert to run the service over HTTPS", type: :boolean, default: false

0 commit comments

Comments
 (0)