imp(http helper): simplified `createHttpRequest` function and it's usages
MODIFY
packages/core/src/+internal/testing/http.helper.ts
packages/core/src/+internal/testing/http.helper.ts
@@ -10,7 +10,7 @@ import {
import { EventEmitter } from 'events';
interface HttpRequestMockParams {
- url: string;
+ url?: string;
body?: any;
params?: RouteParameters;
query?: QueryParameters;
@@ -30,15 +30,17 @@ export interface HttpServerMocks {
on?: jest.Mock;
}
-export const createHttpRequest = (data: HttpRequestMockParams = { url: '/' }) => ({
- ...data,
- url: data.url,
- body: data.body,
- params: data.params || {},
- query: data.query || {},
- headers: data.headers || {},
- method: data.method || 'GET',
-}) as HttpRequest;
+export const createHttpRequest = (data?: HttpRequestMockParams) => Object.assign(
+ {},
+ {
+ url: '/',
+ method: 'GET',
+ headers: {},
+ query: {},
+ params: {},
+ },
+ data,
+) as HttpRequest;
export const createHttpResponse = (data: HttpResponseMockParams = {}) =>
new class extends EventEmitter {

MODIFY
packages/core/src/effects/specs/effects.combiner.spec.ts
packages/core/src/effects/specs/effects.combiner.spec.ts
@@ -9,8 +9,8 @@ describe('#combineMiddlewares', () => {
const a$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = 1; }));
const b$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
const c$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
- const incomingRequest = createHttpRequest({ url: '/' });
- const outgoingRequest = createHttpRequest({ url: '/', test: 3 });
+ const incomingRequest = createHttpRequest();
+ const outgoingRequest = createHttpRequest({ test: 3 });
// when
const middlewares$ = combineMiddlewares(a$, b$, c$);
@@ -24,8 +24,8 @@ describe('#combineMiddlewares', () => {
test('returns stream even if middlewares are not provided', () => {
// given
- const incomingRequest = createHttpRequest({ url: '/' });
- const outgoingRequest = createHttpRequest({ url: '/' });
+ const incomingRequest = createHttpRequest();
+ const outgoingRequest = createHttpRequest();
// when
const middlewares$ = combineMiddlewares();

MODIFY
packages/core/src/error/specs/error.effect.spec.ts
packages/core/src/error/specs/error.effect.spec.ts
@@ -3,7 +3,7 @@ import { defaultError$ } from '../error.effect';
import { HttpError } from '../error.model';
describe('defaultError$', () => {
- const incomingRequest = createHttpRequest({ url: '/' });
+ const incomingRequest = createHttpRequest();
const client = createHttpResponse();
test('maps HttpError', () => {

MODIFY
packages/core/src/operators/switchToProtocol/switchToProtocol.operator.spec.ts
packages/core/src/operators/switchToProtocol/switchToProtocol.operator.spec.ts
@@ -7,7 +7,6 @@ describe('#switchToProtocol operator', () => {
test(`responds with ${HttpStatus.SWITCHING_PROTOCOLS} status if protocol is supported`, () => {
// given
const incomingWebsocketReq = createHttpRequest({
- url: '/',
headers: {
upgrade: 'websocket',
connection: 'upgrade',
@@ -17,7 +16,6 @@ describe('#switchToProtocol operator', () => {
status: HttpStatus.SWITCHING_PROTOCOLS,
};
const incomingHttpReq = createHttpRequest({
- url: '/',
headers: {
upgrade: 'http2',
connection: 'upgrade',

MODIFY
packages/middleware-body/src/specs/body.util.spec.ts
packages/middleware-body/src/specs/body.util.spec.ts
@@ -2,9 +2,9 @@ import { createHttpRequest } from '@marblejs/core/dist/+internal/testing/http.he
import { hasBody } from '../body.util';
test('#hasBody checks if request has body', () => {
- expect(hasBody(createHttpRequest({ url: '/', body: null }))).toEqual(false);
- expect(hasBody(createHttpRequest({ url: '/', body: undefined }))).toEqual(false);
- expect(hasBody(createHttpRequest({ url: '/', body: 'test' }))).toEqual(true);
- expect(hasBody(createHttpRequest({ url: '/', body: {} }))).toEqual(true);
- expect(hasBody(createHttpRequest({ url: '/', body: 1 }))).toEqual(true);
+ expect(hasBody(createHttpRequest({ body: null }))).toEqual(false);
+ expect(hasBody(createHttpRequest({ body: undefined }))).toEqual(false);
+ expect(hasBody(createHttpRequest({ body: 'test' }))).toEqual(true);
+ expect(hasBody(createHttpRequest({ body: {} }))).toEqual(true);
+ expect(hasBody(createHttpRequest({ body: 1 }))).toEqual(true);
});

MODIFY
packages/serverless/src/+awsLambda/spec/awsLambda.middleware.spec.ts
packages/serverless/src/+awsLambda/spec/awsLambda.middleware.spec.ts
@@ -7,7 +7,6 @@ describe('@marblejs/serverless - AWS API Gateway Middleware', () => {
test('extracts headers from request', async () => {
const request = await awsApiGatewayMiddleware$()(
of(createHttpRequest({
- url: 'ignored',
headers: {
[AwsLambdaHeaders.AWSLAMBDA_CONTEXT]: ['%7B%22context%22%3A0%7D'],
[AwsLambdaHeaders.APIGATEWAY_EVENT]: '%7B%22event%22%3A1%7D',

imp(http helper): simplified `createHttpRequest` function and it's usages