pact_broker-client 1.16.2 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac3b8dc618560f1c89e8b1f47c3e77d4a50dd091
4
- data.tar.gz: 535a97e290212d90e7478b60653f774060b41a88
3
+ metadata.gz: fdab3c67440cdd2d780675b8a599d6f1dd09e3d1
4
+ data.tar.gz: 8f114056c4f6447b1cd6efac1434d9a0eb878d67
5
5
  SHA512:
6
- metadata.gz: cc58335634f112c188020e115f3e492da5d300898ad6c8e229f5307ad85d608b44c9e806f658c6cde6533939b5ed89058efdc9a038e1879fed31609fc3f3fb6a
7
- data.tar.gz: f37507fb98f716d1c2c0ef1ad6c903493521059dc829b5d5ced9f6449f557d81fcf72d8e5b9c9800709a87938007c538a1a23052302d1eaf12715c3508635ae8
6
+ metadata.gz: 39b123daae457e35c9a64492ef8516979259cef4e71462c990c46d22c02cfaa0156262f42710f2e00ae97aebe6facb261c3ed1e117315ba44fdca94f938c042b
7
+ data.tar.gz: 994166b7e4151a37b36f783385634363cc3aa14056e89faaf5740f2882cebb53054bd1fe2b23973d0a70f6d699f2f106370e1fdb1f9d73306410fc4829fa417d
@@ -1,3 +1,12 @@
1
+ <a name="v1.17.0"></a>
2
+ ### v1.17.0 (2018-11-15)
3
+
4
+
5
+ #### Features
6
+
7
+ * update HAL client from pact codebase ([ed77fe0](/../../commit/ed77fe0))
8
+
9
+
1
10
  <a name="v1.16.0"></a>
2
11
  ### v1.16.0 (2018-07-09)
3
12
 
@@ -1,12 +1,18 @@
1
- require 'uri'
1
+ require 'erb'
2
2
  require 'delegate'
3
3
  require 'pact_broker/client/hal/link'
4
4
 
5
5
  module PactBroker
6
6
  module Client
7
7
  module Hal
8
+ class RelationNotFoundError < ::PactBroker::Client::Error; end
9
+
10
+ class ErrorResponseReturned < ::PactBroker::Client::Error; end
11
+
8
12
  class Entity
9
- def initialize(data, http_client, response = nil)
13
+
14
+ def initialize(href, data, http_client, response = nil)
15
+ @href = href
10
16
  @data = data
11
17
  @links = (@data || {}).fetch("_links", {})
12
18
  @client = http_client
@@ -33,14 +39,20 @@ module PactBroker
33
39
  Link.new(@links[key].merge(method: http_method), @client).run(*args)
34
40
  end
35
41
 
36
- def _link(key)
42
+ def _link(key, fallback_key = nil)
37
43
  if @links[key]
38
44
  Link.new(@links[key], @client)
45
+ elsif fallback_key && @links[fallback_key]
46
+ Link.new(@links[fallback_key], @client)
39
47
  else
40
48
  nil
41
49
  end
42
50
  end
43
51
 
52
+ def _link!(key)
53
+ _link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
54
+ end
55
+
44
56
  def success?
45
57
  true
46
58
  end
@@ -49,8 +61,8 @@ module PactBroker
49
61
  @response
50
62
  end
51
63
 
52
- def fetch(key)
53
- @links[key]
64
+ def fetch(key, fallback_key = nil)
65
+ @links[key] || (fallback_key && @links[fallback_key])
54
66
  end
55
67
 
56
68
  def method_missing(method_name, *args, &block)
@@ -66,12 +78,29 @@ module PactBroker
66
78
  def respond_to_missing?(method_name, include_private = false)
67
79
  @data.key?(method_name) || @links.key?(method_name)
68
80
  end
81
+
82
+ def assert_success!
83
+ self
84
+ end
69
85
  end
70
86
 
71
87
  class ErrorEntity < Entity
88
+
89
+ def initialize(href, data, http_client, response = nil)
90
+ @href = href
91
+ @data = data
92
+ @links = {}
93
+ @client = http_client
94
+ @response = response
95
+ end
96
+
72
97
  def success?
73
98
  false
74
99
  end
