Skip to content

Commit c1df6dd

Browse files
authored
fix: follow first redirect when fetching remote pact artifacts. (pact-foundation#80)
When a pact artifact is fetched remotely, the method in Pact::PactFile that facilitates this will now follow a single redirect response. If the second request results in anything but a 200 response, an error will be raised.
1 parent d1d8261 commit c1df6dd

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/pact/consumer_contract/pact_file.rb

+25
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,37 @@ def get_remote_with_retry(uri_string, options)
7979

8080
def get_remote(uri, options)
8181
request = Net::HTTP::Get.new(uri)
82+
request = prepare_auth(request, options) if options[:username] || options[:token]
83+
84+
http = prepare_request(uri)
85+
response = perform_http_request(http, request, options)
86+
87+
if response.is_a?(Net::HTTPRedirection)
88+
uri = URI(response.header['location'])
89+
req = Net::HTTP::Get.new(uri)
90+
req = prepare_auth(req, options) if options[:username] || options[:token]
91+
92+
http = prepare_request(uri)
93+
response = perform_http_request(http, req, options)
94+
end
95+
response
96+
end
97+
98+
def prepare_auth(request, options)
8299
request.basic_auth(options[:username], options[:password]) if options[:username]
83100
request['Authorization'] = "Bearer #{options[:token]}" if options[:token]
101+
request
102+
end
103+
104+
def prepare_request(uri)
84105
http = Net::HTTP.new(uri.host, uri.port, :ENV)
85106
http.use_ssl = (uri.scheme == 'https')
86107
http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
87108
http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
109+
http
110+
end
111+
112+
def perform_http_request(http, request, options)
88113
http.start do |http|
89114
http.open_timeout = options[:open_timeout] || OPEN_TIMEOUT
90115
http.read_timeout = options[:read_timeout] || READ_TIMEOUT

spec/lib/pact/consumer_contract/pact_file_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Pact
77
describe PactFile do
88
describe 'render_pact' do
99
let(:uri_without_userinfo) { 'http://pactbroker.com'}
10+
let(:redirect_uri_without_userinfo) { 'http://alternate-pactbroker.com'}
1011
let(:pact_content) { 'api contract'}
1112

1213
describe 'from a local file URI' do
@@ -106,6 +107,18 @@ def render_pact(options = {})
106107
end
107108
end
108109

110+
context 'with redirect' do
111+
before do
112+
stub_request(:get, uri_without_userinfo).to_return(status: 302, headers: {"location" => redirect_uri_without_userinfo})
113+
stub_request(:get, redirect_uri_without_userinfo).to_return(status: 200, body: pact_content)
114+
end
115+
116+
it 'succeeds' do
117+
expect(PactFile).not_to receive(:delay_retry)
118+
expect(render_pact).to eq(pact_content)
119+
end
120+
end
121+
109122
context 'with single server error' do
110123
before do
111124
stub_request(:get, uri_without_userinfo).to_return(status: 500).

0 commit comments

Comments
 (0)