Skip to content

Commit

Permalink
fix: use configured credentials when fetching the diff with previous …
Browse files Browse the repository at this point in the history
…version

Closes: #205
  • Loading branch information
bethesque committed Jan 11, 2020
1 parent 8a5eacd commit b9deb09
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 208 deletions.
7 changes: 4 additions & 3 deletions lib/pact/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ def post href, body = nil, headers = {}

def create_request uri, http_method, body = nil, headers = {}
request = Net::HTTP.const_get(http_method).new(uri.request_uri)
request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method)
request['Accept'] = "application/hal+json"
headers.each do | key, value |
request[key] = value
end

request.body = body if body
request.basic_auth username, password if username
request['Authorization'] = "Bearer #{token}" if token
Expand Down Expand Up @@ -85,6 +82,10 @@ def status
def success?
__getobj__().code.start_with?("2")
end

def json?
self['content-type'] && self['content-type'] =~ /json/
end
end
end
end
Expand Down
24 changes: 15 additions & 9 deletions lib/pact/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ def initialize(attrs, http_client)
end

def run(payload = nil)
response = case request_method
when :get
get(payload)
when :put
put(payload)
when :post
post(payload)
end
case request_method
when :get
get(payload)
when :put
put(payload)
when :post
post(payload)
end
end

def title_or_name
Expand Down Expand Up @@ -89,8 +89,14 @@ def with_query(query)

def wrap_response(href, http_response)
require 'pact/hal/entity' # avoid circular reference
require 'pact/hal/non_json_entity'

if http_response.success?
Entity.new(href, http_response.body, @http_client, http_response)
if http_response.json?
Entity.new(href, http_response.body, @http_client, http_response)
else
NonJsonEntity.new(href, http_response.raw_body, @http_client, http_response)
end
else
ErrorEntity.new(href, http_response.raw_body, @http_client, http_response)
end
Expand Down
28 changes: 28 additions & 0 deletions lib/pact/hal/non_json_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Pact
module Hal
class NonJsonEntity
def initialize(href, body, http_client, response = nil)
@href = href
@body = body
@client = http_client
@response = response
end

def success?
true
end

def response
@response
end

def body
@body
end

def assert_success!
self
end
end
end
end
8 changes: 4 additions & 4 deletions lib/pact/provider/help/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module Provider
module Help
class Content

def initialize pact_jsons
@pact_jsons = pact_jsons
def initialize pact_sources
@pact_sources = pact_sources
end

def text
Expand All @@ -15,7 +15,7 @@ def text

private

attr_reader :pact_jsons
attr_reader :pact_sources

def help_text
temp_dir = Pact.configuration.tmp_dir
Expand All @@ -28,7 +28,7 @@ def template_string
end

def pact_diffs
pact_jsons.collect do | pact_json |
pact_sources.collect do | pact_json |
PactDiff.call(pact_json)
end.compact.join("\n")
end
Expand Down
42 changes: 10 additions & 32 deletions lib/pact/provider/help/pact_diff.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
require 'pact/hal/entity'

module Pact
module Provider
module Help
class PactDiff
class PrintPactDiffError < StandardError; end

attr_reader :pact_json, :output
attr_reader :pact_source, :output

def initialize pact_json
@pact_json = pact_json
def initialize pact_source
@pact_source = pact_source
end

def self.call pact_json
new(pact_json).call
def self.call pact_source
new(pact_source).call
end

def call
begin
if diff_rel && diff_url
header + "\n" + get_diff
end
header + "\n" + get_diff
rescue PrintPactDiffError => e
return e.message
end
Expand All @@ -30,35 +30,13 @@ def header
"The following changes have been made since the previous distinct version of this pact, and may be responsible for verification failure:\n"
end

def pact_hash
@pact_hash ||= json_load(pact_json)
end

def links
pact_hash['_links'] || pact_hash['links']
end

def diff_rel
return nil unless links
key = links.keys.find { | key | key =~ /diff/ && key =~ /distinct/ && key =~ /previous/}
key ? links[key] : nil
end

def diff_url
diff_rel['href']
end

def get_diff
begin
URI.open(diff_url) { | file | file.read }
pact_source.hal_entity._link!("pb:diff-previous-distinct").get!(nil, "Accept" => "text/plain").body
rescue StandardError => e
raise PrintPactDiffError.new("Tried to retrieve diff with previous pact from #{diff_url}, but received response code #{e}.")
raise PrintPactDiffError.new("Tried to retrieve diff with previous pact, but received error #{e.class} #{e.message}.")
end
end

def json_load json
JSON.load(json, nil, { max_nesting: 50 })
end
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/pact/provider/help/write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Write

HELP_FILE_NAME = 'help.md'

def self.call pact_jsons, reports_dir = Pact.configuration.reports_dir
new(pact_jsons, reports_dir).call
def self.call pact_sources, reports_dir = Pact.configuration.reports_dir
new(pact_sources, reports_dir).call
end

def initialize pact_jsons, reports_dir
@pact_jsons = pact_jsons
def initialize pact_sources, reports_dir
@pact_sources = pact_sources
@reports_dir = File.expand_path(reports_dir)
end