100
+
101
+ def assert_success!
102
+ raise ErrorResponseReturned.new("Error retrieving #{@href} status=#{response ? response.code: nil} #{response ? response.raw_body : ''}")
103
+ end
75
104
  end
76
105
  end
77
106
  end
@@ -4,7 +4,7 @@ module PactBroker
4
4
  module Client
5
5
  module Hal
6
6
  class HttpClient
7
- attr_reader :username, :password, :verbose
7
+ attr_accessor :username, :password, :verbose
8
8
 
9
9
  def initialize options
10
10
  @username = options[:username]
@@ -56,7 +56,7 @@ module PactBroker
56
56
 
57
57
  class Response < SimpleDelegator
58
58
  def body
59
- bod = __getobj__().body
59
+ bod = raw_body
60
60
  if bod && bod != ''
61
61
  JSON.parse(bod)
62
62
  else
@@ -77,6 +77,7 @@ module PactBroker
77
77
  end
78
78
  end
79
79
  end
80
+
80
81
  end
81
82
  end
82
83
  end
@@ -38,15 +38,15 @@ module PactBroker
38
38
  end
39
39
 
40
40
  def get(payload = {}, headers = {})
41
- wrap_response(@http_client.get(href, payload, headers))
41
+ wrap_response(href, @http_client.get(href, payload, headers))
42
42
  end
43
43
 
44
44
  def put(payload = nil, headers = {})
45
- wrap_response(@http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
45
+ wrap_response(href, @http_client.put(href, payload ? JSON.dump(payload) : nil, headers))
46
46
  end
47
47
 
48
48
  def post(payload = nil, headers = {})
49
- wrap_response(@http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
49
+ wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers))
50
50
  end
51
51
 
52
52
  def expand(params)
@@ -57,21 +57,19 @@ module PactBroker
57
57
 
58
58
  private
59
59
 
60
- def wrap_response(http_response)
61
- require 'pact_broker/client/hal/entity' # avoid circular reference
60
+ def wrap_response(href, http_response)
61
+ require 'pact/hal/entity' # avoid circular reference
62
62
  if http_response.success?
63
- Entity.new(http_response.body, @http_client, http_response)
63
+ Entity.new(href, http_response.body, @http_client, http_response)
64
64
  else
65
- ErrorEntity.new(http_response.body, @http_client, http_response)
65
+ ErrorEntity.new(href, http_response.raw_body, @http_client, http_response)
66
66
  end
67
67
  end
68
68
 
69
69
  def expand_url(params, url)
70
- new_url = url
71
- params.each do | key, value |
72
- new_url = new_url.gsub('{' + key.to_s + '}', URI.escape(value))
70
+ params.inject(url) do | url, (key, value) |
71
+ url.gsub('{' + key.to_s + '}', ERB::Util.url_encode(value))
73
72
  end
74
- new_url
75
73
  end
76
74
  end
77
75
  end
@@ -1,5 +1,5 @@
1
1
  module PactBroker
2
2
  module Client
3
- VERSION = '1.16.2'
3
+ VERSION = '1.17.0'
4
4
  end
5
5
  end
@@ -32,7 +32,7 @@ module PactBroker::Client
32
32
  }
33
33
  end
34
34
 
35
- subject(:entity) { Entity.new(pact_hash, http_client) }
35
+ subject(:entity) { Entity.new("http://pact", pact_hash, http_client) }
36
36
 
37
37
  it "delegates to the properties in the data" do
38
38
  expect(subject.name).to eq "a name"
@@ -60,6 +60,32 @@ module PactBroker::Client
60
60
  end
61
61
  end
62
62
 
63
+ describe "assert_success!" do
64
+ context "when the response is successful" do
65
+ it "returns the entity" do
66
+ expect(entity.assert_success!).to be entity
67
+ end
68
+ end
69
+
70
+ context "when the response is not successful and there is no response" do
71
+ subject(:entity) { ErrorEntity.new("http://pact", pact_hash, http_client) }
72
+
73
+ it "raises an error" do
74
+ expect { entity.assert_success! }.to raise_error ErrorResponseReturned, "Error retrieving http://pact status= "
75
+ end
76
+ end
77
+
78
+ context "when the response is not successful and there is a response" do
79
+ let(:response) { double('response', code: 200, raw_body: "body") }
80
+
81
+ subject(:entity) { ErrorEntity.new("http://pact", pact_hash, http_client, response) }
82
+
83
+ it "raises an error" do
84
+ expect { entity.assert_success! }.to raise_error ErrorResponseReturned, "Error retrieving http://pact status=200 body"
85
+ end
86
+ end
87
+ end
88
+
63
89
  describe "can?" do
