refactor(http): `responseBody.factory`
MODIFY
packages/http/src/response/http.responseBody.factory.ts
packages/http/src/response/http.responseBody.factory.ts
@@ -7,7 +7,7 @@ import { transformUrlEncoded } from '../+internal/urlEncoded.util';
export type ResponseBodyFactory = (headers: HttpHeaders) => (body: any) => string | Stream | Buffer;
-export const bodyFactory: ResponseBodyFactory = headers => body => {
+export const factorizeBody: ResponseBodyFactory = headers => body => {
const contentType = getContentTypeUnsafe(headers);
if (isStream(body))

MODIFY
packages/http/src/response/http.responseHandler.ts
packages/http/src/response/http.responseHandler.ts
@@ -10,7 +10,7 @@ import { HttpRequest, HttpResponse, HttpStatus } from '../http.interface';
import { HttpRequestMetadataStorageToken } from '../server/internal-dependencies/httpRequestMetadataStorage.reader';
import { getTestingRequestIdHeader } from '../+internal/header.util';
import { factorizeHeaders } from './http.responseHeaders.factory';
-import { bodyFactory } from './http.responseBody.factory';
+import { factorizeBody } from './http.responseBody.factory';
export const handleResponse = (ask: ContextProvider) => (res: HttpResponse) => (req: HttpRequest) => (effectResponse: HttpEffectResponse) => {
if (res.finished) { return EMPTY; }
@@ -21,7 +21,7 @@ export const handleResponse = (ask: ContextProvider) => (res: HttpResponse) => (
const headersFactoryWithData = factorizeHeaders({ body: effectResponse.body, path, status });
const headers = headersFactoryWithData(effectResponse.headers)(); // @TODO: refactor `#handleResponse` function
- const bodyFactoryWithHeaders = bodyFactory(headers);
+ const bodyFactoryWithHeaders = factorizeBody(headers);
const body = bodyFactoryWithHeaders(effectResponse.body);
const testingHeader = getTestingRequestIdHeader(req);

MODIFY
packages/http/src/response/http.responseHeaders.factory.ts
packages/http/src/response/http.responseHeaders.factory.ts
@@ -29,7 +29,7 @@ export const MARBLE_HTTP_HEADERS_NORMALIZATION_ENV_KEY = 'MARBLE_HTTP_HEADERS_NO
const useHttpHeadersNormalization = getEnvConfigOrElseAsBoolean(MARBLE_HTTP_HEADERS_NORMALIZATION_ENV_KEY, true);
-const provideContentLengthHeader = (response: HttpResponseLike): HttpHeaders => {
+export const provideContentLengthHeader = (response: HttpResponseLike): HttpHeaders => {
if (isStream(response.body)) return {};
const contentLength = isNullable(response.body) ? 0 : Buffer.byteLength(

MODIFY
packages/http/src/response/specs/http.responseBody.factory.spec.ts
packages/http/src/response/specs/http.responseBody.factory.spec.ts
import { ContentType } from '../../+internal/contentType.util';
-import { bodyFactory } from '../http.responseBody.factory';
+import { factorizeBody } from '../http.responseBody.factory';
-describe('Response body factory', () => {
-
- it('#bodyFactory factorizes body for urlencoded', () => {
+describe('#bodyFactory', () => {
+ test('factorizes body for urlencoded', () => {
// given
const headers = { 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED };
const body = { test: 'test' };
// when
- const factorizedBody = bodyFactory(headers)(body);
+ const factorizedBody = factorizeBody(headers)(body);
// then
expect(factorizedBody).toEqual('test=test');
});
- it('#bodyFactory doesn\'t factorize body for urlencoded if body is stringified', () => {
+ test('factorizes body for JSON', () => {
// given
- const headers = { 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED };
- const body = 'test=test';
+ const headers = { 'Content-Type': ContentType.APPLICATION_JSON };
+ const body = { test: 'test' };
// when
- const factorizedBody = bodyFactory(headers)(body);
+ const factorizedBody = factorizeBody(headers)(body);
// then
- expect(factorizedBody).toEqual('test=test');
+ expect(factorizedBody).toEqual(JSON.stringify(body));
});
- it('#bodyFactory factorizes body for JSON', () => {
+ test('factorizes body for plain text', () => {
// given
- const headers = { 'Content-Type': ContentType.APPLICATION_JSON };
- const body = { test: 'test' };
+ const headers = { 'Content-Type': ContentType.TEXT_PLAIN };
+ const bodies = [33, 'test', false];
// when
- const factorizedBody = bodyFactory(headers)(body);
+ const factorizedBodies = bodies.map(factorizeBody(headers));
// then
- expect(factorizedBody).toEqual(JSON.stringify(body));
+ expect(factorizedBodies).toEqual(bodies.map(String));
});
- it('#bodyFactory factorizes body for plain text', () => {
+ test('doesn\'t factorize body for urlencoded if body is stringified', () => {
// given
- const headers = { 'Content-Type': ContentType.TEXT_PLAIN };
- const bodies = [33, 'test', false];
+ const headers = { 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED };
+ const body = 'test=test';
// when
- const factorizedBodies = bodies.map(bodyFactory(headers));
+ const factorizedBody = factorizeBody(headers)(body);
// then
- expect(factorizedBodies).toEqual(bodies.map(String));
+ expect(factorizedBody).toEqual('test=test');
});
- it(`#bodyFactory doesn't factorize body`, () => {
+ test(`doesn't factorize body if "Content-Type" is unknown`, () => {
// given
const headers = { 'Content-Type': ContentType.AUDIO };
const body = '1234';
// when
- const factorizedBody = bodyFactory(headers)(body);
+ const factorizedBody = factorizeBody(headers)(body);
// then
expect(factorizedBody).toEqual('1234');
});
-
});

MODIFY
packages/testing/src/testBed/http/http.testBed.ts
packages/testing/src/testBed/http/http.testBed.ts
@@ -6,7 +6,7 @@ import { lookup, ContextProvider } from '@marblejs/core';
import { createServer, HttpMethod, HttpRequestMetadataStorageToken } from '@marblejs/http';
import { closeServer, getServerAddress } from '@marblejs/http/dist/+internal/server.util';
import { isStream } from '@marblejs/core/dist/+internal/utils';
-import { bodyFactory } from '@marblejs/http/dist/response/http.responseBody.factory';
+import { factorizeBody } from '@marblejs/http/dist/response/http.responseBody.factory';
import { TESTING_REQUEST_ID_HEADER, TestingMetadata } from '@marblejs/core/dist/+internal/testing';
import { TestBedType, TestBedFactory } from '../testBed.interface';
import { HttpTestBedConfig, HttpTestBed } from './http.testBed.interface';
@@ -48,7 +48,7 @@ const sendRequest = async <T extends HttpMethod>(testBedRequest: HttpTestBedRequ
}
if (testBedRequest.body) {
- const body = bodyFactory(testBedRequest.headers)(testBedRequest.body);
+ const body = factorizeBody(testBedRequest.headers)(testBedRequest.body);
return clientRequest.end(body);
}

refactor(http): `responseBody.factory`