Skip to content

Commit

Permalink
feat: add support for bearer token
Browse files Browse the repository at this point in the history
  • Loading branch information
vixplows committed Feb 21, 2019
1 parent 7f0357d commit 297268d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/pact/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
module Pact
module Hal
class HttpClient
attr_accessor :username, :password, :verbose
attr_accessor :username, :password, :verbose, :broker_token

def initialize options
@username = options[:username]
@password = options[:password]
@verbose = options[:verbose]
@broker_token = options[:broker_token]
end

def get href, params = {}, headers = {}
Expand Down Expand Up @@ -39,6 +40,7 @@ def create_request uri, http_method, body = nil, headers = {}

request.body = body if body
request.basic_auth username, password if username
request['Authorization'] = "Bearer #{broker_token}" if broker_token
request
end

Expand Down
16 changes: 16 additions & 0 deletions spec/lib/pact/hal/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ module Hal
end
end

context "with broker token set" do
let!(:request) do
stub_request(:any, /.*/).
with( headers: {
'Authorization'=>'Bearer mytoken123'
}).
to_return(status: 200, body: response_body, headers: {'Content-Type' => 'application/json'})
end

subject { HttpClient.new(broker_token: 'mytoken123') }

it "sets a bearer authorization header" do
do_get
expect(request).to have_been_made
end
end

it "retries on failure" do
expect(Retry).to receive(:until_true)
Expand Down

0 comments on commit 297268d

Please sign in to comment.