forked from pact-foundation/pact-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow WIP pacts to be verified without causing the process to r…
…eturn an non zero exit code
- Loading branch information
Showing
22 changed files
with
335 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'pact/hal/entity' | ||
require 'pact/hal/http_client' | ||
require 'pact/provider/pact_uri' | ||
|
||
module Pact | ||
module PactBroker | ||
class FetchWipPacts | ||
attr_reader :provider, :tags, :broker_base_url, :http_client_options, :http_client, :index_entity | ||
|
||
WIP_PROVIDER_RELATION = 'pb:wip-provider-pacts'.freeze | ||
PACTS = 'pacts'.freeze | ||
PB_PACTS = 'pb:pacts'.freeze | ||
HREF = 'href'.freeze | ||
|
||
def initialize(provider, broker_base_url, http_client_options) | ||
@provider = provider | ||
@http_client_options = http_client_options | ||
@broker_base_url = broker_base_url | ||
@http_client = Pact::Hal::HttpClient.new(http_client_options) | ||
end | ||
|
||
def self.call(provider, broker_base_url, http_client_options) | ||
new(provider, broker_base_url, http_client_options).call | ||
end | ||
|
||
def call | ||
log_message | ||
if get_index.success? | ||
get_wip_pacts_for_provider | ||
else | ||
raise Pact::Error.new("Error retrieving #{broker_base_url} status=#{index_entity.response.code} #{index_entity.response.raw_body}") | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_index | ||
@index_entity = Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get | ||
end | ||
|
||
def get_wip_pacts_for_provider | ||
link = index_entity._link(WIP_PROVIDER_RELATION) | ||
if link | ||
get_pact_urls(link.expand(provider: provider).get) | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def get_pact_urls(link_by_provider) | ||
link_by_provider.fetch(PB_PACTS, PACTS).collect do |pact| | ||
Pact::Provider::PactURI.new(pact[HREF], http_client_options) | ||
end | ||
end | ||
|
||
def log_message | ||
message = "INFO: Fetching WIP pacts for #{provider} from #{broker_base_url}" | ||
Pact.configuration.output_stream.puts message | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'pact/pact_broker/fetch_wip_pacts' | ||
|
||
module Pact | ||
module PactBroker | ||
describe FetchWipPacts do | ||
describe "call" do | ||
before do | ||
allow(Pact.configuration).to receive(:output_stream).and_return(double('output stream').as_null_object) | ||
end | ||
|
||
let(:provider) { "Foo"} | ||
let(:broker_base_url) { "http://broker.org" } | ||
let(:http_client_options) { {} } | ||
subject { FetchWipPacts.call(provider, broker_base_url, http_client_options)} | ||
|
||
context "when there is an error retrieving the index resource" do | ||
before do | ||
stub_request(:get, "http://broker.org/").to_return(status: 500, body: "foo", headers: {}) | ||
end | ||
|
||
let(:subject_with_rescue) do | ||
begin | ||
subject | ||
rescue Pact::Error | ||
# can't be bothered stubbing out everything to make the rest of the code execute nicely | ||
# when all we care about is the message | ||
end | ||
end | ||
|
||
it "raises a Pact::Error" do | ||
expect { subject }.to raise_error Pact::Error, /500.*foo/ | ||
end | ||
end | ||
|
||
context "when the pb:wip-provider-pacts relation does not exist" do | ||
before do | ||
stub_request(:get, "http://broker.org/").to_return(status: 200, body: response_body, headers: response_headers) | ||
end | ||
|
||
let(:response_headers) { { "Content-Type" => "application/hal+json" } } | ||
let(:response_body) do | ||
{ | ||
_links: {} | ||
}.to_json | ||
end | ||
|
||
it "returns an empty list" do | ||
expect(subject).to eq [] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.