diff --git a/lib/pact_broker/client/base_client.rb b/lib/pact_broker/client/base_client.rb index e64ecc00..48b82513 100644 --- a/lib/pact_broker/client/base_client.rb +++ b/lib/pact_broker/client/base_client.rb @@ -1,6 +1,7 @@ require 'erb' require 'httparty' require 'pact_broker/client/error' +require 'cgi' module PactBroker module Client @@ -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 diff --git a/lib/pact_broker/client/matrix.rb b/lib/pact_broker/client/matrix.rb index 3e739119..7d3337a6 100644 --- a/lib/pact_broker/client/matrix.rb +++ b/lib/pact_broker/client/matrix.rb @@ -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? @@ -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 diff --git a/spec/lib/pact_broker/client/matrix_spec.rb b/spec/lib/pact_broker/client/matrix_spec.rb index 9cbcef61..73b6ac71 100644 --- a/spec/lib/pact_broker/client/matrix_spec.rb +++ b/spec/lib/pact_broker/client/matrix_spec.rb @@ -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 diff --git a/spec/pacts/pact_broker_client-pact_broker.json b/spec/pacts/pact_broker_client-pact_broker.json index 69569a64..ae86b459 100644 --- a/spec/pacts/pact_broker_client-pact_broker.json +++ b/spec/pacts/pact_broker_client-pact_broker.json @@ -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, @@ -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, @@ -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, diff --git a/spec/service_providers/pact_broker_client_matrix_spec.rb b/spec/service_providers/pact_broker_client_matrix_spec.rb index a08af701..15c69729 100644 --- a/spec/service_providers/pact_broker_client_matrix_spec.rb +++ b/spec/service_providers/pact_broker_client_matrix_spec.rb @@ -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, @@ -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 @@ -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, @@ -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,