fix(core): bodyFactory - support for "application/x-www-form-urlencoded"
MODIFY
packages/core/src/http/response/http.responseBody.factory.ts
packages/core/src/http/response/http.responseBody.factory.ts
+import * as qs from 'qs';
import { HttpHeaders } from '../http.interface';
import { ContentType, getContentType } from '../../+internal/http';
-import { isStream } from '../../+internal/utils';
+import { isStream, isString } from '../../+internal/utils';
export type ResponseBodyFactory = (headers: HttpHeaders) => (body: any) => any;
+export type BodyTransformer = (body: any) => string;
+
+const transformUrlEncoded: BodyTransformer = body =>
+ !isString(body) ? qs.stringify(body) : body;
+
+const transformJson: BodyTransformer = body =>
+ JSON.stringify(body);
export const bodyFactory: ResponseBodyFactory = headers => body => {
- if (isStream(body)) {
- return body;
- }
+ if (isStream(body)) return body;
switch (getContentType(headers)) {
+ case ContentType.APPLICATION_X_WWW_FORM_URLENCODED:
+ return transformUrlEncoded(body);
case ContentType.APPLICATION_JSON:
- return JSON.stringify(body);
+ return transformJson(body);
case ContentType.TEXT_PLAIN:
return String(body);
default:

MODIFY
packages/core/src/http/response/http.responseContentType.factory.ts
packages/core/src/http/response/http.responseContentType.factory.ts
@@ -18,7 +18,7 @@ export const contentTypeFactory = (data: {
status: HttpStatus;
}) => ({
'Content-Type':
- data.status === 200
+ data.status < 400
? getMimeType(data.body, data.path)
: DEFAULT_CONTENT_TYPE,
});

MODIFY
packages/core/src/http/response/specs/http.responseBody.factory.spec.ts
packages/core/src/http/response/specs/http.responseBody.factory.spec.ts
@@ -3,6 +3,30 @@ import { bodyFactory } from '../http.responseBody.factory';
describe('Response body factory', () => {
+ it('#bodyFactory 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);
+
+ // then
+ expect(factorizedBody).toEqual('test=test');
+ });
+
+ it('#bodyFactory doesn\'t factorize body for urlencoded if body is stringified', () => {
+ // given
+ const headers = { 'Content-Type': ContentType.APPLICATION_X_WWW_FORM_URLENCODED };
+ const body = 'test=test';
+
+ // when
+ const factorizedBody = bodyFactory(headers)(body);
+
+ // then
+ expect(factorizedBody).toEqual('test=test');
+ });
+
it('#bodyFactory factorizes body for JSON', () => {
// given
const headers = { 'Content-Type': ContentType.APPLICATION_JSON };

fix(core): bodyFactory - support for "application/x-www-form-urlencoded"