pact-foundation / pact-net

.NET version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://pact.io
MIT License
823 stars 225 forks source link

Requesting enhancement to update query param and body param in post call in provider state before pact verification #385

Open tl-madhulika-mitra opened 2 years ago

tl-madhulika-mitra commented 2 years ago

Hi , I found a post for JVM https://pactflow.io/blog/injecting-values-from-provider-states/ where we can modify the request before the validation, but is there a mechanism in C#?

mefellows commented 2 years ago

I don't believe so.

You could achieve this by adding some custom middleware to your test application that can transform the incoming request.

tl-madhulika-mitra commented 2 years ago

Thank you @mefellows , @adamrodger- Could you let me know if we plan to add this for the upcoming versions, as currently we are adding some heavy middleware to support our chained requests.

adamrodger commented 2 years ago

I don't fully understand the feature request if I'm honest.

It would be really helpful if you could create a proposal for the API changes with an explanation of what the changes do, and then we can discuss about feasibility/design.

For example, see issue #392 for a good format for proposing changes.

mefellows commented 2 years ago

to support our chained requests.

I'd be keen to understand this use case also. "chained requests" is an anti-pattern as far as contract testing is concerned, each request/response should be able to be tested in isolation.

Whilst the feature of essentially proxying incoming/outgoing bodies is doable, I'd be wary of adding features that readily enables improper use of the tool that will lead to future challenges for all parties.

adamrodger commented 8 months ago

Closed due to inactivity

Dreamescaper commented 12 hours ago

Please consider reopening this issue. The problem is described in the blog post: https://pactflow.io/blog/injecting-values-from-provider-states/ .

Assume I have the following setup:

        _pactBuilder
            .UponReceiving("A POST request to process the entity")
                .Given("An entity {id} is created", new Dictionary<string, string> { ["id"] = entityId })
                .WithRequest(HttpMethod.Post, $"/entities/{entityId}/process")
                    .WithHeader("X-Api-Key", ApiKey)
            .WillRespond()
                .WithStatus(HttpStatusCode.OK)
                .WithJsonBody(new
                {
                    Result = Match.Regex("123456789012", "\\w{13}"),
                });

The problem is that the entityId is auto-generated, I cannot easily force it via Given step.

Therefore, it would be nice to be able to return that auto-generated value from Given state, and make it accessible in VerifyAsync, e.g.

        await _pactBuilder.VerifyAsync(async ctx =>
        {
              // Injected from provider state
              var entityId = ctx.FromProviderState("entityId");
              await ProcessEntity(entityId);
        });