Skip to content

Commit

Permalink
feat(http): generate an empty response if no matching response if fou…
Browse files Browse the repository at this point in the history
…nd for chaos
  • Loading branch information
P0lip committed Nov 28, 2024
1 parent b855365 commit 9dbe170
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/http/src/mocker/negotiator/NegotiatorHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ const helpers = {
);
},

negotiateEmptyResponse(code: number): RE.ReaderEither<Logger, Error, IHttpNegotiationResult> {
return withLogger(() =>
pipe(
createEmptyResponse(String(code), [], ['*/*']),
E.fromOption(() => new Error(`Unable to create an empty response for ${code}`))
)
);
},

negotiateOptionsBySpecificCode(
httpOperation: IHttpOperation,
desiredOptions: NegotiationOptions,
Expand Down Expand Up @@ -304,7 +313,11 @@ const helpers = {
O.map(chaos => chaos.codes as NonEmptyArray<number>),
O.fold(
() => helpers.negotiateOptionsForUnspecifiedCode(httpOperation, desiredOptions),
statusCodes => helpers.negotiateOptionsForErrorCodes(httpOperation, desiredOptions, statusCodes)
statusCodes =>
pipe(
helpers.negotiateOptionsForErrorCodes(httpOperation, desiredOptions, statusCodes),
RE.orElse(() => helpers.negotiateEmptyResponse(statusCodes[0]))
)
)
),
code => helpers.negotiateOptionsBySpecificCode(httpOperation, desiredOptions, code)
Expand Down
38 changes: 38 additions & 0 deletions test-harness/specs/config/chaos_missing_codes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
====test====
Given I mock and specify a missing error code for chaos
When I send a request to an operation
Then the response should be blank with that status code
====spec====
openapi: "3.1.0"
info:
version: "0.0"
title: Config Test
paths:
/pets/{petId}:
get:
description: Get a pet by ID
responses:
"200":
description: A pet
content:
application/json:
schema:
type: object
properties:
name:
type: string
====config====
{
"chaos": {
"enabled": true,
"rate": 100,
"codes": [401]
}
}
====server====
mock -p 4010 --config ${config} ${document}
====command====
curl -i http://localhost:4010/pets/2
====expect====
HTTP/1.1 401 Unauthorized
Content-Length: 0

0 comments on commit 9dbe170

Please sign in to comment.