From 297268d8be284916eaf915d1364319e544a2a532 Mon Sep 17 00:00:00 2001 From: Victoria Plows Date: Thu, 21 Feb 2019 10:22:32 +1100 Subject: [PATCH] feat: add support for bearer token --- lib/pact/hal/http_client.rb | 4 +++- spec/lib/pact/hal/http_client_spec.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/pact/hal/http_client.rb b/lib/pact/hal/http_client.rb index 4395c0bb..a17317eb 100644 --- a/lib/pact/hal/http_client.rb +++ b/lib/pact/hal/http_client.rb @@ -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 = {} @@ -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 diff --git a/spec/lib/pact/hal/http_client_spec.rb b/spec/lib/pact/hal/http_client_spec.rb index 209cec18..62481dfe 100644 --- a/spec/lib/pact/hal/http_client_spec.rb +++ b/spec/lib/pact/hal/http_client_spec.rb @@ -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)