-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support multi-line matching with plain text matcher #1579
- Loading branch information
Showing
5 changed files
with
177 additions
and
7 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/Defect1579Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package au.com.dius.pact.consumer.junit5; | ||
|
||
import au.com.dius.pact.consumer.MockServer; | ||
import au.com.dius.pact.consumer.dsl.PactDslRootValue; | ||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider; | ||
import au.com.dius.pact.core.model.PactSpecVersion; | ||
import au.com.dius.pact.core.model.RequestResponsePact; | ||
import au.com.dius.pact.core.model.annotations.Pact; | ||
import org.apache.commons.collections4.MapUtils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.hc.client5.http.fluent.Request; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonArrayMinLike; | ||
import static java.lang.String.format; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
@ExtendWith(PactConsumerTestExt.class) | ||
@PactTestFor(providerName = "TextProvider", pactVersion = PactSpecVersion.V3) | ||
public class Defect1579Test { | ||
@Pact(consumer = "TextConsumer") | ||
public RequestResponsePact articles(PactDslWithProvider builder) { | ||
return builder | ||
.given("A text generation job finished successfully") | ||
.uponReceiving("A request to download text") | ||
.pathFromProviderState("/textresult/${jobId}", "/textresult/dummyJobId") | ||
.method("GET") | ||
.willRespondWith() | ||
.status(200) | ||
.headers(Map.of("Content-Type", "text/plain")) | ||
.body(PactDslRootValue.stringMatcher("^.+$", "whatever")) | ||
.toPact(); | ||
} | ||
|
||
@Test | ||
@PactTestFor | ||
void testApi(MockServer mockServer) throws IOException { | ||
String response = Request.get(mockServer.getUrl() + "/textresult/dummyJobId") | ||
.execute().returnContent().asString(); | ||
assertThat(response, is(equalTo("whatever"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ider/junit5/src/test/groovy/au/com/dius/pact/provider/junit5/TextPlainProviderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package au.com.dius.pact.provider.junit5 | ||
|
||
import au.com.dius.pact.provider.junitsupport.Provider | ||
import au.com.dius.pact.provider.junitsupport.State | ||
import au.com.dius.pact.provider.junitsupport.loader.PactFolder | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import groovy.util.logging.Slf4j | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.TestTemplate | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import ru.lanwen.wiremock.ext.WiremockResolver | ||
import ru.lanwen.wiremock.ext.WiremockUriResolver | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.* | ||
|
||
@Provider('TextProvider') | ||
@PactFolder('pacts') | ||
@ExtendWith([ | ||
WiremockResolver, | ||
WiremockUriResolver | ||
]) | ||
@Slf4j | ||
class TextPlainProviderTest { | ||
|
||
@TestTemplate | ||
@ExtendWith(PactVerificationInvocationContextProvider) | ||
void testTemplate(PactVerificationContext context) { | ||
context.verifyInteraction() | ||
} | ||
|
||
@BeforeEach | ||
void before(PactVerificationContext context, @WiremockResolver.Wiremock WireMockServer server, | ||
@WiremockUriResolver.WiremockUri String uri) throws MalformedURLException { | ||
context.setTarget(HttpTestTarget.fromUrl(new URL(uri))) | ||
|
||
server.stubFor( | ||
get(urlPathEqualTo('/textresult/12345')) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withHeader('Content-Type', 'text/plain') | ||
.withBody('Hello \r\n World')) | ||
) | ||
} | ||
|
||
@State('A text generation job finished successfully') | ||
Map<String, Object> jobFinishedState() { | ||
[jobId: '12345'] | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
provider/junit5/src/test/resources/pacts/TextConsumer-TextProvider.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"consumer": { | ||
"name": "TextConsumer" | ||
}, | ||
"interactions": [ | ||
{ | ||
"description": "A request to download text", | ||
"providerStates": [ | ||
{ | ||
"name": "A text generation job finished successfully" | ||
} | ||
], | ||
"request": { | ||
"generators": { | ||
"path": { | ||
"dataType": "STRING", | ||
"expression": "/textresult/${jobId}", | ||
"type": "ProviderState" | ||
} | ||
}, | ||
"method": "GET", | ||
"path": "/textresult/dummyJobId" | ||
}, | ||
"response": { | ||
"body": "whatever", | ||
"headers": { | ||
"Content-Type": "text/plain" | ||
}, | ||
"matchingRules": { | ||
"body": { | ||
"$": { | ||
"combine": "AND", | ||
"matchers": [ | ||
{ | ||
"match": "regex", | ||
"regex": "^.+$" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"status": 200 | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"pact-jvm": { | ||
"version": "4.3.12" | ||
}, | ||
"pactSpecification": { | ||
"version": "3.0.0" | ||
} | ||
}, | ||
"provider": { | ||
"name": "TextProvider" | ||
} | ||
} |