64
90
  context "when the relation exists" do
65
91
  it "returns true" do
@@ -74,19 +100,47 @@ module PactBroker::Client
74
100
  end
75
101
  end
76
102
 
103
+ describe "_link!" do
104
+ context 'when the key exists' do
105
+ it 'returns a Link' do
106
+ expect(subject._link!('pb:provider')).to be_a(Link)
107
+ expect(subject._link!('pb:provider').href).to eq 'http://provider'
108
+ end
109
+ end
110
+
111
+ context 'when the key does not exist' do
112
+ it 'raises an error' do
113
+ expect { subject._link!('foo') }.to raise_error RelationNotFoundError, "Could not find relation 'foo' in resource at http://pact"
114
+ end
115
+ end
116
+ end
117
+
77
118
  describe 'fetch' do
78
- context 'when the key exist' do
119
+ context 'when the key exists' do
79
120
  it 'returns fetched value' do
80
- expect(subject.fetch('pb:provider')).to be do
81
- {href: 'http://provider'}
82
- end
121
+ expect(subject.fetch('pb:provider')).to eq("href" => 'http://provider')
83
122
  end
84
123
  end
124
+
85
125
  context "when the key doesn't not exist" do
86
126
  it 'returns nil' do
87
127
  expect(subject.fetch('i-dont-exist')).to be nil
88
128
  end
89
129
  end
130
+
131
+ context "when a fallback key is provided" do
132
+ context "when the fallback value exists" do
133
+ it "returns the fallback value" do
134
+ expect(subject.fetch('i-dont-exist', 'pb:provider')).to eq("href" => 'http://provider')
135
+ end
136
+ end
137
+
138
+ context "when the fallback value does not exist" do
139
+ it "returns nil" do
140
+ expect(subject.fetch('i-dont-exist', 'i-also-dont-exist')).to be nil
141
+ end
142
+ end
143
+ end
90
144
  end
91
145
  end
92
146
  end
@@ -10,7 +10,7 @@ module PactBroker::Client
10
10
  end
11
11
 
12
12
  let(:response) do
13
- instance_double('PactBroker::Client::Hal::HttpClient::Response', success?: success, body: response_body)
13
+ instance_double('PactBroker::Client::Hal::HttpClient::Response', success?: success, body: response_body, raw_body: response_body.to_json)
14
14
  end
15
15
 
16
16
  let(:success) { true }
@@ -48,7 +48,7 @@ module PactBroker::Client
48
48
  end
49
49
 
50
50
  it "creates an Entity" do
51
- expect(PactBroker::Client::Hal::Entity).to receive(:new).with(response_body, http_client, response)
51
+ expect(PactBroker::Client::Hal::Entity).to receive(:new).with("http://foo/{bar}", response_body, http_client, response)
52
52
  do_run
53
53
  end
54
54
 
@@ -64,7 +64,7 @@ module PactBroker::Client
64
64
  let(:success) { false }
65
65
 
66
66
  it "creates an ErrorEntity" do
67
- expect(PactBroker::Client::Hal::ErrorEntity).to receive(:new).with(response_body, http_client, response)
67
+ expect(PactBroker::Client::Hal::ErrorEntity).to receive(:new).with("http://foo/{bar}", response_body.to_json, http_client, response)
68
68
  do_run
69
69
  end
70
70
  end
@@ -102,6 +102,10 @@ module PactBroker::Client
102
102
  it "returns a duplicate Link with the expanded href with URL escaping" do
103
103
  expect(subject.expand(bar: 'wiffle meep').href).to eq "http://foo/wiffle%20meep"
104
104
  end
105
+
106
+ it "returns a duplicate Link with the expanded href with URL escaping for forward slashes" do
107
+ expect(subject.expand(bar: 'wiffle/meep').href).to eq "http://foo/wiffle%2Fmeep"
108
+ end
105
109
  end
106
110
  end
107
111
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pact_broker-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.16.2
4
+ version: 1.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Beth Skurrie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2018-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty