Hi Team, We are using HttpPost method to fetch dat...
# pact-net
p
Hi Team, We are using HttpPost method to fetch data, When I write Pact test for the same it fails. But if I change the method to HTTPGet then the pact test passes. Is there any limitation/ feature from Pact to ignore data returned over Post?
t
Er. I suspect that if the pact test fails unless you’re using Get, then you’re not really using Post.
☝️ 1
m
Pact just blindly fires the requests you put into the pact file
if your provider tests pass when you put a GET in your consumer test and fail with a POST, there is a misunderstanding somewhere
If you’re sending a body with the GET, then there are additional problems to be concerned about
(bodies should not be sent with a GET)
p
For us we don't have any body for the request. We are simply taking data from header. So I simply changed Get to Post. But when I run test it throws error with status code 415 "request": { "headers": { "Accept": "application/json", "Tenant-Id": "DevTenant" }, "method": "POST", "path": "/api/v1/configurations:get-for-audit-determination" },
m
that error is coming from your provider API. You need to look at the logs for it, or talk to the team that runs the provider API
a 415 is inappropriate media type
so that tells me your provider doesn’t return JSON, but you’re asking it to. Does it return JSON?
p
It does
Sorry...from test?
Yes...from test I'm not getting any Json
From Provider test it says it received "" but we debugged the api from test.N it works as expected. It does return json data. But not sure why from test what's the issue...
m
which test is failing? The consumer one or provider?
p
provider one.
Consumer creates the contract file just fine. But from provider side broker returns "" and fail to validate the response from consumer pact with status code 415
m
Can you please set logging to DEBUG/Verbose and share?
p
yes..Verbose is set to true LOg from test explorer like below  HiiRetail.Sslp.TrustLevel.Service.UnitTests.Contracts.ProviderTest.EnsureProviderApiHonoursPactWithConsumer  Source: ProviderTest.cs line 19  Duration: 19.4 sec Message:  PactNet.PactFailureException : Pact verification failed. See output for details. If the output is empty please provide a custom config.Outputters (IOutput) for your test framework, as we couldn't write to the console. Stack Trace:  PactCoreHost`1.Start() PactVerifier.Verify(String description, String providerState) ProviderTest.EnsureProviderApiHonoursPactWithConsumer() line 42 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr) Standard Output:  WARN: Ignoring unsupported combine AND for path $ INFO: Reading pact at ../../../pacts/sslp-basketauditdeterminationservice-sslp-trustlevelservice.json Verifying a pact between sslp-basketauditdeterminationservice and sslp-trustlevelservice Given basket audit configurations exist A valid request to get all basket audit configurations with POST /api/v1/configurations:get-for-audit-determination returns a response which WARN: Skipping set up for provider state 'basket audit configurations exist' for consumer 'sslp-basketauditdeterminationservice' as there is no --provider-states-setup-url specified. INFO: Replacing header 'Tenant-Id: DevTenant' with 'Tenant-Id: DevTenant' has status code 200 (FAILED - 1) has a matching body (FAILED - 2) includes headers "Content-Type" which equals "application/json; charset=utf-8" (FAILED - 3) Failures: 1) Verifying a pact between sslp-basketauditdeterminationservice and sslp-trustlevelservice Given basket audit configurations exist A valid request to get all basket audit configurations with POST /api/v1/configurations:get-for-audit-determination returns a response which has status code 200 Failure/Error: expect(response_status).to eql expected_response_status expected: 200 got: 415 (compared using eql?) 2) Verifying a pact between sslp-basketauditdeterminationservice and sslp-trustlevelservice Given basket audit configurations exist A valid request to get all basket audit configurations with POST /api/v1/configurations:get-for-audit-determination returns a response which has a matching body Failure/Error: expect(response_body).to match_term expected_response_body, diff_options, example Actual: Diff -------------------------------------- WARN: Ignoring unsupported combine AND for path $ Key: - is expected + is actual Matching keys and values are not shown -{ - "tenant": { - "defaultTrustLevelNumber": 3, - "id": "DevTenant", - "name": "Use this tenant during development" - } -} Description of differences -------------------------------------- * Expected a Hash (like {"tenant"=>{"defaultTrustLevelNumber"=>3, "id"=>"DevTenant", "name"=>"Use this tenant during development"}}) but got a String ("") at $ 3) Verifying a pact between sslp-basketauditdeterminationservice and sslp-trustlevelservice Given basket audit configurations exist A valid request to get all basket audit configurations with POST /api/v1/configurations:get-for-audit-determination returns a response which includes headers "Content-Type" which equals "application/json; charset=utf-8" WARN: Cannot publish verification for sslp-basketauditdeterminationservice as there is no link named pb:publish-verification-results in the pact JSON. If you are using a pact broker, please upgrade to version 2.0.0 or later. Failure/Error: expect(header_value).to match_header(name, expected_header_value) Expected header "Content-Type" to equal "application/json; charset=utf-8", but was nil 1 interaction, 1 failure Failed interactions: To re-run just this failing interaction, change the verify method to '.Verify(description: "A valid request to get all basket audit configurations", providerState: "basket audit configurations exist")'. Please do not check in this change! # A valid request to get all basket audit configurations given basket audit configurations exist
👍 1
m
I think you can also get your version of Pact .NET to write to a log file. Please share that file also and ensure it is set to DEBUG level (it will contain the request/responses)
Pact is going to issue the request in the pact file at your Provider API. The response is what Pact is showing in that output
It’s possible, that Pact is not sending what’s in the pact file and that’s the cause of the issue. You can debug this in a few ways 1. Add logging to your provider and check what it sees 2. Use something like Wireshark to snoop the requests/responses, and check against what it should be doing
p
Sure... Let me try doing that...Only thing is When I change Post to Get...it falls in place. Will check in PactNet 3x how can I expose debug file..May be it's from CLI only
m
Only thing is When I change Post to Get
If that works, doesn’t that tell you something important about the problem?
p
We had a setting at global level services.AddControllers(options => { options.Filters.Add(new ConsumesAttribute(MediaTypeNames.Application.Json)); }
which seems to have been causing issues
So we added empty body in consumer like below .WithBody("{}", $"{MediaTypeNames.Application.Json}; charset={Encoding.UTF8.WebName}") So now our header request looks like below "request": { "headers": { "Accept": "application/json", "Content-Type": "application/json; charset=utf-8", "Tenant-Id": "DevTenant" }, "method": "POST", "path": "/api/v1/configurations:get-for-audit-determination" }, N it works now
m
this is even more confusing
Now you are saying you’re sending JSON (the
content-type
header), but are not sending JSON at all
p
yes, we just intimating request will have content-type but practically it does not have any body. but it's also some set up from our end, requests for it
👍 1
m
glad it’s sorted
p
Yeah...phew ... We kept thinking may be Pact requires content-type even if it's not needed. But it was our API which actually needed it and not Pact
1