Expand All @@ -25,7 +25,7 @@ def call

private

attr_reader :reports_dir, :pact_jsons
attr_reader :reports_dir, :pact_sources

def clean_reports_dir
raise "Cleaning report dir #{reports_dir} would delete project!" if reports_dir_contains_pwd
Expand All @@ -46,9 +46,8 @@ def help_path
end

def help_text
Content.new(pact_jsons).text
Content.new(pact_sources).text
end

end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/pact/provider/pact_source.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'pact/consumer_contract/pact_file'
require 'pact/hal/http_client'
require 'pact/hal/entity'

module Pact
module Provider
Expand All @@ -17,6 +19,13 @@ def pact_json
def pact_hash
@pact_hash ||= JSON.load(pact_json, nil, { max_nesting: 50 })
end

def hal_entity
http_client_keys = [:username, :password, :token]
http_client_options = uri.options.reject{ |k, _| !http_client_keys.include?(k) }
http_client = Pact::Hal::HttpClient.new(http_client_options.merge(verbose: true))
Pact::Hal::Entity.new(uri, pact_hash, http_client)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact/provider/pact_spec_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def configure_rspec
executing_with_ruby = executing_with_ruby?

config.after(:suite) do | suite |
Pact::Provider::Help::Write.call(jsons) if executing_with_ruby
Pact::Provider::Help::Write.call(Pact.provider_world.pact_sources) if executing_with_ruby
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/publish_verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
subject { Pact::Provider::VerificationResults::PublishAll.call(pact_sources, test_results_hash) }

let!(:request) do
stub_request(:post, 'http://publish').to_return(status: 200, body: created_verification_body)
stub_request(:post, 'http://publish').to_return(status: 200, headers: {'Content-Type' => 'application/hal+json'}, body: created_verification_body)
end

it "publishes the results" do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Hal
end

let(:provider_response) do
double('response', body: provider_hash, success?: true)
double('response', body: provider_hash, success?: true, json?: true)
end

let(:provider_hash) do
Expand Down
7 changes: 3 additions & 4 deletions spec/lib/pact/hal/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Hal
let!(:request) do
stub_request(:get, "http://example.org/").
with( headers: {
'Accept'=>'application/hal+json',
'Accept'=>'*/*',
'Authorization'=>'Basic Zm9vOmJhcg=='
}).
to_return(status: 200, body: response_body, headers: {'Content-Type' => 'application/json'})
Expand Down Expand Up @@ -86,9 +86,8 @@ module Hal
let!(:request) do
stub_request(:post, "http://example.org/").
with( headers: {
'Accept'=>'application/hal+json',
'Authorization'=>'Basic Zm9vOmJhcg==',
'Content-Type'=>'application/json'
'Accept'=>'*/*',
'Authorization'=>'Basic Zm9vOmJhcg=='
},
body: request_body).
to_return(status: 200, body: response_body, headers: {'Content-Type' => 'application/json'})
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact/hal/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Hal
end

let(:response) do
instance_double('Pact::Hal::HttpClient::Response', success?: success, body: response_body, raw_body: response_body.to_json)
instance_double('Pact::Hal::HttpClient::Response', success?: success, body: response_body, raw_body: response_body.to_json, json?: true)
end

let(:success) { true }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact/pact_broker/fetch_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module PactBroker

context "when there is a HAL relation missing" do
before do
stub_request(:get, "http://broker.org/").to_return(status: 200, body: {"_links" => {} }.to_json, headers: {})
stub_request(:get, "http://broker.org/").to_return(status: 200, body: {"_links" => {} }.to_json, headers: {"Content-Type" => "application/hal+json"})
end

it "raises a Pact::Error" do
Expand Down
17 changes: 7 additions & 10 deletions spec/lib/pact/provider/help/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ module Pact
module Provider
module Help
describe Content do

describe "#text" do

let(:pact_1_json) { { some: 'json'}.to_json }
let(:pact_2_json) { { some: 'other json'}.to_json }
let(:pact_jsons) { [pact_1_json, pact_2_json] }

before do
allow(PactDiff).to receive(:call).with(pact_1_json).and_return('diff 1')
allow(PactDiff).to receive(:call).with(pact_2_json).and_return(nil)
allow(PactDiff).to receive(:call).with(pact_source_1).and_return('diff 1')
allow(PactDiff).to receive(:call).with(pact_source_2).and_return(nil)
end

subject { Content.new(pact_jsons) }
let(:pact_source_1) { { some: 'json'}.to_json }
let(:pact_source_2) { { some: 'other json'}.to_json }
let(:pact_sources) { [pact_source_1, pact_source_2] }

subject { Content.new(pact_sources) }

it "displays the log path" do
expect(subject.text).to include Pact.configuration.log_path
Expand All @@ -29,7 +27,6 @@ module Help
it "displays the diff" do
expect(subject.text).to include 'diff 1'
end

end
end
end
Expand Down
Loading

0 comments on commit b9deb09

Please sign in to comment.