forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.spec.ts
71 lines (66 loc) 路 2.17 KB
/
express.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
ExpressAdapter,
NestExpressApplication,
} from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import { readFileSync } from 'fs';
import { join } from 'path';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
const readme = readFileSync(join(process.cwd(), 'Readme.md'));
const readmeString = readme.toString();
describe('Express FileSend', () => {
let app: NestExpressApplication;
beforeEach(async () => {
const modRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = modRef.createNestApplication(new ExpressAdapter());
await app.init();
});
it('should return a file from a stream', async () => {
return request(app.getHttpServer())
.get('/file/stream/')
.expect(200)
.expect(res => {
expect(res.body.toString()).to.be.eq(readmeString);
});
});
it('should return a file from a buffer', async () => {
return request(app.getHttpServer())
.get('/file/buffer')
.expect(200)
.expect(res => {
expect(res.body.toString()).to.be.eq(readmeString);
});
});
it('should not stream a non-file', async () => {
return request(app.getHttpServer())
.get('/non-file/pipe-method')
.expect(200)
.expect({ value: 'Hello world' });
});
it('should return a file from an RxJS stream', async () => {
return request(app.getHttpServer())
.get('/file/rxjs/stream/')
.expect(200)
.expect(res => {
expect(res.body.toString()).to.be.eq(readmeString);
});
});
it('should return a file with correct headers', async () => {
return request(app.getHttpServer())
.get('/file/with/headers')
.expect(200)
.expect('Content-Type', 'text/markdown')
.expect('Content-Disposition', 'attachment; filename="Readme.md"')
.expect('Content-Length', readme.byteLength.toString())
.expect(res => {
expect(res.text).to.be.eq(readmeString);
});
});
it('should return an error if the file does not exist', async () => {
return request(app.getHttpServer()).get('/file/not/exist').expect(400);
});
});