Skip to content

Commit 5db4134

Browse files
committedJul 24, 2018
feat: raise error when an expected HAL relation cannot be found in a resource
1 parent 10ebf85 commit 5db4134

File tree

7 files changed

+49
-15
lines changed

7 files changed

+49
-15
lines changed
 

‎lib/pact/hal/entity.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
module Pact
66
module Hal
7+
class RelationNotFoundError < ::Pact::Error; end
8+
79
class Entity
8-
def initialize(data, http_client, response = nil)
10+
11+
def initialize(href, data, http_client, response = nil)
12+
@href = href
913
@data = data
1014
@links = (@data || {}).fetch("_links", {})
1115
@client = http_client
@@ -40,6 +44,10 @@ def _link(key)
4044
end
4145
end
4246

47+
def _link!(key)
48+
_link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
49+
end
50+
4351
def success?
4452
true
4553
end
@@ -69,7 +77,8 @@ def respond_to_missing?(method_name, include_private = false)
6977

7078
class ErrorEntity < Entity
7179

72-
def initialize(data, http_client, response = nil)
80+
def initialize(href, data, http_client, response = nil)
81+
@href = href
7382
@data = data
7483
@links = {}
7584
@client = http_client

‎lib/pact/hal/link.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def run(payload = nil)
2525
end
2626

2727
def get(payload = {}, headers = {})
28-
wrap_response(@http_client.get(href, payload, headers))
28+
wrap_response(href, @http_client.get(href, payload, headers))
2929
end
3030

3131
def put(payload = nil, headers = {})
32-
wrap_response(@http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
32+
wrap_response(href, @http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
3333
end
3434

3535
def post(payload = nil, headers = {})
36-
wrap_response(@http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
36+
wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
3737
end
3838

3939
def expand(params)
@@ -44,12 +44,12 @@ def expand(params)
4444

4545
private
4646

47-
def wrap_response(http_response)
47+
def wrap_response(href, http_response)
4848
require 'pact/hal/entity' # avoid circular reference
4949
if http_response.success?
50-
Entity.new(http_response.body, @http_client, http_response)
50+
Entity.new(href, http_response.body, @http_client, http_response)
5151
else
52-
ErrorEntity.new(http_response.raw_body, @http_client, http_response)
52+
ErrorEntity.new(href, http_response.raw_body, @http_client, http_response)
5353
end
5454
end
5555

‎lib/pact/pact_broker/fetch_pacts.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ def tagged_pacts_for_provider
6464

6565
def link_for(tag)
6666
if !tag[:all]
67-
index_entity._link(LATEST_PROVIDER_TAG_RELATION)
67+
index_entity._link!(LATEST_PROVIDER_TAG_RELATION)
6868
else
69-
index_entity._link(ALL_PROVIDER_TAG_RELATION)
69+
index_entity._link!(ALL_PROVIDER_TAG_RELATION)
7070
end
7171
end
7272

@@ -75,7 +75,7 @@ def index
7575
end
7676

7777
def latest_pacts_for_provider
78-
link = index_entity._link(LATEST_PROVIDER_RELATION)
78+
link = index_entity._link!(LATEST_PROVIDER_RELATION)
7979
pact_urls(link.expand(provider: provider).get)
8080
end
8181

‎lib/pact/provider/verification_results/publish.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def initialize pact_source, verification_result
3232
end
3333

3434
@http_client = Pact::Hal::HttpClient.new(http_client_options)
35-
@pact_entity = Pact::Hal::Entity.new(pact_source.pact_hash, http_client)
35+
@pact_entity = Pact::Hal::Entity.new(pact_source.uri, pact_source.pact_hash, http_client)
3636
end
3737

3838
def call

‎spec/lib/pact/hal/entity_spec.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Hal
3232
}
3333
end
3434

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

3737
it "delegates to the properties in the data" do
3838
expect(subject.name).to eq "a name"
@@ -74,6 +74,21 @@ module Hal
7474
end
7575
end
7676

77+
describe "_link!" do
78+
context 'when the key exists' do
79+
it 'returns a Link' do
80+
expect(subject._link!('pb:provider')).to be_a(Link)
81+
expect(subject._link!('pb:provider').href).to eq 'http://provider'
82+
end
83+
end
84+
85+
context 'when the key does not exist' do
86+
it 'raises an error' do
87+
expect { subject._link!('foo') }.to raise_error RelationNotFoundError, "Could not find relation 'foo' in resource at http://pact"
88+
end
89+
end
90+
end
91+
7792
describe 'fetch' do
7893
context 'when the key exists' do
7994
it 'returns fetched value' do

‎spec/lib/pact/hal/link_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module Hal
4848
end
4949

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

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

6666
it "creates an ErrorEntity" do
67-
expect(Pact::Hal::ErrorEntity).to receive(:new).with(response_body.to_json, http_client, response)
67+
expect(Pact::Hal::ErrorEntity).to receive(:new).with("http://foo/{bar}", response_body.to_json, http_client, response)
6868
do_run
6969
end
7070
end

‎spec/lib/pact/pact_broker/fetch_pacts_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ module PactBroker
3131
end
3232
end
3333

34+
context "when there is a HAL relation missing" do
35+
before do
36+
stub_request(:get, "http://broker.org/").to_return(status: 200, body: {"_links" => {} }.to_json, headers: {})
37+
end
38+
39+
it "raises a Pact::Error" do
40+
expect { subject }.to raise_error Pact::Error, /Could not find relation/
41+
end
42+
end
43+
3444
context "for the latest tag" do
3545
it "logs a message" do
3646
expect(Pact.configuration.output_stream).to receive(:puts).with("INFO: Fetching pacts for Foo from http://broker.org for tags: latest master, latest prod")

0 commit comments

Comments
 (0)
Please sign in to comment.