Skip to content

Commit 8d9ed4f

Browse files
committed
adds tests
1 parent a0238fa commit 8d9ed4f

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/test-http-body.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,85 @@ test('Provides HTTP POST empty body as empty string parameter and rawBody', t =>
135135
});
136136
}, errHandler(t));
137137
});
138+
139+
test('Rejects HTTP POST exceeding bodyLimit', t => {
140+
const largeBody = 'x'.repeat(1048576 + 1);
141+
t.plan(2);
142+
start((_, receivedBody) => receivedBody, { bodyLimit: 1048576 })
143+
.then(server => {
144+
request(server)
145+
.post('/')
146+
.send(largeBody)
147+
.set({ 'Content-Type': 'text/plain' })
148+
.expect(413)
149+
.end((err, res) => {
150+
t.error(err, 'No error');
151+
t.equal(res.status, 413, 'Should reject payload larger than limit');
152+
server.close();
153+
});
154+
}, errHandler(t));
155+
});
156+
157+
test('Accepts HTTP POST within bodyLimit', t => {
158+
const body = 'x'.repeat(1048576);
159+
t.plan(2);
160+
start((_, receivedBody) => {
161+
t.equal(receivedBody, body);
162+
return receivedBody;
163+
}, { bodyLimit: 1048576 })
164+
.then(server => {
165+
request(server)
166+
.post('/')
167+
.send(body)
168+
.set({ 'Content-Type': 'text/plain' })
169+
.expect(200)
170+
.end((err, _) => {
171+
t.error(err, 'No error');
172+
server.close();
173+
});
174+
}, errHandler(t));
175+
});
176+
177+
test('Rejects payload exceeding environment variable bodyLimit', t => {
178+
const bodyLimit = 1048576;
179+
const largeBody = 'x'.repeat(bodyLimit + 1);
180+
process.env.FUNC_BODY_LIMIT = bodyLimit;
181+
t.plan(2);
182+
start((_, receivedBody) => receivedBody)
183+
.then(server => {
184+
request(server)
185+
.post('/')
186+
.send(largeBody)
187+
.set({ 'Content-Type': 'text/plain' })
188+
.expect(413)
189+
.end((err, res) => {
190+
t.error(err, 'No error');
191+
t.equal(res.status, 413, 'Should reject payload larger than env var limit');
192+
delete process.env.FUNC_BODY_LIMIT;
193+
server.close();
194+
});
195+
}, errHandler(t));
196+
});
197+
198+
test('Accepts payload within environment variable bodyLimit', t => {
199+
const bodyLimit = 524288;
200+
const body = 'x'.repeat(bodyLimit);
201+
process.env.FUNC_BODY_LIMIT = bodyLimit;
202+
t.plan(2);
203+
start((_, receivedBody) => {
204+
t.equal(receivedBody, body);
205+
return receivedBody;
206+
})
207+
.then(server => {
208+
request(server)
209+
.post('/')
210+
.send(body)
211+
.set({ 'Content-Type': 'text/plain' })
212+
.expect(200)
213+
.end((err, _) => {
214+
t.error(err, 'No error');
215+
delete process.env.FUNC_BODY_LIMIT;
216+
server.close();
217+
});
218+
}, errHandler(t));
219+
});

0 commit comments

Comments
 (0)