fix(testing): parse response body to undefined if not provided in case of "application/json" header
MODIFY
packages/testing/src/testBed/http/http.testBed.response.ts
packages/testing/src/testBed/http/http.testBed.response.ts
@@ -12,7 +12,11 @@ const parseResponseBody = (props: HttpTestBedResponseProps): string | Array<any>
O.map(body => {
switch (getContentTypeUnsafe(props.headers)) {
case ContentType.APPLICATION_JSON:
- return pipe(body.toString(), parseJson);
+ return pipe(
+ body.toString(),
+ O.fromPredicate(Boolean),
+ O.map(parseJson),
+ O.toUndefined);
case ContentType.TEXT_PLAIN:
case ContentType.TEXT_HTML:
return body.toString();

MODIFY
packages/testing/src/testBed/http/http.testBed.spec.ts
packages/testing/src/testBed/http/http.testBed.spec.ts
import { pipe } from 'fp-ts/lib/function';
-import { httpListener, r } from '@marblejs/http';
+import { httpListener, HttpStatus, r } from '@marblejs/http';
import { requestValidator$, t } from '@marblejs/middleware-io';
import { bodyParser$ } from '@marblejs/middleware-body';
import { mapTo, map } from 'rxjs/operators';
@@ -131,4 +131,43 @@ describe('TetBed - HTTP', () => {
await finish();
});
+
+ test('parses response body to "undefined" if not provided', async () => {
+ // given
+ const test$ = r.pipe(
+ r.matchPath('/test'),
+ r.matchType('GET'),
+ r.useEffect(req$ => req$.pipe(
+ mapTo({ status: HttpStatus.NO_CONTENT }),
+ )));
+
+ const listener = httpListener({ effects: [test$] });
+ const { request: req, finish } = await createHttpTestBed({ listener })();
+
+ // when
+ const response = await pipe(
+ req('GET'),
+ req.withPath('/test'),
+ req.withHeaders({ 'Content-Type': 'application/json' }),
+ req.send,
+ );
+
+ // then
+ expect(response.req).toEqual(expect.objectContaining({
+ method: 'GET',
+ path: '/test',
+ }));
+
+ expect(response).toEqual(expect.objectContaining({
+ body: undefined,
+ statusCode: 204,
+ statusMessage: 'No Content',
+ headers: expect.objectContaining({
+ 'content-length': '0',
+ 'content-type': 'application/json',
+ }),
+ }));
+
+ await finish();
+ });
});

fix(testing): parse response body to undefined if not provided in case of "application/json" header