-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
buffers.js
120 lines (115 loc) · 3.18 KB
/
buffers.js
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
'use strict';
const assert = require('assert');
const request = require('../support/client');
const getSetup = require('../support/setup');
describe("req.buffer['someMimeType']", () => {
let setup;
let base;
before(async () => {
setup = await getSetup();
base = setup.uri;
});
it('should respect that agent.buffer(true) takes precedent', (done) => {
const agent = request.agent();
agent.buffer(true);
const type = 'application/somerandomtype';
const send = 'somerandomtext';
request.buffer[type] = false;
agent
.post(`${base}/echo`)
.type(type)
.send(send)
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(res.type, type);
assert.equal(send, res.text);
assert(res.buffered);
done();
});
});
it('should respect that agent.buffer(false) takes precedent', (done) => {
const agent = request.agent();
agent.buffer(false);
const type = 'application/barrr';
const send = 'some random text2';
request.buffer[type] = true;
agent
.post(`${base}/echo`)
.type(type)
.send(send)
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(null, res.text);
assert.equal(res.type, type);
assert(!res.buffered);
res.body.should.eql({});
let buf = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
buf += chunk;
});
res.on('end', () => {
buf.should.equal(send);
done();
});
});
});
it('should disable buffering for that mimetype when false', (done) => {
const type = 'application/bar';
const send = 'some random text';
request.buffer[type] = false;
request
.post(`${base}/echo`)
.type(type)
.send(send)
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(null, res.text);
assert.equal(res.type, type);
assert(!res.buffered);
res.body.should.eql({});
let buf = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
buf += chunk;
});
res.on('end', () => {
buf.should.equal(send);
done();
});
});
});
it('should enable buffering for that mimetype when true', (done) => {
const type = 'application/baz';
const send = 'woooo';
request.buffer[type] = true;
request
.post(`${base}/echo`)
.type(type)
.send(send)
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(res.type, type);
assert.equal(send, res.text);
assert(res.buffered);
done();
});
});
it('should fallback to default handling for that mimetype when undefined', () => {
const type = 'application/bazzz';
const send = 'woooooo';
return request
.post(`${base}/echo`)
.type(type)
.send(send)
.then((res) => {
assert.equal(res.type, type);
assert.equal(send, res.body.toString());
assert(res.buffered);
});
});
});