Skip to content

Commit 003e305

Browse files
committed
feat: change query params for matrix to use pacticipant[]=? and version[]=?
1 parent 0dc86c2 commit 003e305

File tree

5 files changed

+82
-15
lines changed

5 files changed

+82
-15
lines changed

lib/pact_broker/client/base_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'erb'
22
require 'httparty'
33
require 'pact_broker/client/error'
4+
require 'cgi'
45

56
module PactBroker
67
module Client
@@ -11,6 +12,10 @@ module UrlHelpers
1112
def encode_param param
1213
ERB::Util.url_encode param
1314
end
15+
16+
def encode_query_param param
17+
CGI::escape param
18+
end
1419
end
1520

1621
module StringToSymbol

lib/pact_broker/client/matrix.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module PactBroker
44
module Client
55
class Matrix < BaseClient
66
def get selectors
7-
query = {selectors: convert_selector_hashes_to_params(selectors)}
7+
query = convert_selector_hashes_to_params(selectors)
88
response = self.class.get("/matrix", query: query, headers: default_get_headers)
99
$stdout.puts("DEBUG: Response headers #{response.headers}") if verbose?
1010
$stdout.puts("DEBUG: Response body #{response}") if verbose?
@@ -32,7 +32,7 @@ def handle_response response
3232
end
3333

3434
def convert_selector_hashes_to_params(selectors)
35-
selectors.collect{ |selector| "#{selector[:name]}/version/#{selector[:version]}" }
35+
selectors.collect{ |selector| ["pacticipant[]=#{encode_query_param(selector[:name])}&version[]=#{encode_query_param(selector[:version])}"] }.join("&")
3636
end
3737
end
3838
end

spec/lib/pact_broker/client/matrix_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
module PactBroker
44
module Client
55
describe Matrix do
6+
let(:matrix) { Matrix.new(base_url: 'http://example.org') }
7+
68
context "when the matrix resource is not found because the broker is the wrong version" do
7-
let(:matrix) { Matrix.new(base_url: 'http://example.org') }
89
let!(:request) { stub_request(:get, /matrix/).to_return(status: 404) }
910

1011
it "raises a helpful error" do

spec/pacts/pact_broker_client-pact_broker.json

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,49 @@
204204
"request": {
205205
"method": "get",
206206
"path": "/matrix",
207-
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F4.5.6"
207+
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
208+
},
209+
"response": {
210+
"status": 200,
211+
"headers": {
212+
},
213+
"body": {
214+
"matrix": {
215+
"json_class": "Pact::SomethingLike",
216+
"contents": [
217+
{
218+
"consumer": {
219+
"name": "Foo",
220+
"version": {
221+
"number": "4"
222+
}
223+
},
224+
"provider": {
225+
"name": "Bar",
226+
"version": {
227+
"number": "5"
228+
}
229+
},
230+
"verificationResult": {
231+
"verifiedAt": "2017-10-10T12:49:04+11:00",
232+
"success": true
233+
},
234+
"pact": {
235+
"createdAt": "2017-10-10T12:49:04+11:00"
236+
}
237+
}
238+
]
239+
}
240+
}
241+
}
242+
},
243+
{
244+
"description": "a request for the compatibility matrix for Foo version 1.2.3 and Bar version 4.5.6",
245+
"providerState": "the pact for Foo Thing version 1.2.3 has been verified by Bar version 4.5.6",
246+
"request": {
247+
"method": "get",
248+
"path": "/matrix",
249+
"query": "pacticipant[]=Foo+Thing&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
208250
},
209251
"response": {
210252
"status": 200,
@@ -246,7 +288,7 @@
246288
"request": {
247289
"method": "get",
248290
"path": "/matrix",
249-
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F9.9.9"
291+
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
250292
},
251293
"response": {
252294
"status": 400,
@@ -266,7 +308,7 @@
266308
"request": {
267309
"method": "get",
268310
"path": "/matrix",
269-
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F9.9.9"
311+
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
270312
},
271313
"response": {
272314
"status": 400,

spec/service_providers/pact_broker_client_matrix_spec.rb

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,30 @@ module PactBroker::Client
1919
with(
2020
method: :get,
2121
path: "/matrix",
22-
query: {
23-
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/4.5.6']
24-
}
22+
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
23+
).
24+
will_respond_with(
25+
status: 200,
26+
headers: pact_broker_response_headers,
27+
body: matrix_response_body
28+
)
29+
end
30+
31+
it 'a matrix of compatible versions' do
32+
matrix = pact_broker_client.matrix.get(selectors)
33+
expect(matrix.size).to eq 1
34+
end
35+
end
36+
37+
context "when the pacticipant name has a space in it" do
38+
before do
39+
pact_broker.
40+
given("the pact for Foo Thing version 1.2.3 has been verified by Bar version 4.5.6").
41+
upon_receiving("a request for the compatibility matrix for Foo version 1.2.3 and Bar version 4.5.6").
42+
with(
43+
method: :get,
44+
path: "/matrix",
45+
query: "pacticipant[]=Foo+Thing&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
2546
).
2647
will_respond_with(
2748
status: 200,
@@ -30,6 +51,8 @@ module PactBroker::Client
3051
)
3152
end
3253

54+
let(:selectors) { [{ name: "Foo Thing", version: "1.2.3" }, { name: "Bar", version: "4.5.6" }] }
55+
3356
it 'a matrix of compatible versions' do
3457
matrix = pact_broker_client.matrix.get(selectors)
3558
expect(matrix.size).to eq 1
@@ -64,9 +87,7 @@ module PactBroker::Client
6487
with(
6588
method: :get,
6689
path: "/matrix",
67-
query: {
68-
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9']
69-
}
90+
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
7091
).
7192
will_respond_with(
7293
status: 400,
@@ -93,9 +114,7 @@ module PactBroker::Client
93114
with(
94115
method: :get,
95116
path: "/matrix",
96-
query: {
97-
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9']
98-
}
117+
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
99118
).
100119
will_respond_with(
101120
status: 400,

0 commit comments

Comments
 (0)