Skip to content

Commit

Permalink
feat: change query params for matrix to use pacticipant[]=? and versi…
Browse files Browse the repository at this point in the history
…on[]=?
  • Loading branch information
bethesque committed Oct 27, 2017
1 parent 0dc86c2 commit 003e305
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
5 changes: 5 additions & 0 deletions lib/pact_broker/client/base_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'erb'
require 'httparty'
require 'pact_broker/client/error'
require 'cgi'

module PactBroker
module Client
Expand All @@ -11,6 +12,10 @@ module UrlHelpers
def encode_param param
ERB::Util.url_encode param
end

def encode_query_param param
CGI::escape param
end
end

module StringToSymbol
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/client/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module PactBroker
module Client
class Matrix < BaseClient
def get selectors
query = {selectors: convert_selector_hashes_to_params(selectors)}
query = convert_selector_hashes_to_params(selectors)
response = self.class.get("/matrix", query: query, headers: default_get_headers)
$stdout.puts("DEBUG: Response headers #{response.headers}") if verbose?
$stdout.puts("DEBUG: Response body #{response}") if verbose?
Expand Down Expand Up @@ -32,7 +32,7 @@ def handle_response response
end

def convert_selector_hashes_to_params(selectors)
selectors.collect{ |selector| "#{selector[:name]}/version/#{selector[:version]}" }
selectors.collect{ |selector| ["pacticipant[]=#{encode_query_param(selector[:name])}&version[]=#{encode_query_param(selector[:version])}"] }.join("&")
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/pact_broker/client/matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
module PactBroker
module Client
describe Matrix do
let(:matrix) { Matrix.new(base_url: 'http://example.org') }

context "when the matrix resource is not found because the broker is the wrong version" do
let(:matrix) { Matrix.new(base_url: 'http://example.org') }
let!(:request) { stub_request(:get, /matrix/).to_return(status: 404) }

it "raises a helpful error" do
Expand Down
48 changes: 45 additions & 3 deletions spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,49 @@
"request": {
"method": "get",
"path": "/matrix",
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F4.5.6"
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
},
"response": {
"status": 200,
"headers": {
},
"body": {
"matrix": {
"json_class": "Pact::SomethingLike",
"contents": [
{
"consumer": {
"name": "Foo",
"version": {
"number": "4"
}
},
"provider": {
"name": "Bar",
"version": {
"number": "5"
}
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
}
}
]
}
}
}
},
{
"description": "a request for the compatibility matrix for Foo version 1.2.3 and Bar version 4.5.6",
"providerState": "the pact for Foo Thing version 1.2.3 has been verified by Bar version 4.5.6",
"request": {
"method": "get",
"path": "/matrix",
"query": "pacticipant[]=Foo+Thing&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
},
"response": {
"status": 200,
Expand Down Expand Up @@ -246,7 +288,7 @@
"request": {
"method": "get",
"path": "/matrix",
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F9.9.9"
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
},
"response": {
"status": 400,
Expand All @@ -266,7 +308,7 @@
"request": {
"method": "get",
"path": "/matrix",
"query": "selectors[]=Foo%2Fversion%2F1.2.3&selectors[]=Bar%2Fversion%2F9.9.9"
"query": "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
},
"response": {
"status": 400,
Expand Down
37 changes: 28 additions & 9 deletions spec/service_providers/pact_broker_client_matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,30 @@ module PactBroker::Client
with(
method: :get,
path: "/matrix",
query: {
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/4.5.6']
}
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
).
will_respond_with(
status: 200,
headers: pact_broker_response_headers,
body: matrix_response_body
)
end

it 'a matrix of compatible versions' do
matrix = pact_broker_client.matrix.get(selectors)
expect(matrix.size).to eq 1
end
end

context "when the pacticipant name has a space in it" do
before do
pact_broker.
given("the pact for Foo Thing version 1.2.3 has been verified by Bar version 4.5.6").
upon_receiving("a request for the compatibility matrix for Foo version 1.2.3 and Bar version 4.5.6").
with(
method: :get,
path: "/matrix",
query: "pacticipant[]=Foo+Thing&version[]=1.2.3&pacticipant[]=Bar&version[]=4.5.6"
).
will_respond_with(
status: 200,
Expand All @@ -30,6 +51,8 @@ module PactBroker::Client
)
end

let(:selectors) { [{ name: "Foo Thing", version: "1.2.3" }, { name: "Bar", version: "4.5.6" }] }

it 'a matrix of compatible versions' do
matrix = pact_broker_client.matrix.get(selectors)
expect(matrix.size).to eq 1
Expand Down Expand Up @@ -64,9 +87,7 @@ module PactBroker::Client
with(
method: :get,
path: "/matrix",
query: {
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9']
}
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
).
will_respond_with(
status: 400,
Expand All @@ -93,9 +114,7 @@ module PactBroker::Client
with(
method: :get,
path: "/matrix",
query: {
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9']
}
query: "pacticipant[]=Foo&version[]=1.2.3&pacticipant[]=Bar&version[]=9.9.9"
).
will_respond_with(
status: 400,
Expand Down

0 comments on commit 003e305

Please sign in to comment.