@adaptivestone/framework 4.11.1 → 4.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/services/http/middleware/Cors.js +15 -14
- package/services/http/middleware/Cors.test.js +16 -4
- package/tests/setup.js +1 -3
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -14,28 +14,29 @@ class Cors extends AbstractMiddleware {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
async middleware(req, res, next) {
|
|
17
|
-
if (req.method !== 'OPTIONS') {
|
|
18
|
-
// only get supported
|
|
19
|
-
return next();
|
|
20
|
-
}
|
|
21
17
|
for (const host of this.params.origins) {
|
|
22
18
|
if (
|
|
23
19
|
(typeof host === 'string' && req.headers.origin === host) ||
|
|
24
20
|
(host instanceof RegExp && host.test(req.headers.origin))
|
|
25
21
|
) {
|
|
26
22
|
res.set('Access-Control-Allow-Origin', req.headers.origin);
|
|
27
|
-
res.set(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
res.set('Vary', 'Origin');
|
|
24
|
+
|
|
25
|
+
if (req.method === 'OPTIONS') {
|
|
26
|
+
res.set(
|
|
27
|
+
'Access-Control-Allow-Methods',
|
|
28
|
+
'GET,HEAD,PUT,PATCH,POST,DELETE',
|
|
29
|
+
);
|
|
30
|
+
res.set('Vary', 'Origin, Access-Control-Request-Headers');
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const allowedHeaders = req.headers['access-control-request-headers'];
|
|
33
|
+
if (allowedHeaders) {
|
|
34
|
+
res.set('Access-Control-Allow-Headers', allowedHeaders);
|
|
35
|
+
}
|
|
36
|
+
res.set('Content-Length', '0');
|
|
37
|
+
res.status(204);
|
|
38
|
+
return res.end();
|
|
36
39
|
}
|
|
37
|
-
res.status(204);
|
|
38
|
-
return res.end();
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
return next();
|
|
@@ -28,21 +28,30 @@ describe('cors middleware methods', () => {
|
|
|
28
28
|
expect(() => new Cors(global.server.app, { origins: 'origins' })).toThrow();
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
it('non options should be
|
|
32
|
-
expect.assertions(
|
|
31
|
+
it('non options should be different', async () => {
|
|
32
|
+
expect.assertions(2);
|
|
33
33
|
let isCalled = false;
|
|
34
34
|
const nextFunction = () => {
|
|
35
35
|
isCalled = true;
|
|
36
36
|
};
|
|
37
|
+
const map = new Map();
|
|
37
38
|
const req = {
|
|
38
39
|
method: 'GET',
|
|
40
|
+
|
|
41
|
+
headers: { origin: 'https://localhost' },
|
|
42
|
+
};
|
|
43
|
+
const res = {
|
|
44
|
+
set: (key, val) => {
|
|
45
|
+
map.set(key, val);
|
|
46
|
+
},
|
|
39
47
|
};
|
|
40
48
|
const middleware = new Cors(global.server.app, {
|
|
41
|
-
origins: ['
|
|
49
|
+
origins: ['https://localhost'],
|
|
42
50
|
});
|
|
43
51
|
|
|
44
|
-
await middleware.middleware(req,
|
|
52
|
+
await middleware.middleware(req, res, nextFunction);
|
|
45
53
|
expect(isCalled).toBeTruthy();
|
|
54
|
+
expect(map.get('Vary')).toBe('Origin');
|
|
46
55
|
});
|
|
47
56
|
|
|
48
57
|
it('host the not match origin', async () => {
|
|
@@ -78,6 +87,7 @@ describe('cors middleware methods', () => {
|
|
|
78
87
|
set: (key, val) => {
|
|
79
88
|
map.set(key, val);
|
|
80
89
|
},
|
|
90
|
+
status: () => {},
|
|
81
91
|
end: () => {
|
|
82
92
|
isEndCalled = true;
|
|
83
93
|
},
|
|
@@ -113,6 +123,8 @@ describe('cors middleware methods', () => {
|
|
|
113
123
|
set: (key, val) => {
|
|
114
124
|
map.set(key, val);
|
|
115
125
|
},
|
|
126
|
+
status: () => {},
|
|
127
|
+
|
|
116
128
|
end: () => {
|
|
117
129
|
isEndCalled = true;
|
|
118
130
|
},
|
package/tests/setup.js
CHANGED
|
@@ -107,13 +107,11 @@ afterAll(async () => {
|
|
|
107
107
|
global.server.app.httpServer.shutdown();
|
|
108
108
|
global.server.app.events.emit('shutdown');
|
|
109
109
|
}
|
|
110
|
-
|
|
110
|
+
|
|
111
111
|
if (typeof global.testSetup.afterAll === 'function') {
|
|
112
112
|
await global.testSetup.afterAll();
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
await mongoose.disconnect();
|
|
116
116
|
await mongoMemoryServerInstance.stop();
|
|
117
|
-
|
|
118
|
-
// }, 2000);
|
|
119
117
|
});
|