Skip to content

Commit

Permalink
fix: strip queryparams from path and get them handled separately (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlafourc authored Sep 16, 2024
1 parent 3a3e030 commit 2144113
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import au.com.dius.pact.core.model.V4Interaction.SynchronousHttp;
import au.com.dius.pact.core.model.V4Pact;
import com.github.tomakehurst.wiremock.common.Metadata;
import com.github.tomakehurst.wiremock.common.Urls;
import com.github.tomakehurst.wiremock.http.HttpHeader;
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.LoggedResponse;
Expand Down Expand Up @@ -88,7 +89,12 @@ public void saveAll() {

final IRequest pactRequest = asSynchronousRequestResponse.getRequest();
pactRequest.setMethod(wireMockRequest.getMethod().getName());
pactRequest.setPath(wireMockRequest.getUrl());
pactRequest.setPath(Urls.getPath(wireMockRequest.getUrl()));
pactRequest
.getQuery()
.putAll(
Urls.splitQueryFromUrl(wireMockRequest.getUrl()).entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().values())));
pactRequest.setBody(new OptionalBody(State.PRESENT, wireMockRequest.getBody()));
for (final HttpHeader header :
Optional.ofNullable(wireMockRequest.getHeaders()).orElse(new HttpHeaders()).all()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,12 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "GET",
"path": "/getrequest_1?a=b"
"path": "/getrequest_1",
"query": {
"a": [
"b"
]
}
},
"response": {
"body": {
Expand All @@ -291,7 +296,13 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "GET",
"path": "/getrequest_2?a=b&a=b"
"path": "/getrequest_2",
"query": {
"a": [
"b",
"b"
]
}
},
"response": {
"body": {
Expand All @@ -318,8 +329,19 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "GET",
"path": "/getrequest_3?a=b&c=d&e=f"
},
"path": "/getrequest_3",
"query": {
"a": [
"b"
],
"c": [
"d"
],
"e": [
"f"
]
}
},
"response": {
"body": {
"content": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "POST",
"path": "/postrequest?a=b"
"path": "/postrequest",
"query": {
"a": [
"b"
]
}
},
"response": {
"body": {
Expand Down Expand Up @@ -372,7 +377,13 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "POST",
"path": "/postrequest?a=b&a=b"
"path": "/postrequest",
"query": {
"a": [
"b",
"b"
]
}
},
"response": {
"body": {
Expand Down Expand Up @@ -409,7 +420,18 @@ public void testThatSingleGetRequestCanGeneratePact_3_invocations_query() {
]
},
"method": "POST",
"path": "/postrequest?a=b&c=d&e=f"
"path": "/postrequest",
"query": {
"a": [
"b"
],
"c": [
"d"
],
"e": [
"f"
]
}
},
"response": {
"body": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package se.bjurr.wiremockpact.wiremockpact.test;

import com.github.tomakehurst.wiremock.client.WireMock;
import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import se.bjurr.wiremockpact.wiremockpact.testutils.BaseTest;

public class QueryParamsTest extends BaseTest {

@BeforeEach
public void beforeEach() {
WireMock.reset();
WireMock.stubFor(WireMock.any(WireMock.anyUrl()).willReturn(WireMock.ok()));
}

@Test
public void testThatSingleHeaderIsRecorded() {
RestAssured.given()
.queryParam("foo", "bar")
.log()
.all()
.when()
.put("/therequest")
.then()
.statusCode(200);

this.assertPactEquals(
"""
{
"consumer": {
"name": "this-is-me"
},
"interactions": [
{
"description": "PUT /therequest?foo=bar -> 200",
"key": "a08c5440",
"pending": false,
"request": {
"body": {
"content": ""
},
"headers": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"gzip,deflate"
],
"Content-Length": [
"0"
]
},
"method": "PUT",
"path": "/therequest",
"query": {
"foo": [
"bar"
]
}
},
"response": {
"body": {
"content": ""
},
"status": 200
},
"type": "Synchronous/HTTP"
}
],
"metadata": {
"pact-jvm": {
"version": "4.6.9"
},
"pactSpecification": {
"version": "4.0"
}
},
"provider": {
"name": "this-is-you"
}
}
""");
}
}

0 comments on commit 2144113

Please sign in to comment.