Skip to content

Commit

Permalink
fix: use the raw URL path in the mock server as HttpExchange will dec…
Browse files Browse the repository at this point in the history
…ode the path #1326
  • Loading branch information
Ronald Holshausen committed Mar 27, 2021
1 parent 4f06881 commit 08d6518
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class QueryParameterEncodingTest extends ConsumerPactTest {
protected RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.uponReceiving("java test interaction with a query string")
.path("/some path")
.path("/some_path")
.method("GET")
.query("datetime=2011-12-03T10:15:30+01:00")
.willRespondWith()
Expand All @@ -35,6 +35,6 @@ protected String consumerName() {

@Override
protected void runTest(MockServer mockServer, PactTestExecutionContext context) throws IOException {
new ConsumerClient(mockServer.getUrl()).getAsMap("/some path", "datetime=2011-12-03T10:15:30+01:00");
new ConsumerClient(mockServer.getUrl()).getAsMap("/some_path", "datetime=2011-12-03T10:15:30+01:00");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class QueryParameterMatchingTest extends ConsumerPactTest {
protected RequestResponsePact createPact(PactDslWithProvider builder) {
return builder
.uponReceiving("java test interaction with a query string matcher")
.path("/some path")
.path("/some/path")
.method("GET")
.queryMatchingDate("date", "yyyy-MM-dd", "2011-12-03")
.queryMatchingDate("date2", "yyyy-MM-dd")
Expand All @@ -40,7 +40,7 @@ protected String consumerName() {

@Override
protected void runTest(MockServer mockServer, PactTestExecutionContext context) throws IOException {
new ConsumerClient(mockServer.getUrl()).getAsMap("/some path",
new ConsumerClient(mockServer.getUrl()).getAsMap("/some/path",
"date=2011-12-03&date2=2012-09-13&time=10:05:22&datetime=2019-01-23 16:09:33" +
"&isodate=2011-12-03&isotime=10:05:22&isodatetime=2019-01-23T16:09:33+11:00");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package au.com.dius.pact.consumer.junit5

import au.com.dius.pact.consumer.MockServer
import au.com.dius.pact.consumer.dsl.PactDslWithProvider
import au.com.dius.pact.core.model.RequestResponsePact
import au.com.dius.pact.core.model.annotations.Pact
import org.apache.http.HttpResponse
import org.apache.http.client.fluent.Request
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(PactConsumerTestExt)
@PactTestFor(providerName = 'PathWithValueWithSlashTest', port = '1234')
class PathWithValueWithSlashTest {
@Pact(consumer = 'Consumer')
RequestResponsePact filesPact(PactDslWithProvider builder) {
builder
.uponReceiving('a request with a slash in the path')
.path('/endpoint/Some%2FValue')
.willRespondWith()
.status(200)
.toPact()
}

@Test
void testFiles(MockServer mockServer) {
HttpResponse httpResponse = Request.Get("${mockServer.url}/endpoint/Some%2FValue")
.execute().returnResponse()
assert httpResponse.statusLine.statusCode == 200
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ abstract class BaseJdkMockServer(
exchange.close()
}

private fun toPactRequest(exchange: HttpExchange): Request {
fun toPactRequest(exchange: HttpExchange): Request {
val headers = exchange.requestHeaders
val contentType = contentType(headers)
val bodyContents = when (bodyIsCompressed(headers.getFirst("Content-Encoding"))) {
Expand All @@ -270,7 +270,7 @@ abstract class BaseJdkMockServer(
} else {
OptionalBody.body(bodyContents, contentType)
}
return Request(exchange.requestMethod, exchange.requestURI.path,
return Request(exchange.requestMethod, exchange.requestURI.rawPath,
queryStringToMap(exchange.requestURI.rawQuery).toMutableMap(), headers.toMutableMap(), body)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import au.com.dius.pact.core.model.Consumer
import au.com.dius.pact.consumer.model.MockProviderConfig
import au.com.dius.pact.core.model.Provider
import au.com.dius.pact.core.model.RequestResponsePact
import com.sun.net.httpserver.HttpExchange
import spock.lang.IgnoreIf
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.Unroll
Expand Down Expand Up @@ -45,7 +47,7 @@ class MockHttpServerSpec extends Specification {
}

@Timeout(60)
@IgnoreIf({ System.env.TRAVIS != 'true' })
@IgnoreIf({ System.env.CI != 'true' })
def 'handle more than 200 tests'() {
given:
def pact = new RequestResponsePact(new Provider(), new Consumer(), [])
Expand All @@ -61,4 +63,22 @@ class MockHttpServerSpec extends Specification {
true
}

@Issue('#1326')
def 'use the raw path when creating the Pact request'() {
given:
def mockServer = new MockHttpServer(new RequestResponsePact(new Provider(), new Consumer(), []),
MockProviderConfig.createDefault())
def exchange = Mock(HttpExchange) {
getRequestHeaders() >> new com.sun.net.httpserver.Headers()
getRequestURI() >> new URI('http://localhost/endpoint/Some%2FValue')
getRequestBody() >> new ByteArrayInputStream([] as byte[])
getRequestMethod() >> 'GET'
}

when:
def request = mockServer.toPactRequest(exchange)

then:
request.path == '/endpoint/Some%2FValue'
}
}
28 changes: 14 additions & 14 deletions pact-publish/src/test/groovy/broker/PactBrokerClientPactSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PactBrokerClientPactSpec extends Specification {
}
uponReceiving('a pact publish request')
withAttributes(method: 'PUT',
path: '/pacts/provider/Provider/consumer/Foo Consumer/version/10.0.0/A',
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/version/10.0.0%2FA',
body: pactContents
)
willRespondWith(status: 200)
Expand Down Expand Up @@ -99,7 +99,7 @@ class PactBrokerClientPactSpec extends Specification {
}
uponReceiving('a tag create request')
withAttributes(method: 'PUT',
path: '/pacticipants/Foo Consumer/versions/10.0.0/tags/A',
path: '/pacticipants/Foo%20Consumer/versions/10.0.0/tags/A',
body: '{}'
)
willRespondWith(status: 201)
Expand Down Expand Up @@ -131,7 +131,7 @@ class PactBrokerClientPactSpec extends Specification {
}
uponReceiving('a tag create request')
withAttributes(method: 'PUT',
path: '/pacticipants/Foo Consumer/versions/10.0.0/tags/A',
path: '/pacticipants/Foo%20Consumer/versions/10.0.0/tags/A',
body: '{}'
)
willRespondWith(status: 500)
Expand Down Expand Up @@ -163,7 +163,7 @@ class PactBrokerClientPactSpec extends Specification {
}
uponReceiving('a pact publish request which will be forbidden')
withAttributes(method: 'PUT',
path: '/pacts/provider/Provider/consumer/Foo Consumer/version/10.0.0',
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/version/10.0.0',
body: pactContents
)
willRespondWith(status: 401, headers: [
Expand Down Expand Up @@ -201,7 +201,7 @@ class PactBrokerClientPactSpec extends Specification {
given('No pact has been published between the Provider and Foo Consumer')
uponReceiving('a pact publish request with invalid version')
withAttributes(method: 'PUT',
path: '/pacts/provider/Provider/consumer/Foo Consumer/version/XXXX',
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/version/XXXX',
body: pactContents
)
willRespondWith(status: 400, headers: ['Content-Type': 'application/json;charset=utf-8'], body: body)
Expand Down Expand Up @@ -245,7 +245,7 @@ class PactBrokerClientPactSpec extends Specification {
given('No pact has been published between the Provider and Foo Consumer and there is a similar consumer')
uponReceiving('a pact publish request')
withAttributes(method: 'PUT',
path: '/pacts/provider/Provider/consumer/Foo Consumer/version/10.0.0',
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/version/10.0.0',
body: pactContents
)
willRespondWith(status: 409, headers: ['Content-Type': 'text/plain'], body: body)
Expand Down Expand Up @@ -281,7 +281,7 @@ class PactBrokerClientPactSpec extends Specification {
given('Non-JSON response')
uponReceiving('a pact publish request')
withAttributes(method: 'PUT',
path: '/pacts/provider/Provider/consumer/Foo Consumer/version/10.0.0',
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/version/10.0.0',
body: pactContents
)
willRespondWith(status: 400, headers: ['Content-Type': 'text/plain'],
Expand Down Expand Up @@ -319,7 +319,7 @@ class PactBrokerClientPactSpec extends Specification {
}
}
uponReceiving('a request for the provider pacts')
withAttributes(path: '/pacts/provider/Activity Service/latest')
withAttributes(path: '/pacts/provider/Activity%20Service/latest')
willRespondWith(status: 200)
withBody(contentType: 'application/hal+json') {
'_links' {
Expand Down Expand Up @@ -353,7 +353,7 @@ class PactBrokerClientPactSpec extends Specification {
given('A pact has been published between the Provider and Foo Consumer')
uponReceiving('a pact publish verification request')
withAttributes(method: 'POST',
path: '/pacts/provider/Provider/consumer/Foo Consumer/pact-version/1234567890/verification-results'
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/pact-version/1234567890/verification-results'
)
withBody {
success true
Expand Down Expand Up @@ -386,7 +386,7 @@ class PactBrokerClientPactSpec extends Specification {
given('A pact has been published between the Provider and Foo Consumer')
uponReceiving('a pact publish verification request with build info')
withAttributes(method: 'POST',
path: '/pacts/provider/Provider/consumer/Foo Consumer/pact-version/1234567890/verification-results'
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/pact-version/1234567890/verification-results'
)
withBody {
success true
Expand Down Expand Up @@ -420,7 +420,7 @@ class PactBrokerClientPactSpec extends Specification {
given('A pact has been published between the Provider and Foo Consumer')
uponReceiving('a pact publish verification request with failure info')
withAttributes(method: 'POST',
path: '/pacts/provider/Provider/consumer/Foo Consumer/pact-version/1234567890/verification-results')
path: '/pacts/provider/Provider/consumer/Foo%20Consumer/pact-version/1234567890/verification-results')
withBody(mimeType: 'application/json') {
success false
providerApplicationVersion '10.0.0'
Expand Down Expand Up @@ -582,7 +582,7 @@ class PactBrokerClientPactSpec extends Specification {
consumer2: 'Foo Web Client 2'
])
uponReceiving('a request for the provider pacts')
withAttributes(method: 'POST', path: '/pacts/provider/Activity Service/for-verification')
withAttributes(method: 'POST', path: '/pacts/provider/Activity%20Service/for-verification')
withBody(contentType: 'application/hal+json') {
consumerVersionSelectors([
{
Expand Down Expand Up @@ -681,7 +681,7 @@ class PactBrokerClientPactSpec extends Specification {
consumer2: 'Foo Web Client 2'
])
uponReceiving('a request for the provider pacts')
withAttributes(method: 'POST', path: '/pacts/provider/Activity Service/for-verification')
withAttributes(method: 'POST', path: '/pacts/provider/Activity%20Service/for-verification')
withBody(contentType: 'application/hal+json') {
consumerVersionSelectors([
{
Expand Down Expand Up @@ -794,7 +794,7 @@ class PactBrokerClientPactSpec extends Specification {
consumer2: 'Foo Web Client 2'
])
uponReceiving('a request for the provider pacts')
withAttributes(method: 'POST', path: '/pacts/provider/Activity Service/for-verification')
withAttributes(method: 'POST', path: '/pacts/provider/Activity%20Service/for-verification')
withBody(contentType: 'application/hal+json') {
consumerVersionSelectors([
{
Expand Down

0 comments on commit 08d6518

Please sign in to comment.