-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
multipart.spec.ts
191 lines (174 loc) · 5.6 KB
/
multipart.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as express from 'express';
import * as path from 'path';
import * as fs from 'fs';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';
describe('a multipart request', () => {
let app;
const fileNames = [];
before(async () => {
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
app = await createApp(
{
apiSpec,
fileUploader: {
fileFilter: (req, file, cb) => {
fileNames.push(file.originalname);
cb(null, true);
},
},
},
3003,
(app) =>
app.use(
`${app.basePath}`,
express
.Router()
.post(`/sample_2`, (req, res) => {
const files = req.files;
res.status(200).json({
files,
metadata: req.body.metadata,
});
})
.post(`/sample_*`, (req, res) => res.json(req.body)),
),
);
});
beforeEach(() => {
fileNames.length = 0;
});
after(() => {
(<any>app).server.close();
});
describe('that contains $refs', () => {
it('should validate a request body with a schemaObject $ref', async () =>
request(app)
.post(`${app.basePath}/sample_4`)
.set('Content-Type', 'multipart/form-data')
.attach('image', 'package.json')
.expect(200));
it('should validate a requestBody $ref', async () =>
request(app)
.post(`${app.basePath}/sample_5`)
.set('Content-Type', 'multipart/form-data')
.attach('image', 'package.json')
.expect(200));
it('should validate a requestBody $ref that contains a schemaObject $ref', async () =>
request(app)
.post(`${app.basePath}/sample_6`)
.set('Content-Type', 'multipart/form-data')
.attach('image', 'package.json')
.expect(200));
});
describe('that is malformed or not defined', () => {
it('should throw 400 when required multipart file field', async () =>
request(app)
.post(`${app.basePath}/sample_2`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.expect(400)
.then((e) => {
expect(e.body).has.property('errors').with.length(1);
expect(e.body.errors[0])
.has.property('message')
.equal('multipart file(s) required');
}));
it('should throw 400 when required form field is missing during multipart upload', async () =>
request(app)
.post(`${app.basePath}/sample_2`)
.set('Content-Type', 'multipart/sample_2')
.set('Accept', 'application/json')
.attach('file', 'package.json')
.expect(400));
it('should throw 405 get method not allowed', async () =>
request(app)
.get(`${app.basePath}/sample_2`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.attach('file', 'package.json')
.field('metadata', 'some-metadata')
.expect(405));
it('should throw 415 unsupported media type', async () =>
request(app)
.post(`${app.basePath}/sample_2`)
.send({ test: 'test' })
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(415)
.then((r) => {
expect(r.body).has.property('errors').with.length(1);
expect(r.body.errors[0])
.has.property('message')
.equal('unsupported media type application/json');
}));
});
describe('that is well formed', () => {
it('should validate application/octet-stream file and metadata', (done) => {
const testImage = `${__dirname}/assets/image.png`;
const req = request(app)
.post(`${app.basePath}/sample_3`)
.set('content-type', 'application/octet-stream');
const imgStream = fs.createReadStream(testImage);
imgStream.on('end', () => req.end(done));
imgStream.pipe(<any>req, { end: false });
});
it('should validate multipart file and metadata', async () => {
const array_with_objects = JSON.stringify([
{
foo: 'bar'
}
]);
await request(app)
.post(`${app.basePath}/sample_2`)
.set('Content-Type', 'multipart/form-data')
.set('Accept', 'application/json')
.attach('file', 'package.json')
.field('array_with_objects', array_with_objects)
.field('metadata', 'some-metadata')
.expect(200)
.then((r) => {
const b = r.body;
expect(b.files).to.be.an('array').with.length(1);
expect(b.files[0]).to.have.property('fieldname').to.equal('file');
expect(b.metadata).to.equal('some-metadata');
});
expect(fileNames).to.deep.equal(['package.json']);
});
});
});
describe('when request does not use parsers', () => {
let app;
after(() => {
(<any>app).server.close();
});
before(async () => {
const apiSpec = path.join('test', 'resources', 'multipart.yaml');
app = await createApp(
{
apiSpec,
},
3004,
(app) =>
app.use(
`${app.basePath}`,
express
.Router()
.post(`/sample_7`, (req, res) => res.json('ok')),
),
false,
false,
);
});
it('should validate that endpoint exists', async () => {
await request(app)
.post(`${app.basePath}/sample_7`)
.set('Content-Type', 'multipart/form-data')
.expect(200)
.then((r) => {
expect(r.body).to.equal('ok');
});
});
});