Skip to content

Commit e854118

Browse files
Merge branch '6118' of https://github.com/Sikora00/nest into Sikora00-6118
2 parents c48e2bc + ec97270 commit e854118

21 files changed

Lines changed: 600 additions & 145 deletions

File tree

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { NestFastifyApplication } from '@nestjs/platform-fastify';
2+
import { Test } from '@nestjs/testing';
3+
import * as request from 'supertest';
4+
import { AppModule } from '../src/app.module';
5+
6+
describe('Express Cors', () => {
7+
let app: NestFastifyApplication;
8+
const configs = [
9+
{
10+
origin: 'example.com',
11+
methods: 'GET',
12+
credentials: true,
13+
exposedHeaders: ['foo', 'bar'],
14+
allowedHeaders: ['baz', 'woo'],
15+
maxAge: 123,
16+
},
17+
{
18+
origin: 'sample.com',
19+
methods: 'GET',
20+
credentials: true,
21+
exposedHeaders: ['zoo', 'bar'],
22+
allowedHeaders: ['baz', 'foo'],
23+
maxAge: 321,
24+
},
25+
];
26+
describe('Dynamic config', () => {
27+
describe('enableCors', () => {
28+
before(async () => {
29+
const module = await Test.createTestingModule({
30+
imports: [AppModule],
31+
}).compile();
32+
33+
app = module.createNestApplication<NestFastifyApplication>();
34+
35+
let requestId = 0;
36+
const configDelegation = function (req, cb) {
37+
const config = configs[requestId];
38+
requestId++;
39+
cb(null, config);
40+
};
41+
app.enableCors(configDelegation);
42+
43+
await app.init();
44+
});
45+
46+
it(`Should add cors headers based on the first config`, async () => {
47+
return request(app.getHttpServer())
48+
.get('/')
49+
.expect('access-control-allow-origin', 'example.com')
50+
.expect('vary', 'Origin')
51+
.expect('access-control-allow-credentials', 'true')
52+
.expect('access-control-expose-headers', 'foo,bar')
53+
.expect('content-length', '0');
54+
});
55+
56+
it(`Should add cors headers based on the second config`, async () => {
57+
return request(app.getHttpServer())
58+
.options('/')
59+
.expect('access-control-allow-origin', 'sample.com')
60+
.expect('vary', 'Origin')
61+
.expect('access-control-allow-credentials', 'true')
62+
.expect('access-control-expose-headers', 'zoo,bar')
63+
.expect('access-control-allow-methods', 'GET')
64+
.expect('access-control-allow-headers', 'baz,foo')
65+
.expect('access-control-max-age', '321')
66+
.expect('content-length', '0');
67+
});
68+
69+
after(async () => {
70+
await app.close();
71+
});
72+
});
73+
74+
describe('Application Options', () => {
75+
before(async () => {
76+
const module = await Test.createTestingModule({
77+
imports: [AppModule],
78+
}).compile();
79+
80+
let requestId = 0;
81+
const configDelegation = function (req, cb) {
82+
const config = configs[requestId];
83+
requestId++;
84+
cb(null, config);
85+
};
86+
87+
app = module.createNestApplication<NestFastifyApplication>(null, {
88+
cors: configDelegation,
89+
});
90+
91+
await app.init();
92+
});
93+
94+
it(`Should add cors headers based on the first config`, async () => {
95+
return request(app.getHttpServer())
96+
.get('/')
97+
.expect('access-control-allow-origin', 'example.com')
98+
.expect('vary', 'Origin')
99+
.expect('access-control-allow-credentials', 'true')
100+
.expect('access-control-expose-headers', 'foo,bar')
101+
.expect('content-length', '0');
102+
});
103+
104+
it(`Should add cors headers based on the second config`, async () => {
105+
return request(app.getHttpServer())
106+
.options('/')
107+
.expect('access-control-allow-origin', 'sample.com')
108+
.expect('vary', 'Origin')
109+
.expect('access-control-allow-credentials', 'true')
110+
.expect('access-control-expose-headers', 'zoo,bar')
111+
.expect('access-control-allow-methods', 'GET')
112+
.expect('access-control-allow-headers', 'baz,foo')
113+
.expect('access-control-max-age', '321')
114+
.expect('content-length', '0');
115+
});
116+
117+
after(async () => {
118+
await app.close();
119+
});
120+
});
121+
});
122+
describe('Static config', () => {
123+
describe('enableCors', () => {
124+
before(async () => {
125+
const module = await Test.createTestingModule({
126+
imports: [AppModule],
127+
}).compile();
128+
129+
app = module.createNestApplication<NestFastifyApplication>();
130+
app.enableCors(configs[0]);
131+
132+
await app.init();
133+
});
134+
135+
it(`CORS headers`, async () => {
136+
return request(app.getHttpServer())
137+
.get('/')
138+
.expect('access-control-allow-origin', 'example.com')
139+
.expect('vary', 'Origin')
140+
.expect('access-control-allow-credentials', 'true')
141+
.expect('access-control-expose-headers', 'foo,bar')
142+
.expect('content-length', '0');
143+
});
144+
});
145+
146+
after(async () => {
147+
await app.close();
148+
});
149+
150+
describe('Application Options', () => {
151+
before(async () => {
152+
const module = await Test.createTestingModule({
153+
imports: [AppModule],
154+
}).compile();
155+
156+
app = module.createNestApplication<NestFastifyApplication>(null, {
157+
cors: configs[0],
158+
});
159+
await app.init();
160+
});
161+
162+
it(`CORS headers`, async () => {
163+
return request(app.getHttpServer())
164+
.get('/')
165+
.expect('access-control-allow-origin', 'example.com')
166+
.expect('vary', 'Origin')
167+
.expect('access-control-allow-credentials', 'true')
168+
.expect('access-control-expose-headers', 'foo,bar')
169+
.expect('content-length', '0');
170+
});
171+
172+
after(async () => {
173+
await app.close();
174+
});
175+
});
176+
});
177+
});
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { NestFastifyApplication } from '@nestjs/platform-fastify';
2+
import { Test } from '@nestjs/testing';
3+
import * as request from 'supertest';
4+
import { AppModule } from '../src/app.module';
5+
6+
describe('Fastify Cors', () => {
7+
let app: NestFastifyApplication;
8+
const configs = [
9+
{
10+
origin: 'example.com',
11+
methods: 'GET',
12+
credentials: true,
13+
exposedHeaders: ['foo', 'bar'],
14+
allowedHeaders: ['baz', 'woo'],
15+
maxAge: 123,
16+
},
17+
{
18+
origin: 'sample.com',
19+
methods: 'GET',
20+
credentials: true,
21+
exposedHeaders: ['zoo', 'bar'],
22+
allowedHeaders: ['baz', 'foo'],
23+
maxAge: 321,
24+
},
25+
];
26+
describe('Dynamic config', () => {
27+
describe('enableCors', () => {
28+
before(async () => {
29+
const module = await Test.createTestingModule({
30+
imports: [AppModule],
31+
}).compile();
32+
33+
app = module.createNestApplication<NestFastifyApplication>();
34+
35+
let requestId = 0;
36+
const configDelegation = function (req, cb) {
37+
const config = configs[requestId];
38+
requestId++;
39+
cb(null, config);
40+
};
41+
app.enableCors(configDelegation);
42+
43+
await app.init();
44+
});
45+
46+
it(`Should add cors headers based on the first config`, async () => {
47+
return request(app.getHttpServer())
48+
.get('/')
49+
.expect('access-control-allow-origin', 'example.com')
50+
.expect('vary', 'Origin')
51+
.expect('access-control-allow-credentials', 'true')
52+
.expect('access-control-expose-headers', 'foo,bar')
53+
.expect('content-length', '0');
54+
});
55+
56+
it(`Should add cors headers based on the second config`, async () => {
57+
return request(app.getHttpServer())
58+
.options('/')
59+
.expect('access-control-allow-origin', 'sample.com')
60+
.expect('vary', 'Origin')
61+
.expect('access-control-allow-credentials', 'true')
62+
.expect('access-control-expose-headers', 'zoo,bar')
63+
.expect('access-control-allow-methods', 'GET')
64+
.expect('access-control-allow-headers', 'baz,foo')
65+
.expect('access-control-max-age', '321')
66+
.expect('content-length', '0');
67+
});
68+
69+
after(async () => {
70+
await app.close();
71+
});
72+
});
73+
74+
describe('Application Options', () => {
75+
before(async () => {
76+
const module = await Test.createTestingModule({
77+
imports: [AppModule],
78+
}).compile();
79+
80+
let requestId = 0;
81+
const configDelegation = function (req, cb) {
82+
const config = configs[requestId];
83+
requestId++;
84+
cb(null, config);
85+
};
86+
87+
app = module.createNestApplication<NestFastifyApplication>(null, {
88+
cors: configDelegation,
89+
});
90+
91+
await app.init();
92+
});
93+
94+
it(`Should add cors headers based on the first config`, async () => {
95+
return request(app.getHttpServer())
96+
.get('/')
97+
.expect('access-control-allow-origin', 'example.com')
98+
.expect('vary', 'Origin')
99+
.expect('access-control-allow-credentials', 'true')
100+
.expect('access-control-expose-headers', 'foo,bar')
101+
.expect('content-length', '0');
102+
});
103+
104+
it(`Should add cors headers based on the second config`, async () => {
105+
return request(app.getHttpServer())
106+
.options('/')
107+
.expect('access-control-allow-origin', 'sample.com')
108+
.expect('vary', 'Origin')
109+
.expect('access-control-allow-credentials', 'true')
110+
.expect('access-control-expose-headers', 'zoo,bar')
111+
.expect('access-control-allow-methods', 'GET')
112+
.expect('access-control-allow-headers', 'baz,foo')
113+
.expect('access-control-max-age', '321')
114+
.expect('content-length', '0');
115+
});
116+
117+
after(async () => {
118+
await app.close();
119+
});
120+
});
121+
});
122+
123+
describe('Static config', () => {
124+
describe('enableCors', () => {
125+
before(async () => {
126+
const module = await Test.createTestingModule({
127+
imports: [AppModule],
128+
}).compile();
129+
130+
app = module.createNestApplication<NestFastifyApplication>();
131+
app.enableCors(configs[0]);
132+
133+
await app.init();
134+
});
135+
136+
it(`CORS headers`, async () => {
137+
return request(app.getHttpServer())
138+
.get('/')
139+
.expect('access-control-allow-origin', 'example.com')
140+
.expect('vary', 'Origin')
141+
.expect('access-control-allow-credentials', 'true')
142+
.expect('access-control-expose-headers', 'foo,bar')
143+
.expect('content-length', '0');
144+
});
145+
});
146+
147+
after(async () => {
148+
await app.close();
149+
});
150+
describe('Application Options', () => {
151+
before(async () => {
152+
const module = await Test.createTestingModule({
153+
imports: [AppModule],
154+
}).compile();
155+
156+
app = module.createNestApplication<NestFastifyApplication>(null, {
157+
cors: configs[0],
158+
});
159+
await app.init();
160+
});
161+
162+
it(`CORS headers`, async () => {
163+
return request(app.getHttpServer())
164+
.get('/')
165+
.expect('access-control-allow-origin', 'example.com')
166+
.expect('vary', 'Origin')
167+
.expect('access-control-allow-credentials', 'true')
168+
.expect('access-control-expose-headers', 'foo,bar')
169+
.expect('content-length', '0');
170+
});
171+
});
172+
173+
after(async () => {
174+
await app.close();
175+
});
176+
});
177+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
3+
@Controller()
4+
export class AppController {
5+
@Get()
6+
getGlobals() {
7+
return '';
8+
}
9+
}

integration/cors/src/app.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { AppController } from './app.controller';
3+
4+
@Module({
5+
controllers: [AppController],
6+
})
7+
export class AppModule {}

integration/cors/tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": false,
5+
"noImplicitAny": false,
6+
"removeComments": true,
7+
"noLib": false,
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"target": "es6",
11+
"sourceMap": true,
12+
"allowJs": true,
13+
"outDir": "./dist"
14+
},
15+
"include": [
16+
"src/**/*",
17+
"e2e/**/*"
18+
],
19+
"exclude": [
20+
"node_modules",
21+
]
22+
}

0 commit comments

Comments
 (0)