hi all, I am getting HTTP 500 error for a Post request with a body in my consumer test
Here is my request object in C#
``` var exposureRequest = new ExposuresRequest
{
PortfolioId = "10000000-0000-0000-0000-000000000000",
PortfolioNamespace = "MaceConsumerTests/GBP Holdings portfolio",
PortfolioClassificationNamespace = "namespace_PortfolioA",
EffectDate = new DateTime(2021, 05, 05),
Currency = "USD",
PortfolioDataDates = new Dictionary<string, DateTime> { { "10000000-0000-0000-0000-000000000000", new DateTime(2021, 05, 06) } },
UseStorage = true,
ClassificationHierarchy = new string[][] { new string[] { "industry" } },
OutputType = ExposuresOutputType.GroupLevel,
Path = null
};```
and here is my Pact consumer request:
``` pact.UponReceiving("A valid request")
.Given("A valid request for get exposures")
.WithRequest(
http://HttpMethod.Post, "/api/exposures")
.WithHeader("content-type", "application/json; charset=utf-8")
.WithJsonBody(exposureRequest,new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })
.WillRespond()
.WithStatus(HttpStatusCode.OK)
.WithHeader("content-type", "application/json")
.WithJsonBody(new TypeMatcher(new ExposuresResponse()));
await pact.VerifyAsync(async ctx =>
{
var response = await ApiClient.GetExposures(exposureRequest);
Assert.NotNull(response);
});```
and this the ApiClient that is actually making http request
``` public async Task<ExposuresResponse> GetExposures(ExposuresRequest request)
{
var requestContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
var httpResponse = await httpClient.PostAsync("api/exposures", requestContent);
var exposuresResponse = await TryGetResponse<ExposuresResponse>(httpResponse);
return exposuresResponse;
}```
I am getting HTTP Status code 500, Internal server error because the actual request does not meet expected request. Here is the Log output
``` Standard Output Messages:
Mock server logs:
[DEBUG][pact_mock_server::hyper_server] Creating pact request from hyper request
[DEBUG][pact_mock_server::hyper_server] Extracting query from uri /api/exposures
[INFO][pact_mock_server::hyper_server] Received request Request ( method: POST, path: /api/exposures, query: None, headers: Some({"host": ["localhost:8197"], "content-type": ["application/json; charset=utf-8"], "content-length": ["501"]}), body: Present(501 bytes, application/json;charset=utf-8) )
[DEBUG][pact_mock_server::hyper_server] body: '{"ClassificationHierarchy":[["industry"]],"UseStorage":true,"OutputType":1,"Path":null,"EffectDate":"2021-05-05T00:00:00","PortfolioDataDates":{"10000000-0000-0000-0000-000000000000":"2021-05-06T00:00:00"},"Currency":"USD","PortfolioId":"10000000-0000-0000-0000-000000000000","PortfolioNamespace":"MaceConsumerTests/GBP Holdings portfolio","PortfolioClassificationNamespace":"namespace_PortfolioA","BenchmarkPortfolioId":null,"BenchmarkPortfolioNamespace":null,"BenchmarkClassificationNamespace":null}'
[INFO][pact_matching] comparing to expected Request ( method: POST, path: /api/exposures, query: None, headers: Some({"content-type": ["application/json; charset=utf-8"]}), body: Present(501 bytes) )
[DEBUG][pact_matching] body: '{"benchmarkClassificationNamespace":null,"benchmarkPortfolioId":null,"benchmarkPortfolioNamespace":null,"classificationHierarchy":[["industry"]],"currency":"USD","effectDate":"2021-05-05T00:00:00","outputType":1,"path":null,"portfolioClassificationNamespace":"namespace_PortfolioA","portfolioDataDates":{"10000000-0000-0000-0000-000000000000":"2021-05-06T00:00:00"},"portfolioId":"10000000-0000-0000-0000-000000000000","portfolioNamespace":"MaceConsumerTests/GBP Holdings portfolio","useStorage":true}'
[DEBUG][pact_matching] matching_rules: MatchingRules { rules: {HEADER: MatchingRuleCategory { name: HEADER, rules: {} }, PATH: MatchingRuleCategory { name: PATH, rules: {} }, BODY: MatchingRuleCategory { name: BODY, rules: {} }} }
[DEBUG][pact_matching] generators: Generators { categories: {} }
[DEBUG][pact_matching::matchers] String -> String: comparing '/api/exposures' to '/api/exposures' using Equality
[DEBUG][pact_matching] expected content type = 'application/json;charset=utf-8', actual content type = 'application/json;charset=utf-8'
[DEBUG][pact_matching] content type header matcher = 'None'
[DEBUG][pact_matching] Using body matcher for content type 'application/json;charset=utf-8'
[DEBUG][pact_matching::json] compare: Comparing path $
[DEBUG][pact_matching::json] compare_maps: Comparing maps at $: {"benchmarkClassificationNamespace": Null, "benchmarkPortfolioId": Null, "benchmarkPortfolioNamespace": Null, "classificationHierarchy": Array([Array([String("industry")])]), "currency": String("USD"), "effectDate": String("2021-05-05T00:00:00"), "outputType": Number(1), "path": Null, "portfolioClassificationNamespace": String("namespace_PortfolioA"), "portfolioDataDates": Object({"10000000-0000-0000-0000-000000000000": String("2021-05-06T00:00:00")}), "portfolioId": String("10000000-0000-0000-0000-000000000000"), "portfolioNamespace": String("MaceConsumerTests/GBP Holdings portfolio"), "useStorage": Bool(true)} -> {"BenchmarkClassificationNamespace": Null, "BenchmarkPortfolioId": Null, "BenchmarkPortfolioNamespace": Null, "ClassificationHierarchy": Array([Array([String("industry")])]), "Currency": String("USD"), "EffectDate": String("2021-05-05T00:00:00"), "OutputType": Number(1), "Path": Null, "PortfolioClassificationNamespace": String("namespace_PortfolioA"), "PortfolioDataDates": Object({"10000000-0000-0000-0000-000000000000": String("2021-05-06T00:00:00")}), "PortfolioId": String("10000000-0000-0000-0000-000000000000"), "PortfolioNamespace": String("MaceConsumerTests/GBP Holdings portfolio"), "UseStorage": Bool(true)}
[DEBUG][pact_matching] --> Mismatches: [BodyMismatch { path: "$", expected: Some(b"{\"benchmarkClassificationNamespace\":\"null\",\"benchmarkPortfolioId\":\"null\",\"benchmarkPortfolioNamespace\":\"null\",\"classificationHierarchy\":\"[[\\\"industry\\\"]]\",\"currency\":\"\\\"USD\\\"\",\"effectDate\":\"\\\"2021-05-05T00:00:00\\\"\",\"outputType\":\"1\",\"path\":\"null\",\"portfolioClassificationNamespace\":\"\\\"namespace_PortfolioA\\\"\",\"portfolioDataDates\":\"{\\\"10000000-0000-0000-0000-000000000000\\\":\\\"2021-05-06T00:00:00\\\"}\",\"portfolioId\":\"\\\"10000000-0000-0000-0000-000000000000\\\"\",\"portfolioNamespace\":\"\\\"MaceConsumerTests/GBP Holdings portfolio\\\"\",\"useStorage\":\"true\"}"), actual: Some(b"{\"BenchmarkClassificationNamespace\":\"null\",\"BenchmarkPortfolioId\":\"null\",\"BenchmarkPortfolioNamespace\":\"null\",\"ClassificationHierarchy\":\"[[\\\"industry\\\"]]\",\"Currency\":\"\\\"USD\\\"\",\"EffectDate\":\"\\\"2021-05-05T00:00:00\\\"\",\"OutputType\":\"1\",\"Path\":\"null\",\"PortfolioClassificationNamespace\":\"\\\"namespace_PortfolioA\\\"\",\"PortfolioDataDates\":\"{\\\"10000000-0000-0000-0000-000000000000\\\":\\\"2021-05-06T00:00:00\\\"}\",\"PortfolioId\":\"\\\"10000000-0000-0000-0000-000000000000\\\"\",\"PortfolioNamespace\":\"\\\"MaceConsumerTests/GBP Holdings portfolio\\\"\",\"UseStorage\":\"true\"}"), mismatch: "Expected a Map with keys benchmarkClassificationNamespace, benchmarkPortfolioId, benchmarkPortfolioNamespace, classificationHierarchy, currency, effectDate, outputType, path, portfolioClassificationNamespace, portfolioDataDates, portfolioId, portfolioNamespace, useStorage but received one with keys BenchmarkClassificationNamespace, BenchmarkPortfolioId, BenchmarkPortfolioNamespace, ClassificationHierarchy, Currency, EffectDate, OutputType, Path, PortfolioClassificationNamespace, PortfolioDataDates, PortfolioId, PortfolioNamespace, UseStorage" }]
[DEBUG][pact_mock_server::hyper_server] Request did not match: Request did not match - Request ( method: POST, path: /api/exposures, query: None, headers: Some({"content-type": ["application/json; charset=utf-8"]}), body: Present(501 bytes) ) 0) $ -> Expected a Map with keys benchmarkClassificationNamespace, benchmarkPortfolioId, benchmarkPortfolioNamespace, classificationHierarchy, currency, effectDate, outputType, path, portfolioClassificationNamespace, portfolioDataDates, portfolioId, portfolioNamespace, useStorage but received one with keys BenchmarkClassificationNamespace, BenchmarkPortfolioId, BenchmarkPortfolioNamespace, ClassificationHierarchy, Currency, EffectDate, OutputType, Path, PortfolioClassificationNamespace, PortfolioDataDates, PortfolioId, PortfolioNamespace, UseStorage```
I am not able to find out why Pact complains request did not match. Can someone please help here. I have already spent 1 day on this and blocked here.
Thank you