Maksym Liannoi
09/27/2022, 10:12 AM"application/octet-stream"
(pact-js library) and verification on the provider side with C# language (pact-net library)? Because of many of my attempts, the response is 200 on the provider side, but when I try to test HTTP 400 Bad Request, I cannot find a suitable withRequest
body for that test.
test("...", async () => {
const badRequestData = {
error: {
code: "validationError",
message: "Validation error(s) has occurred",
details: [],
},
};
const badRequestResponse = somethingLike({
error: {
code: like(badRequestData.error.code),
message: like(badRequestData.error.message),
details: like(badRequestData.error.details),
},
});
const status = 400;
await provider.addInteraction({
state: "...",
uponReceiving: "...",
withRequest: { method, headers, path, body: "\u0000" },
willRespondWith: { status, body: badRequestResponse },
});
expect.assertions(3);
try {
await testService.sendContent(mailboxId, content);
} catch (e) {
checkIfHttpError(e, status);
expect((e as AxiosError).response?.data).toStrictEqual(badRequestData);
}
});
[ApiController]
[Route("test")]
public class TestController : ControllerBase
{
private readonly ITestService _testService;
public TestController(ITestService testService)
{
_testService = testService;
}
[HttpPost("...")]
[Authorize(...)]
[Produces("application/json")]
[Consumes("application/octet-stream")]
[ProducesResponseType(type: typeof(TestResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<TestResponse> HandleAsync(string id, [FromQuery(Name = "fileName")] string? fileName)
{
if (Request.ContentLength == 0)
{
throw new InvalidArgumentException("Request body should not be empty");
}
var messageId = await _testService.HandleAsync(id: id.ToLower().Trim(), fileName, Request.Body);
return new TestResponse(messageId);
}
}
Matt (pactflow.io / pact-js / pact-go)
400
use case?Maksym Liannoi
09/27/2022, 6:34 PMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
400
on the provider by given them garbage and by knowledge of why it’s causing a 400
. The main thing is that the consumer can handle a 400
. The consumer test should not encode detailed knowledge of how the provider works like this, as it might lead to brittle tests if the provider changes its validation.Maksym Liannoi
09/28/2022, 1:17 AMMaksym Liannoi
09/28/2022, 2:00 AMArrayBuffer(0)
to test empty file sending on our UI. But, Pact convert withRequest
body
to null if that body is {}
or ArrayBuffer(0)
[2022-09-28 01:54:13.419 +0000] ERROR (26056 on ...): pact@9.18.1: error making http request: Request failed with status code 500
FAIL .../__tests__/TestingService.contract.test.ts (5.606 s)
● Console
console.error
at node_modules/@pact-foundation/src/httpPact.ts:151:17
console.error
Pact verification failed!
at node_modules/@pact-foundation/src/httpPact.ts:152:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Incorrect requests:
POST /.../004b90d2 (request body did not match)
Diff with interaction: "a bad post content request" given "..."
Diff
--------------------------------------
Key: - is expected
+ is actual
Matching keys and values are not shown
{
- "body": {
- }
+ "body": null
}
Description of differences
--------------------------------------
* Expected a Hash but got nil at $.body
See C:/.../.../.../pact/logs/...-mockserver-interaction-port-9001.log for details.
at node_modules/@pact-foundation/src/httpPact.ts:153:17
await provider.addInteraction({
state: "...",
uponReceiving: "a bad post content request",
withRequest: { method, headers, path, body: /* new ArrayBuffer(0) or {} */ },
willRespondWith: { status, body: badRequestResponse },
});
expect.assertions(3);
try {
await keyService.sendContent(mailboxId, new ArrayBuffer(0));
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Maksym Liannoi
09/28/2022, 2:20 AMMaksym Liannoi
09/28/2022, 2:25 AMMatt (pactflow.io / pact-js / pact-go)
400
. You could do that with provider states, and be agnostic to how the 400
happened (but it’s a bit clunky for the provider). You should definitely functionally test the provider for its validation use cases, but these belong in the provider code baseMaksym Liannoi
09/28/2022, 2:55 AMMatt (pactflow.io / pact-js / pact-go)