pact-net icon indicating copy to clipboard operation
pact-net copied to clipboard

WithCustomHeader Function does not replace existing headers

Open RChild0916 opened this issue 1 year ago • 11 comments

Hello all!

I'm working with pact-net as a provider using pactflow which has some bi-directional testing, and ran into an interesting issue. We recently added authentication to our API, and needed to add an auth header using the .WithCustomHeader method like so:

`

        IPactVerifier pactVerifier = new PactVerifier(config);
        pactVerifier
            .ServiceProvider("App.Api", new Uri(_baseUrl))
            .WithPactBrokerSource(
                new Uri(_providerUrl),
                options => options
                    .TokenAuthentication(_pactflowToken)
                    .PublishResults(
                        _providerVersion,
                        options => options.ProviderTags(_providerTags)
                            .ProviderBranch(_providerBranch)))
            .WithProviderStateUrl(new Uri(_fixture.ServerUri, "/provider-states"))
            .WithCustomHeader("Authorization", $"Bearer {tokenValue}")
            .Verify();

`

The "WithCustomHeader" function seems to have an issue, which I haven't been able to discern exactly but believe it is one of two things:

  1. If a header already exists within the consumer pact json, it does not replace the value as it should.
  2. It is only adding an additional header, and in this case, we end up with 2 "Authorization" headers Either way, the end result is the same in that our API grabs the old token that exists within the pact file and attempts to use it, sees it is expired and throws a 401.

To get this working, we removed the Authorization header from the pact file and everything ran clean from the provider side, but this lead to a new issue within pactflow. Pactflow will check the request headers against our API's OAS, and sees that the authorization header is missing and fails for the consumer side check.

Is it possible to get new functionality to allow users to replace existing headers?

RChild0916 avatar May 10 '23 14:05 RChild0916