Skip to content

Commit e3913f7

Browse files
committed
feat(matrix): change query params for matrix to use q[][pacticipant]=? and q[][version]=?
1 parent 41c20c5 commit e3913f7

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

lib/pact_broker/matrix/parse_query.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
require 'cgi'
1+
require 'rack/utils'
22

33
module PactBroker
44
module Matrix
55
class ParseQuery
66
def self.call query
7-
params = CGI.parse(CGI.unescape(query))
8-
params['pacticipant[]'].zip(params['version[]']).each_with_object({}) do | (pacticipant, version), hash |
9-
hash[pacticipant] = version
7+
params = Rack::Utils.parse_nested_query(query)
8+
(params['q'] || []).each_with_object({}) do | selector, hash |
9+
hash[selector['pacticipant']] = selector['version']
1010
end
1111
end
1212
end

lib/pact_broker/matrix/service.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ def find_compatible_pacticipant_versions criteria
2323
def validate_selectors selectors
2424
error_messages = []
2525

26-
selectors.keys.each do | pacticipant_name |
26+
selectors.each do | pacticipant_name, version |
27+
if pacticipant_name.nil? && version.nil?
28+
error_messages << "Please specify the pacticipant name and version"
29+
elsif pacticipant_name.nil?
30+
error_messages << "Please specify the pacticipant name"
31+
elsif version.nil?
32+
error_messages << "Please specify the version for #{pacticipant_name}"
33+
end
34+
end
35+
36+
if selectors.values.any?(&:nil?)
37+
error_messages << "Please specify the pacticipant version"
38+
end
39+
40+
selectors.keys.compact.each do | pacticipant_name |
2741
unless pacticipant_service.find_pacticipant_by_name(pacticipant_name)
2842
error_messages << "Pacticipant '#{pacticipant_name}' not found"
2943
end

spec/features/get_matrix_spec.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
let(:path) { "/matrix" }
1111
let(:params) do
1212
{
13-
pacticipant: ['Consumer', 'Provider'],
14-
version: ['1.0.0', '4.5.6']
13+
q: [
14+
{ pacticipant: 'Consumer', version: '1.0.0' },
15+
{ pacticipant: 'Provider', version: '4.5.6' }
16+
]
1517
}
1618
end
1719
let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) }

spec/lib/pact_broker/api/resources/matrix_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Resources
1212
let(:td) { TestDataBuilder.new }
1313
let(:path) { "/matrix" }
1414
let(:json_response_body) { JSON.parse(subject.body, symbolize_names: true) }
15-
let(:params) { { pacticipant: ['Foo', 'Bar'], version: ['1', '2'] } }
15+
let(:params) { {q: [{pacticipant: 'Foo', version: '1'}, {pacticipant: 'Bar', version: '2'}]} }
1616
let(:error_messages) { [] }
1717

1818
subject { get path, params, {'Content-Type' => 'application/hal+json'}; last_response }

spec/lib/pact_broker/matrix/parse_query_spec.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module PactBroker
44
module Matrix
55
describe ParseQuery do
66
describe ".call" do
7-
let(:query) { "pacticipant[]=Foo&pacticipant[]=Bar&version[]=1.2.3&version[]=9.9.9" }
7+
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" }
88

99
subject { ParseQuery.call(query) }
1010

@@ -13,12 +13,28 @@ module Matrix
1313
end
1414

1515
context "with spaces" do
16-
let(:query) { "pacticipant%5B%5D=Name%20With%20Spaces&version%5B%5D=1%202" }
16+
let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" }
1717

1818
it "works" do
1919
expect(subject).to eq "Name With Spaces" => "1 2"
2020
end
2121
end
22+
23+
context "with no q" do
24+
let(:query) { "foo" }
25+
26+
it "returns an empty hash" do
27+
expect(subject).to eq({})
28+
end
29+
end
30+
31+
context "with an incorrect param names" do
32+
let(:query) { "q[][wrong]=Foo&q[][blah]=1.2.3" }
33+
34+
it "returns nil keys or values" do
35+
expect(subject).to eq nil => nil
36+
end
37+
end
2238
end
2339
end
2440
end

spec/lib/pact_broker/matrix/service_spec.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,31 @@ module Matrix
4646
expect(subject.first).to eq "Pacticipant 'Foo' not found"
4747
end
4848
end
49+
50+
context "when the pacticipant name is not specified" do
51+
let(:selectors) { {nil => "1"} }
52+
53+
it "returns error messages" do
54+
expect(subject.first).to eq "Please specify the pacticipant name"
55+
end
56+
end
57+
58+
context "when the pacticipant version is not specified" do
59+
let(:selectors) { {'Foo' => nil} }
60+
61+
it "returns error messages" do
62+
expect(subject.first).to eq "Please specify the version for Foo"
63+
end
64+
end
65+
66+
context "when the pacticipant name and version are not specified" do
67+
let(:selectors) { {nil => nil} }
68+
69+
it "returns error messages" do
70+
expect(subject.first).to eq "Please specify the pacticipant name and version"
71+
end
72+
end
4973
end
5074
end
5175
end
5276
end
53-

0 commit comments

Comments
 (0)