Skip to content

Commit

Permalink
feat(matrix): allow success param to be specified in query params
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 28, 2017
1 parent b1f452a commit 2fb5817
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/pact_broker/api/resources/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Api
module Resources
class Matrix < BaseResource

def initialize
@selectors, @options = PactBroker::Matrix::ParseQuery.call(request.uri.query)
end

def content_types_provided
[["application/hal+json", :to_json]]
end
Expand All @@ -31,7 +35,11 @@ def to_json
end

def selectors
@selectors ||= PactBroker::Matrix::ParseQuery.call(request.uri.query)
@selectors
end

def options
@options
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ module Matrix
class ParseQuery
def self.call query
params = Rack::Utils.parse_nested_query(query)
(params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } }
selectors = (params['q'] || []).collect{ |i| { pacticipant_name: i['pacticipant'], pacticipant_version_number: i['version'] } }
options = {}
if params['success']
options[:success] = params['success'] == 'true'
end
return selectors, options
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/pact_broker/api/resources/matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Resources
before do
allow(PactBroker::Matrix::Service).to receive(:validate_selectors).and_return(error_messages)
allow(PactBroker::Matrix::Service).to receive(:find).and_return([])
allow(PactBroker::Matrix::ParseQuery).to receive(:call).and_return(selectors)
allow(PactBroker::Matrix::ParseQuery).to receive(:call).and_return([selectors, options])
end

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

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

Expand Down
22 changes: 17 additions & 5 deletions spec/lib/pact_broker/matrix/parse_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,47 @@ module PactBroker
module Matrix
describe ParseQuery do
describe ".call" do
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" }
let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9&success=true" }

subject { ParseQuery.call(query) }

it "extracts the pacticipant names and respective versions" do
expect(subject).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }])
expect(subject.first).to eq([{ pacticipant_name: "Foo", pacticipant_version_number: "1.2.3" }, { pacticipant_name: "Bar", pacticipant_version_number: "9.9.9" }])
end

it "extracts the options" do
expect(subject.last).to eq success: true
end

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

it "works" do
expect(subject).to eq [{pacticipant_name: "Name With Spaces", pacticipant_version_number: "1 2"}]
expect(subject.first).to eq [{pacticipant_name: "Name With Spaces", pacticipant_version_number: "1 2"}]
end
end

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

it "returns an empty hash" do
expect(subject).to eq([])
expect(subject.first).to eq([])
end
end

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

it "returns nil keys or values" do
expect(subject).to eq [{ pacticipant_name: nil, pacticipant_version_number: nil }]
expect(subject.first).to eq [{ pacticipant_name: nil, pacticipant_version_number: nil }]
end
end

context "with no options specified" do
let(:query) { "" }

it "does not set any options" do
expect(subject.last).to eq({})
end
end
end
Expand Down

0 comments on commit 2fb5817

Please sign in to comment.