Feature Request - Automatically replace path with provider state when using `pathFromProviderState`
#441 opened on Jun 13, 2024
Repository metrics
- Stars
- (103 stars)
- PR merge metrics
- (PR metrics pending)
Description
Expectation: When using pathFromProviderState in a consumer pact test, I expect that the path param in the created pact is replaced by the supplied provider state. That is, when verifying a consumer pact using the standalone pact-verifier (version 1.1.1), I would expect the request that is sent to contain the supplied provider state, but instead the request path of the “example” parameter in the pathFromProviderState property is used.
Consider the following example:
@Pact(provider = "SomeProvider", consumer = "SomeConsumer")
public RequestResponsePact samplePact(PactDslWithProvider builder) {
var responseHeaders = new HashMap<String, String>();
responseHeaders.put("Content-Type", "application/json");
PactDslJsonBody responseBody = new PactDslJsonBody()
.stringValue("name", "John");
return builder
.given("User exists", "name", "John")
.uponReceiving("Hello John")
.pathFromProviderState("/api/hello/{name}", "/api/hello/James")
.method("GET")
.willRespondWith()
.status(200)
.headers(responseHeaders)
.body(responseBody)
.toPact();
}
@Test
@PactTestFor(providerName = "SomeProvider", pactMethod = "samplePact")
public void testForSamplePact(MockServer mockServer) throws ExecutionException, InterruptedException {
HelloApi.HelloName request = HelloApi.HelloName
.builder()
.name("James")
.build();
testClient.apply(request).get().getResponse();
}
Executing this test creates below pact.
{
"consumer": {
"name": "SomeConsumer"
},
"interactions": [
{
"description": "Hello John",
"providerStates": [
{
"name": "User exists",
"params": {
"name": "John"
}
}
],
"request": {
"generators": {
"path": {
"dataType": "STRING",
"expression": "/api/hello/{name}",
"type": "ProviderState"
}
},
"method": "GET",
"path": "/api/hello/James"
},
"response": {
"body": {
"name": "John"
},
"headers": {
"Content-Type": "application/json"
},
"status": 200
}
}
],
"metadata": {
"pact-jvm": {
"version": "4.6.7"
},
"pactSpecification": {
"version": "3.0.0"
}
},
"provider": {
"name": "SomeProvider"
}
}
When using the pact-verifier the request that is sent is always equal to the path in the pact file (i.e. /api/hello/James) . I would expect/hope that the supplied provider state would actually be applied to the request when the pact-verifier sends the request. In this case the request would then be /api/hello/John. In theory, I could obtain the provider state from the request made to the —state-change-url. This way I could replace the path of the request with the provider state, but I would expect that that would happen out of the box. Is it a deliberate choice that this doesn’t happen? If not, could this be added to the pact-verifier in a future version?