-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
basic.js
326 lines (296 loc) · 9.08 KB
/
basic.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use strict';
const assert = require('assert');
const fs = require('fs');
const { EventEmitter } = require('events');
const { StringDecoder } = require('string_decoder');
const getSetup = require('../support/setup');
const request = require('../support/client');
const doesntWorkInHttp2 = !process.env.HTTP2_TEST;
describe('[node] request', () => {
let setup;
let base;
before(async () => {
setup = await getSetup();
base = setup.uri;
});
describe('with an url', () => {
it('should preserve the encoding of the url', (done) => {
request.get(`${base}/url?a=(b%29`).end((error, res) => {
assert.equal('/url?a=(b%29', res.text);
done();
});
});
});
describe('with an object', () => {
it('should format the url', () =>
request.get(new URL(`${base}/login`)).then((res) => {
assert.ok(res.ok);
}));
});
describe('without a schema', () => {
it('should default to http', () =>
request.get(`${base}/login`).then((res) => {
assert.equal(res.status, 200);
}));
});
describe('res.toJSON()', () => {
it('should describe the response', () =>
request
.post(`${base}/echo`)
.send({ foo: 'baz' })
.then((res) => {
const object = res.toJSON();
assert.equal('object', typeof object.header);
assert.equal('object', typeof object.req);
assert.equal(200, object.status);
assert.equal('{"foo":"baz"}', object.text);
}));
});
describe('res.links', () => {
it('should default to an empty object', () =>
request.get(`${base}/login`).then((res) => {
assert.deepEqual(res.links, {});
}));
it('should parse the Link header field', (done) => {
request.get(`${base}/links`).end((error, res) => {
assert.equal(
res.links.next,
'https://api.github.com/repos/visionmedia/mocha/issues?page=2'
);
done();
});
});
});
describe('req.unset(field)', () => {
it('should remove the header field', (done) => {
request
.post(`${base}/echo`)
.unset('User-Agent')
.end((error, res) => {
assert.equal(res.header['user-agent'], undefined);
done();
});
});
});
describe('case-insensitive', () => {
it('should set/get header fields case-insensitively', () => {
const req = request.post(`${base}/echo`);
req.set('MiXeD', 'helloes');
assert.strictEqual(req.get('mixed'), 'helloes');
});
it('should unset header fields case-insensitively', () => {
const req = request.post(`${base}/echo`);
req.set('MiXeD', 'helloes');
req.unset('MIXED');
assert.strictEqual(req.get('mixed'), undefined);
});
});
describe('req.write(str)', () => {
it('should write the given data', (done) => {
const request_ = request.post(`${base}/echo`);
request_.set('Content-Type', 'application/json');
assert.equal('boolean', typeof request_.write('{"name"'));
assert.equal('boolean', typeof request_.write(':"tobi"}'));
request_.end((error, res) => {
assert.equal(res.text, '{"name":"tobi"}');
done();
});
});
});
describe('req.pipe(stream)', () => {
it('should pipe the response to the given stream', (done) => {
const stream = new EventEmitter();
let buf = '';
stream.writable = true;
stream.write = function (chunk) {
buf += chunk;
};
stream.end = function () {
assert.equal(buf, '{"name":"tobi"}');
done();
};
request.post(`${base}/echo`)
.send('{"name":"tobi"}')
.pipe(stream);
});
});
describe('.buffer()', () => {
it('should enable buffering', (done) => {
request
.get(`${base}/custom`)
.buffer()
.end((error, res) => {
assert.ifError(error);
assert.equal('custom stuff', res.text);
assert.ok(res.buffered);
done();
});
});
it("should take precedence over request.buffer['someMimeType'] = false", (done) => {
const type = 'application/barbaz';
const send = 'some text';
request.buffer[type] = false;
request
.post(`${base}/echo`)
.type(type)
.send(send)
.buffer()
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(res.type, type);
assert.equal(send, res.text);
assert.ok(res.buffered);
done();
});
});
});
describe('.buffer(false)', () => {
it('should disable buffering', (done) => {
request
.post(`${base}/echo`)
.type('application/x-dog')
.send('hello this is dog')
.buffer(false)
.end((error, res) => {
assert.ifError(error);
assert.equal(null, res.text);
assert.deepEqual(res.body, {});
let str = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
str += chunk;
});
res.on('end', () => {
assert.equal(str, 'hello this is dog');
done();
});
});
});
it("should take precedence over request.buffer['someMimeType'] = true", (done) => {
const type = 'application/foobar';
const send = 'hello this is a dog';
request.buffer[type] = true;
request
.post(`${base}/echo`)
.type(type)
.send(send)
.buffer(false)
.end((error, res) => {
delete request.buffer[type];
assert.ifError(error);
assert.equal(null, res.text);
assert.equal(res.type, type);
assert.equal(res.buffered, false);
assert.deepEqual(res.body, {});
let str = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
str += chunk;
});
res.on('end', () => {
assert.equal(str, send);
done();
});
});
});
});
describe('.withCredentials()', () => {
it('should not throw an error when using the client-side "withCredentials" method', (done) => {
request
.get(`${base}/custom`)
.withCredentials()
.end((error, res) => {
assert.ifError(error);
done();
});
});
});
describe('.agent()', () => {
it('should return the defaut agent', (done) => {
const agent = request.post(`${base}/echo`).agent();
assert.equal(agent, false);
done();
});
});
describe('.agent(undefined)', () => {
it('should set an agent to undefined and ensure it is chainable', (done) => {
const request_ = request.get(`${base}/echo`);
const returnValue = request_.agent(undefined);
assert.equal(returnValue, request_);
assert.strictEqual(request_.agent(), undefined);
done();
});
});
describe('.agent(new http.Agent())', () => {
it('should set passed agent', (done) => {
const http = require('http');
const request_ = request.get(`${base}/echo`);
const agent = new http.Agent();
const returnValue = request_.agent(agent);
assert.equal(returnValue, request_);
assert.equal(request_.agent(), agent);
done();
});
});
describe('with a content type other than application/json or text/*', () => {
it('should still use buffering', () => {
return request
.post(`${base}/echo`)
.type('application/x-dog')
.send('hello this is dog')
.then((res) => {
assert.equal(res.text, null);
assert.equal(res.body.toString(), 'hello this is dog');
assert.equal(res.buffered, true);
});
});
});
describe('content-length', () => {
it('should be set to the byte length of a non-buffer object', (done) => {
const decoder = new StringDecoder('utf8');
let img = fs.readFileSync(`${__dirname}/fixtures/test.png`);
img = decoder.write(img);
request
.post(`${base}/echo`)
.type('application/x-image')
.send(img)
.buffer(false)
.end((error, res) => {
assert.ifError(error);
assert.equal(res.buffered, false);
assert.equal(res.header['content-length'], Buffer.byteLength(img));
done();
});
});
it('should be set to the length of a buffer object', (done) => {
const img = fs.readFileSync(`${__dirname}/fixtures/test.png`);
request
.post(`${base}/echo`)
.type('application/x-image')
.send(img)
.buffer(true)
.end((error, res) => {
assert.ifError(error);
assert(res.buffered);
assert.equal(res.header['content-length'], img.length);
done();
});
});
});
if (doesntWorkInHttp2)
it('should send body with .get().send()', (next) => {
request
.get(`${base}/echo`)
.set('Content-Type', 'text/plain')
.send('wahoo')
.end((error, res) => {
try {
assert.equal(res.text, 'wahoo');
next();
} catch (err) {
next(err);
}
});
});
});