Skip to content

Commit 95f098e

Browse files
authored
Fix/ci windows CD break from #1739 (#1799)
* fix(dep): express type * fix: race conditions * refactor(test): centralize integration utils methods * chore(test): adjust timeout in file upload cases * refactor(test): request verify methods * chore(test): longer timeout for Koa * chore(test): type hint
1 parent 03f3732 commit 95f098e

18 files changed

+573
-904
lines changed

tests/fixtures/express-router-with-custom-multer/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import '../controllers/rootController';
55

66
import { RegisterRoutes } from './routes';
77

8+
9+
810
export const app: express.Express = express();
911
export const router = express.Router();
1012
app.use('/v1', router);

tests/integration/dynamic-controllers-express-server.spec.ts

Lines changed: 79 additions & 81 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
11
import { expect } from 'chai';
22
import 'mocha';
3-
import * as request from 'supertest';
43
import { app } from '../fixtures/express-router/server';
54
import { TestModel } from '../fixtures/testModel';
6-
import TestAgent = require('supertest/lib/agent');
5+
import { verifyGetRequest } from './utils';
76

87
const basePath = '/v1';
98

109
describe('Express Router Server', () => {
1110
it('can handle get request to root controller`s path', () => {
12-
return verifyGetRequest(basePath + '/', (err, res) => {
11+
return verifyGetRequest(app, basePath + '/', (err, res) => {
1312
const model = res.body as TestModel;
1413
expect(model.id).to.equal(1);
1514
});
1615
});
1716

1817
it('can handle get request to root controller`s method path', () => {
19-
return verifyGetRequest(basePath + '/rootControllerMethodWithPath', (err, res) => {
18+
return verifyGetRequest(app, basePath + '/rootControllerMethodWithPath', (err, res) => {
2019
const model = res.body as TestModel;
2120
expect(model.id).to.equal(1);
2221
});
2322
});
24-
25-
function verifyGetRequest(path: string, verifyResponse: (err: any, res: request.Response) => any, expectedStatus?: number) {
26-
return verifyRequest(verifyResponse, request => request.get(path), expectedStatus);
27-
}
28-
29-
function verifyRequest(verifyResponse: (err: any, res: request.Response) => any, methodOperation: (request: TestAgent<request.Test>) => request.Test, expectedStatus = 200) {
30-
return new Promise<void>((resolve, reject) => {
31-
methodOperation(request(app))
32-
.expect(expectedStatus)
33-
.end((err: any, res: any) => {
34-
let parsedError: any;
35-
try {
36-
parsedError = JSON.parse(res.error);
37-
} catch (err) {
38-
parsedError = res?.error;
39-
}
40-
41-
if (err) {
42-
verifyResponse(err, res);
43-
reject({
44-
error: err,
45-
response: parsedError,
46-
});
47-
return;
48-
}
49-
50-
verifyResponse(parsedError, res);
51-
resolve();
52-
});
53-
});
54-
}
5523
});

tests/integration/express-server-custom-multer.spec.ts

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import { expect } from 'chai';
33
import { readFileSync } from 'fs';
44
import 'mocha';
55
import { resolve } from 'path';
6-
import * as request from 'supertest';
76
import { app } from '../fixtures/express/server';
8-
import TestAgent = require('supertest/lib/agent');
7+
8+
import { verifyFileUploadRequest } from './utils';
99

1010
const basePath = '/v1';
1111

1212
describe('Express Server With custom multer', () => {
13-
describe('file upload With custom multer instance', () => {
13+
describe('file upload With custom multer instance', function () {
14+
this.timeout(15_000);
15+
1416
it('can post a file', () => {
1517
const formData = { someFile: '@../package.json' };
16-
return verifyFileUploadRequest(basePath + '/PostTest/File', formData, (_err, res) => {
18+
return verifyFileUploadRequest(app, basePath + '/PostTest/File', formData, (_err, res) => {
1719
const packageJsonBuffer = readFileSync(resolve(__dirname, '../package.json'));
1820
const returnedBuffer = Buffer.from(res.body.buffer);
1921
expect(res.body).to.not.be.undefined;
@@ -27,31 +29,31 @@ describe('Express Server With custom multer', () => {
2729

2830
it('can post a file without name', () => {
2931
const formData = { aFile: '@../package.json' };
30-
return verifyFileUploadRequest(basePath + '/PostTest/FileWithoutName', formData, (_err, res) => {
32+
return verifyFileUploadRequest(app, basePath + '/PostTest/FileWithoutName', formData, (_err, res) => {
3133
expect(res.body).to.not.be.undefined;
3234
expect(res.body.fieldname).to.equal('aFile');
3335
});
3436
});
3537

3638
it('cannot post a file with wrong attribute name', async () => {
3739
const formData = { wrongAttributeName: '@../package.json' };
38-
verifyFileUploadRequest(basePath + '/PostTest/File', formData, (_err, res) => {
40+
verifyFileUploadRequest(app, basePath + '/PostTest/File', formData, (_err, res) => {
3941
expect(res.status).to.equal(500);
4042
expect(res.text).to.equal('{"message":"Unexpected field","name":"MulterError","status":500}');
4143
});
4244
});
4345

4446
it('cannot post a file with no file', async () => {
4547
const formData = { notAFileAttribute: 'not a file' };
46-
verifyFileUploadRequest(basePath + '/PostTest/File', formData, (_err, res) => {
48+
verifyFileUploadRequest(app, basePath + '/PostTest/File', formData, (_err, res) => {
4749
expect(res.status).to.equal(400);
4850
expect(res.text).to.equal('{"fields":{"someFile":{"message":"\'someFile\' is required"}},"message":"An error occurred during the request.","name":"ValidateError","status":400}');
4951
});
5052
});
5153

5254
it('can post a file with no file', async () => {
5355
const formData = { notAFileAttribute: 'not a file' };
54-
verifyFileUploadRequest(basePath + '/PostTest/FileOptional', formData, (_err, res) => {
56+
verifyFileUploadRequest(app, basePath + '/PostTest/FileOptional', formData, (_err, res) => {
5557
expect(res.status).to.equal(200);
5658
expect(res.text).to.equal('no file');
5759
});
@@ -64,7 +66,7 @@ describe('Express Server With custom multer', () => {
6466
someFiles: ['@../package.json', '@../tsconfig.json'],
6567
};
6668

67-
return verifyFileUploadRequest(basePath + '/PostTest/ManyFilesAndFormFields', formData, (_err, res) => {
69+
return verifyFileUploadRequest(app, basePath + '/PostTest/ManyFilesAndFormFields', formData, (_err, res) => {
6870
for (const file of res.body as File[]) {
6971
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
7072
const returnedBuffer = Buffer.from(file.buffer);
@@ -85,7 +87,7 @@ describe('Express Server With custom multer', () => {
8587
someFiles: ['@../package.json'],
8688
};
8789

88-
return verifyFileUploadRequest(basePath + '/PostTest/ManyFilesAndFormFields', formData, (_err, res) => {
90+
return verifyFileUploadRequest(app, basePath + '/PostTest/ManyFilesAndFormFields', formData, (_err, res) => {
8991
expect(res.body).to.be.length(1);
9092
});
9193
});
@@ -95,7 +97,7 @@ describe('Express Server With custom multer', () => {
9597
file_a: '@../package.json',
9698
file_b: '@../tsconfig.json',
9799
};
98-
return verifyFileUploadRequest(`${basePath}/PostTest/ManyFilesInDifferentFields`, formData, (_err, res) => {
100+
return verifyFileUploadRequest(app, `${basePath}/PostTest/ManyFilesInDifferentFields`, formData, (_err, res) => {
99101
for (const file of res.body as File[]) {
100102
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
101103
const returnedBuffer = Buffer.from(file.buffer);
@@ -115,7 +117,7 @@ describe('Express Server With custom multer', () => {
115117
file_b: '@../tsoa.json',
116118
files_c: ['@../tsconfig.json', '@../package.json'],
117119
};
118-
return verifyFileUploadRequest(`${basePath}/PostTest/ManyFilesInDifferentArrayFields`, formData, (_err, res) => {
120+
return verifyFileUploadRequest(app, `${basePath}/PostTest/ManyFilesInDifferentArrayFields`, formData, (_err, res) => {
119121
for (const fileList of res.body as File[][]) {
120122
for (const file of fileList) {
121123
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
@@ -136,7 +138,7 @@ describe('Express Server With custom multer', () => {
136138
username: 'test',
137139
avatar: '@../tsconfig.json',
138140
};
139-
return verifyFileUploadRequest(`${basePath}/PostTest/MixedFormDataWithFilesContainsOptionalFile`, formData, (_err, res) => {
141+
return verifyFileUploadRequest(app, `${basePath}/PostTest/MixedFormDataWithFilesContainsOptionalFile`, formData, (_err, res) => {
140142
const file = res.body.avatar;
141143
const packageJsonBuffer = readFileSync(resolve(__dirname, `../${file.originalname}`));
142144
const returnedBuffer = Buffer.from(file.buffer);
@@ -157,7 +159,7 @@ describe('Express Server With custom multer', () => {
157159
avatar: '@../tsconfig.json',
158160
optionalAvatar: '@../package.json',
159161
};
160-
return verifyFileUploadRequest(`${basePath}/PostTest/MixedFormDataWithFilesContainsOptionalFile`, formData, (_err, res) => {
162+
return verifyFileUploadRequest(app, `${basePath}/PostTest/MixedFormDataWithFilesContainsOptionalFile`, formData, (_err, res) => {
161163
expect(res.body.username).to.equal(formData.username);
162164
for (const fieldName of ['avatar', 'optionalAvatar']) {
163165
const file = res.body[fieldName];
@@ -172,58 +174,5 @@ describe('Express Server With custom multer', () => {
172174
}
173175
});
174176
});
175-
176-
function verifyFileUploadRequest(
177-
path: string,
178-
formData: any,
179-
verifyResponse: (err: any, res: request.Response) => any = () => {
180-
/**/
181-
},
182-
expectedStatus?: number,
183-
) {
184-
return verifyRequest(
185-
verifyResponse,
186-
request =>
187-
Object.keys(formData).reduce((req, key) => {
188-
const values = [].concat(formData[key]);
189-
values.forEach((v: string) => {
190-
if (v.startsWith('@')) {
191-
req.attach(key, resolve(__dirname, v.slice(1)));
192-
} else {
193-
req.field(key, v);
194-
}
195-
});
196-
return req;
197-
}, request.post(path)),
198-
expectedStatus,
199-
);
200-
}
201177
});
202-
203-
function verifyRequest(verifyResponse: (err: any, res: request.Response) => any, methodOperation: (request: TestAgent<request.Test>) => request.Test, expectedStatus = 200) {
204-
return new Promise<void>((resolve, reject) => {
205-
methodOperation(request(app))
206-
.expect(expectedStatus)
207-
.end((err: any, res: any) => {
208-
let parsedError: any;
209-
try {
210-
parsedError = JSON.parse(res.error);
211-
} catch (err) {
212-
parsedError = res?.error;
213-
}
214-
215-
if (err) {
216-
verifyResponse(err, res);
217-
reject({
218-
error: err,
219-
response: parsedError,
220-
});
221-
return;
222-
}
223-
224-
verifyResponse(parsedError, res);
225-
resolve();
226-
});
227-
});
228-
}
229178
});
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { expect } from 'chai';
22
import 'mocha';
3-
import * as request from 'supertest';
43
import { app } from '../fixtures/express-root-security/server';
54
import { TestModel, UserResponseModel } from '../fixtures/testModel';
6-
import TestAgent = require('supertest/lib/agent');
5+
import { verifyGetRequest } from './utils';
76

87
const basePath = '/v1';
98

@@ -14,14 +13,14 @@ describe('Express Server with api_key Root Security', () => {
1413
};
1514

1615
it('returns a model if the correct API key is given', () => {
17-
return verifyGetRequest(basePath + '/Current?access_token=abc123456', (_err, res) => {
16+
return verifyGetRequest(app, basePath + '/Current?access_token=abc123456', (_err, res) => {
1817
const model = res.body as TestModel;
1918
expect(model.id).to.equal(1);
2019
});
2120
});
2221

2322
it('returns 401 for an invalid key', () => {
24-
return verifyGetRequest(basePath + '/Current?access_token=invalid', emptyHandler, 401);
23+
return verifyGetRequest(app, basePath + '/Current?access_token=invalid', emptyHandler, 401);
2524
});
2625
});
2726

@@ -31,54 +30,23 @@ describe('Express Server with api_key Root Security', () => {
3130
};
3231

3332
it('returns a model without auth for a request with undefined method security', () => {
34-
return verifyGetRequest(basePath + '/NoSecurity/UndefinedSecurity', (_err, res) => {
33+
return verifyGetRequest(app, basePath + '/NoSecurity/UndefinedSecurity', (_err, res) => {
3534
const model = res.body as UserResponseModel;
3635
expect(model.id).to.equal(1);
3736
});
3837
});
3938

4039
describe('method with @Security(api_key)', () => {
4140
it('returns 401 for an invalid key', () => {
42-
return verifyGetRequest(basePath + '/NoSecurity?access_token=invalid', emptyHandler, 401);
41+
return verifyGetRequest(app, basePath + '/NoSecurity?access_token=invalid', emptyHandler, 401);
4342
});
4443

4544
it('returns a model with a valid key', () => {
46-
return verifyGetRequest(basePath + '/NoSecurity?access_token=abc123456', (_err, res) => {
45+
return verifyGetRequest(app, basePath + '/NoSecurity?access_token=abc123456', (_err, res) => {
4746
const model = res.body as UserResponseModel;
4847
expect(model.id).to.equal(1);
4948
});
5049
});
5150
});
5251
});
53-
54-
function verifyGetRequest(path: string, verifyResponse: (err: any, res: request.Response) => any, expectedStatus?: number) {
55-
return verifyRequest(verifyResponse, request => request.get(path), expectedStatus);
56-
}
57-
58-
function verifyRequest(verifyResponse: (err: any, res: request.Response) => any, methodOperation: (request: TestAgent<request.Test>) => request.Test, expectedStatus = 200) {
59-
return new Promise<void>((resolve, reject) => {
60-
methodOperation(request(app))
61-
.expect(expectedStatus)
62-
.end((err: any, res: any) => {
63-
let parsedError: any;
64-
try {
65-
parsedError = JSON.parse(res.error);
66-
} catch (err) {
67-
parsedError = res?.error;
68-
}
69-
70-
if (err) {
71-
verifyResponse(err, res);
72-
reject({
73-
error: err,
74-
response: parsedError,
75-
});
76-
return;
77-
}
78-
79-
verifyResponse(parsedError, res);
80-
resolve();
81-
});
82-
});
83-
}
8452
});

0 commit comments

Comments
 (0)