Skip to content

Commit 3288b81

Browse files
committed
fix: log HTTP request for pacts retrieved by URL when requested with verbose=true
1 parent ec3a1ee commit 3288b81

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

lib/pact/consumer_contract/pact_file.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require 'net/http'
1+
require "net/http"
2+
require "pact/configuration"
3+
require "pact/http/authorization_header_redactor"
24

35
module Pact
46
module PactFile
@@ -81,15 +83,15 @@ def get_remote(uri, options)
8183
request = Net::HTTP::Get.new(uri)
8284
request = prepare_auth(request, options) if options[:username] || options[:token]
8385

84-
http = prepare_request(uri)
86+
http = prepare_request(uri, options)
8587
response = perform_http_request(http, request, options)
8688

8789
if response.is_a?(Net::HTTPRedirection)
8890
uri = URI(response.header['location'])
8991
req = Net::HTTP::Get.new(uri)
9092
req = prepare_auth(req, options) if options[:username] || options[:token]
9193

92-
http = prepare_request(uri)
94+
http = prepare_request(uri, options)
9395
response = perform_http_request(http, req, options)
9496
end
9597
response
@@ -101,11 +103,12 @@ def prepare_auth(request, options)
101103
request
102104
end
103105

104-
def prepare_request(uri)
106+
def prepare_request(uri, options)
105107
http = Net::HTTP.new(uri.host, uri.port, :ENV)
106108
http.use_ssl = (uri.scheme == 'https')
107109
http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
108110
http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
111+
http.set_debug_output(Pact::Http::AuthorizationHeaderRedactor.new(Pact.configuration.output_stream)) if options[:verbose]
109112
http
110113
end
111114

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "delegate"
2+
3+
module Pact
4+
module Http
5+
class AuthorizationHeaderRedactor < SimpleDelegator
6+
def puts(*args)
7+
__getobj__().puts(*redact_args(args))
8+
end
9+
10+
def print(*args)
11+
__getobj__().puts(*redact_args(args))
12+
end
13+
14+
def <<(*args)
15+
__getobj__().send(:<<, *redact_args(args))
16+
end
17+
18+
private
19+
20+
attr_reader :redactions
21+
22+
def redact_args(args)
23+
args.collect{ | s| redact(s) }
24+
end
25+
26+
def redact(string)
27+
return string unless string.is_a?(String)
28+
string.gsub(/Authorization: .*\\r\\n/, "Authorization: [redacted]\\r\\n")
29+
end
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "pact/http/authorization_header_redactor"
2+
3+
module Pact
4+
module Http
5+
describe AuthorizationHeaderRedactor do
6+
let(:stream) { StringIO.new }
7+
let(:stream_redactor) { AuthorizationHeaderRedactor.new(stream) }
8+
9+
it "redacts the authorizaton header" do
10+
stream_redactor << "\\r\\nAuthorization: Bearer TOKEN\\r\\n"
11+
expect(stream.string).to eq "\\r\\nAuthorization: [redacted]\\r\\n"
12+
end
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)