refactor(middleware-cors): adjusted middleware to changed Effect interface
MODIFY
packages/middleware-cors/src/middleware.ts
packages/middleware-cors/src/middleware.ts
-import {
- HttpMethod,
- HttpMiddlewareEffect,
- HttpRequest,
- HttpStatus,
-} from '@marblejs/core';
-import { Observable } from 'rxjs';
+import { HttpMethod, HttpMiddlewareEffect, HttpStatus } from '@marblejs/core';
import { tap } from 'rxjs/operators';
import { isString } from 'util';
@@ -28,10 +22,7 @@ const DEFAULT_OPTIONS: CORSOptions = {
optionsSuccessStatus: HttpStatus.NO_CONTENT,
};
-export const cors$ = (options: CORSOptions = {}): HttpMiddlewareEffect => (
- req$: Observable<HttpRequest>,
- res,
-) => {
+export const cors$ = (options: CORSOptions = {}): HttpMiddlewareEffect => (req$, metadata) => {
options = { ...DEFAULT_OPTIONS, ...options };
return req$.pipe(
@@ -42,10 +33,10 @@ export const cors$ = (options: CORSOptions = {}): HttpMiddlewareEffect => (
}
if (req.method === 'OPTIONS') {
- configurePreflightResponse(req, res, options);
- res.end();
+ configurePreflightResponse(req, metadata.client, options);
+ metadata.client.end();
} else {
- configureResponse(req, res, options);
+ configureResponse(req, metadata.client, options);
}
}),
);

MODIFY
packages/middleware-cors/src/spec/applyHeaders.spec.ts
packages/middleware-cors/src/spec/applyHeaders.spec.ts
import { AccessControlHeader, applyHeaders, ConfiguredHeader } from '../applyHeaders';
-import { createMockResponse } from './middleware.spec';
+import { createMockResponse } from '../util';
describe('applyHeaders', () => {
test('should handle many methods correctly', done => {

MODIFY
packages/middleware-cors/src/spec/checkOrigin.spec.ts
packages/middleware-cors/src/spec/checkOrigin.spec.ts
import { checkOrigin } from '../checkOrigin';
-import { createMockRequest } from './middleware.spec';
+import { createMockRequest } from '../util';
describe('checkOrigin', () => {
test('check wildcard option correctly', done => {

MODIFY
packages/middleware-cors/src/spec/configurePreflightResponse.spec.ts
packages/middleware-cors/src/spec/configurePreflightResponse.spec.ts
import { configurePreflightResponse } from '../configurePreflightResponse';
import { CORSOptions } from '../middleware';
-import { createMockRequest, createMockResponse } from './middleware.spec';
+import { createMockRequest, createMockResponse } from '../util';
describe('configurePreflightResponse', () => {
test('should configure headers correctly', done => {

MODIFY
packages/middleware-cors/src/spec/configureResponse.spec.ts
packages/middleware-cors/src/spec/configureResponse.spec.ts
import { configureResponse } from '../configureResponse';
import { CORSOptions } from '../middleware';
-import { createMockRequest, createMockResponse } from './middleware.spec';
+import { createMockRequest, createMockResponse } from '../util';

MODIFY
packages/middleware-cors/src/spec/middleware.spec.ts
packages/middleware-cors/src/spec/middleware.spec.ts
-import { HttpMethod, HttpRequest, HttpResponse, createEffectMetadata, lookup, createContext } from '@marblejs/core';
import { Marbles } from '@marblejs/core/dist/+internal';
-
import { cors$, CORSOptions } from '../middleware';
import { of } from 'rxjs';
-
-export const createMockResponse = () =>
- (({
- writeHead: jest.fn(),
- setHeader: jest.fn(),
- getHeader: jest.fn(),
- end: jest.fn(),
- } as unknown) as HttpResponse);
-
-export const createMockRequest = (
- method: HttpMethod = 'GET',
- headers: any = { origin: 'fake-origin' },
-) =>
- (({
- method,
- headers: { ...headers },
- } as unknown) as HttpRequest);
-
-const context = createContext();
-const metadata = createEffectMetadata({ ask: lookup(context) });
+import { createMockRequest, createMockMetadata } from '../util';
describe('CORS middleware', () => {
test('pass through non CORS requests', () => {
+ // given
const request = createMockRequest('OPTIONS', { origin: null });
+ const meta = createMockMetadata();
+ // when
const middleware$ = cors$();
+ // then
Marbles.assertEffect(middleware$, [
['-a-', { a: request }],
['-a-', { a: request }],
- ]);
-
+ ], { meta });
});
test('handle CORS preflight request', done => {
expect.assertions(1);
const request = createMockRequest('OPTIONS', { origin: 'fake-origin' });
+ const metadata = createMockMetadata();
const request$ = of(request);
- const response = createMockResponse();
const middleware$ = cors$();
- middleware$(request$, response, metadata).subscribe(() => {
- expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
+ middleware$(request$, metadata).subscribe(() => {
+ expect(metadata.client.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
done();
});
});
@@ -56,8 +38,8 @@ describe('CORS middleware', () => {
expect.assertions(3);
const request = createMockRequest('GET', { origin: 'fake-origin' });
+ const metadata = createMockMetadata();
const request$ = of(request);
- const response = createMockResponse();
const options: CORSOptions = {
origin: 'fake-origin',
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
@@ -70,10 +52,10 @@ describe('CORS middleware', () => {
const middleware$ = cors$(options);
- middleware$(request$, response, metadata).subscribe(() => {
- expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
- expect(response.setHeader).toBeCalledWith('Access-Control-Allow-Credentials', 'true');
- expect(response.setHeader).toBeCalledWith('Access-Control-Expose-Headers', 'X-Header, X-Custom-Header');
+ middleware$(request$, metadata).subscribe(() => {
+ expect(metadata.client.setHeader).toBeCalledWith('Access-Control-Allow-Origin', 'fake-origin');
+ expect(metadata.client.setHeader).toBeCalledWith('Access-Control-Allow-Credentials', 'true');
+ expect(metadata.client.setHeader).toBeCalledWith('Access-Control-Expose-Headers', 'X-Header, X-Custom-Header');
done();
});
});

MODIFY
packages/middleware-cors/src/util.ts
packages/middleware-cors/src/util.ts
+import { HttpResponse, HttpMethod, HttpRequest, createEffectMetadata, createContext, lookup } from '@marblejs/core';
+
export const capitalize = (str: string): string =>
str
.split('-')
@@ -6,3 +8,25 @@ export const capitalize = (str: string): string =>
export const isString = (str: any): boolean =>
typeof str === 'string' || str instanceof String;
+
+
+export const createMockResponse = () => (({
+ writeHead: jest.fn(),
+ setHeader: jest.fn(),
+ getHeader: jest.fn(),
+ end: jest.fn(),
+} as unknown) as HttpResponse);
+
+export const createMockRequest = (
+ method: HttpMethod = 'GET',
+ headers: any = { origin: 'fake-origin' },
+) => (({
+ method,
+ headers: { ...headers },
+} as unknown) as HttpRequest);
+
+export const createMockMetadata = () => {
+ const context = createContext();
+ const client = createMockResponse();
+ return createEffectMetadata({ ask: lookup(context), client });
+};

refactor(middleware-cors): adjusted middleware to changed Effect interface