tests(integration) Add cors middlawre tests
MODIFY
packages/@integration/src/http.listener.ts
packages/@integration/src/http.listener.ts
import { httpListener } from '@marblejs/core';
import { bodyParser$ } from '@marblejs/middleware-body';
import { loggerDev$, loggerFile$ } from './middlewares/logger.middleware';
+import { cors$ } from './middlewares/cors.middleware';
import { api$ } from './effects/api.effects';
export default httpListener({
@@ -8,6 +9,7 @@ export default httpListener({
loggerDev$,
loggerFile$,
bodyParser$(),
+ cors$,
],
effects: [api$]
});

ADD
packages/@integration/src/middlewares/cors.middleware.ts
+import { cors$ as corsMiddleware$ } from '@marblejs/middleware-cors';
+
+export const cors$ = corsMiddleware$({
+ origin: '*',
+ methods: ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
+ optionsSuccessStatus: 204,
+ allowHeaders: ['Authorization', 'X-Header'],
+ withCredentials: true,
+ maxAge: 36000,
+});

MODIFY
packages/@integration/test/integration.spec.ts
packages/@integration/test/integration.spec.ts
@@ -117,4 +117,22 @@ describe('API integration', () => {
request(server)
.get('/api/v1/static/img/flow.png')
.expect('Content-Type', ContentType.IMAGE_PNG));
+
+ test(`OPTIONS returns 204`, async () =>
+ request(app)
+ .options('/api/v2')
+ .set('origin', 'fake-origin')
+ .expect('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS')
+ .expect('Access-Control-Allow-Origin', 'fake-origin')
+ .expect('Access-Control-Allow-Headers', 'Authorization, X-Header')
+ .expect('Access-Control-Max-Age', '36000')
+ .expect('Access-Control-Allow-Credentials', 'true')
+ .expect(204));
+
+ test(`GET returns 200`, async () =>
+ request(app)
+ .get('/api/v2')
+ .set('origin', 'fake-origin')
+ .expect('Access-Control-Allow-Origin', 'fake-origin')
+ .expect(200));
});

tests(integration) Add cors middlawre tests