Skip to content

Commit

Permalink
feat: raise error when an expected HAL relation cannot be found in a …
Browse files Browse the repository at this point in the history
…resource
  • Loading branch information
bethesque committed Jul 24, 2018
1 parent 10ebf85 commit 5db4134
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
13 changes: 11 additions & 2 deletions lib/pact/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

module Pact
module Hal
class RelationNotFoundError < ::Pact::Error; end

class Entity
def initialize(data, http_client, response = nil)

def initialize(href, data, http_client, response = nil)
@href = href
@data = data
@links = (@data || {}).fetch("_links", {})
@client = http_client
Expand Down Expand Up @@ -40,6 +44,10 @@ def _link(key)
end
end

def _link!(key)
_link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
end

def success?
true
end
Expand Down Expand Up @@ -69,7 +77,8 @@ def respond_to_missing?(method_name, include_private = false)

class ErrorEntity < Entity

def initialize(data, http_client, response = nil)
def initialize(href, data, http_client, response = nil)
@href = href
@data = data
@links = {}
@client = http_client
Expand Down
12 changes: 6 additions & 6 deletions lib/pact/hal/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def run(payload = nil)
end

def get(payload = {}, headers = {})
wrap_response(@http_client.get(href, payload, headers))
wrap_response(href, @http_client.get(href, payload, headers))
end

def put(payload = nil, headers = {})
wrap_response(@http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
wrap_response(href, @http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
end

def post(payload = nil, headers = {})
wrap_response(@http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
end

def expand(params)
Expand All @@ -44,12 +44,12 @@ def expand(params)

private

def wrap_response(http_response)
def wrap_response(href, http_response)
require 'pact/hal/entity' # avoid circular reference
if http_response.success?
Entity.new(http_response.body, @http_client, http_response)
Entity.new(href, http_response.body, @http_client, http_response)
else
ErrorEntity.new(http_response.raw_body, @http_client, http_response)
ErrorEntity.new(href, http_response.raw_body, @http_client, http_response)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/pact/pact_broker/fetch_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def tagged_pacts_for_provider

def link_for(tag)
if !tag[:all]
index_entity._link(LATEST_PROVIDER_TAG_RELATION)
index_entity._link!(LATEST_PROVIDER_TAG_RELATION)
else
index_entity._link(ALL_PROVIDER_TAG_RELATION)
index_entity._link!(ALL_PROVIDER_TAG_RELATION)
end
end

Expand All @@ -75,7 +75,7 @@ def index
end

def latest_pacts_for_provider
link = index_entity._link(LATEST_PROVIDER_RELATION)
link = index_entity._link!(LATEST_PROVIDER_RELATION)
pact_urls(link.expand(provider: provider).get)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact/provider/verification_results/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize pact_source, verification_result
end

@http_client = Pact::Hal::HttpClient.new(http_client_options)
@pact_entity = Pact::Hal::Entity.new(pact_source.pact_hash, http_client)
@pact_entity = Pact::Hal::Entity.new(pact_source.uri, pact_source.pact_hash, http_client)
end

def call
Expand Down
17 changes: 16 additions & 1 deletion spec/lib/pact/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Hal
}
end

subject(:entity) { Entity.new(pact_hash, http_client) }
subject(:entity) { Entity.new("http://pact", pact_hash, http_client) }

it "delegates to the properties in the data" do
expect(subject.name).to eq "a name"
Expand Down Expand Up @@ -74,6 +74,21 @@ module Hal
end
end

describe "_link!" do
context 'when the key exists' do
it 'returns a Link' do
expect(subject._link!('pb:provider')).to be_a(Link)
expect(subject._link!('pb:provider').href).to eq 'http://provider'
end
end

context 'when the key does not exist' do
it 'raises an error' do
expect { subject._link!('foo') }.to raise_error RelationNotFoundError, "Could not find relation 'foo' in resource at http://pact"
end
end
end

describe 'fetch' do
context 'when the key exists' do
it 'returns fetched value' do
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact/hal/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module Hal
end

it "creates an Entity" do
expect(Pact::Hal::Entity).to receive(:new).with(response_body, http_client, response)
expect(Pact::Hal::Entity).to receive(:new).with("http://foo/{bar}", response_body, http_client, response)
do_run
end

Expand All @@ -64,7 +64,7 @@ module Hal
let(:success) { false }

it "creates an ErrorEntity" do
expect(Pact::Hal::ErrorEntity).to receive(:new).with(response_body.to_json, http_client, response)
expect(Pact::Hal::ErrorEntity).to receive(:new).with("http://foo/{bar}", response_body.to_json, http_client, response)
do_run
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/pact/pact_broker/fetch_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ module PactBroker
end
end

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: {})
end

it "raises a Pact::Error" do
expect { subject }.to raise_error Pact::Error, /Could not find relation/
end
end

context "for the latest tag" do
it "logs a message" do
expect(Pact.configuration.output_stream).to receive(:puts).with("INFO: Fetching pacts for Foo from http://broker.org for tags: latest master, latest prod")
Expand Down

0 comments on commit 5db4134

Please sign in to comment.