Skip to content

Commit 2fb5817

Browse files
committed
feat(matrix): allow success param to be specified in query params
1 parent b1f452a commit 2fb5817

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

lib/pact_broker/api/resources/matrix.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module Api
77
module Resources
88
class Matrix < BaseResource
99

10+
def initialize
11+
@selectors, @options = PactBroker::Matrix::ParseQuery.call(request.uri.query)
12+
end
13+
1014
def content_types_provided
1115
[["application/hal+json", :to_json]]
1216
end
@@ -31,7 +35,11 @@ def to_json
3135
end
3236

3337
def selectors
34-
@selectors ||= PactBroker::Matrix::ParseQuery.call(request.uri.query)
38+
@selectors
39+
end
40+
41+
def options
42+
@options
3543
end
3644
end
3745
end

lib/pact_broker/matrix/parse_query.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ module Matrix
55
class ParseQuery
66
def self.call query
77
params = Rack::Utils.parse_nested_query(query)
8-
(params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } }
8+
selectors = (params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } }
9+
options = {}
10+
if params['success']
11+
options[:success] = params['success'] == 'true'
12+
end
13+
return selectors, options
914
end
1015
end
1116
end

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Resources
88
before do
99
allow(PactBroker::Matrix::Service).to receive(:validate_selectors).and_return(error_messages)
1010
allow(PactBroker::Matrix::Service).to receive(:find).and_return([])
11-
allow(PactBroker::Matrix::ParseQuery).to receive(:call).and_return(selectors)
11+
allow(PactBroker::Matrix::ParseQuery).to receive(:call).and_return([selectors, options])
1212
end
1313

1414
let(:td) { TestDataBuilder.new }
@@ -17,6 +17,7 @@ module Resources
1717
let(:params) { {q: [{pacticipant: 'Foo', version: '1'}, {pacticipant: 'Bar', version: '2'}]} }
1818
let(:error_messages) { [] }
1919
let(:selectors) { double('selectors') }
20+
let(:options) { double('options') }
2021

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

spec/lib/pact_broker/matrix/parse_query_spec.rb

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

99
subject { ParseQuery.call(query) }
1010

1111
it "extracts the pacticipant names and respective versions" do
12-
expect(subject).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }])
12+
expect(subject.first).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }])
13+
end
14+
15+
it "extracts the options" do
16+
expect(subject.last).to eq success: true
1317
end
1418

1519
context "with spaces" do
1620
let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" }
1721

1822
it "works" do
19-
expect(subject).to eq [{pacticipant_name: "Name With Spaces", pacticipant_version_number: "1 2"}]
23+
expect(subject.first).to eq [{pacticipant_name: "Name With Spaces", pacticipant_version_number: "1 2"}]
2024
end
2125
end
2226

2327
context "with no q" do
2428
let(:query) { "foo" }
2529

2630
it "returns an empty hash" do
27-
expect(subject).to eq([])
31+
expect(subject.first).to eq([])
2832
end
2933
end
3034

3135
context "with an incorrect param names" do
3236
let(:query) { "q[][wrong]=Foo&q[][blah]=1.2.3" }
3337

3438
it "returns nil keys or values" do
35-
expect(subject).to eq [{ pacticipant_name: nil, pacticipant_version_number: nil }]
39+
expect(subject.first).to eq [{ pacticipant_name: nil, pacticipant_version_number: nil }]
40+
end
41+
end
42+
43+
context "with no options specified" do
44+
let(:query) { "" }
45+
46+
it "does not set any options" do
47+
expect(subject.last).to eq({})
3648
end
3749
end
3850
end

0 commit comments

Comments
 (0)