forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
244 lines (220 loc) · 6.36 KB
/
basic.js
File metadata and controls
244 lines (220 loc) · 6.36 KB
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
var EventEmitter = require('events').EventEmitter;
var assert = require('better-assert');
var url = require('url');
var request = require('../../');
describe('[node] request', function(){
describe('with an object', function(){
it('should format the url', function(done){
request
.get(url.parse('http://localhost:5000/login'))
.end(function(err, res){
assert(res.ok);
done();
})
})
})
describe('without a schema', function(){
it('should default to http', function(done){
request
.get('localhost:5000/login')
.end(function(err, res){
assert(res.status == 200);
done();
})
})
})
describe('req.toJSON()', function(){
it('should describe the request', function(done){
request
.post(':5000/echo')
.send({ foo: 'baz' })
.end(function(err, res){
var obj = res.request.toJSON();
assert('POST' == obj.method);
assert(':5000/echo' == obj.url);
assert('baz' == obj.data.foo);
done();
});
})
})
describe('should allow the send shorthand', function() {
it('with callback in the method call', function(done) {
request
.get('http://localhost:5000/login', function(err, res) {
assert(res.status == 200);
done();
});
})
it('with data in the method call', function(done) {
request
.post('http://localhost:5000/echo', { foo: 'bar' })
.end(function(err, res) {
assert('{"foo":"bar"}' == res.text);
done();
});
})
it('with callback and data in the method call', function(done) {
request
.post('http://localhost:5000/echo', { foo: 'bar' }, function(err, res) {
assert('{"foo":"bar"}' == res.text);
done();
});
})
})
describe('res.toJSON()', function(){
it('should describe the response', function(done){
request
.post('http://localhost:5000/echo')
.send({ foo: 'baz' })
.end(function(err, res){
var obj = res.toJSON();
assert('object' == typeof obj.header);
assert('object' == typeof obj.req);
assert(200 == obj.status);
assert('{"foo":"baz"}' == obj.text);
done();
});
});
})
describe('res.links', function(){
it('should default to an empty object', function(done){
request
.get('http://localhost:5000/login')
.end(function(err, res){
res.links.should.eql({});
done();
})
})
it('should parse the Link header field', function(done){
request
.get('http://localhost:5000/links')
.end(function(err, res){
res.links.next.should.equal('https://api.github.com/repos/visionmedia/mocha/issues?page=2');
done();
})
})
})
describe('req.unset(field)', function(){
it('should remove the header field', function(done){
request
.post('http://localhost:5000/echo')
.unset('User-Agent')
.end(function(err, res){
assert(void 0 == res.header['user-agent']);
done();
})
})
})
describe('req.write(str)', function(){
it('should write the given data', function(done){
var req = request.post('http://localhost:5000/echo');
req.set('Content-Type', 'application/json');
req.write('{"name"').should.be.a.boolean;
req.write(':"tobi"}').should.be.a.boolean;
req.end(function(err, res){
res.text.should.equal('{"name":"tobi"}');
done();
});
})
})
describe('req.pipe(stream)', function(){
it('should pipe the response to the given stream', function(done){
var stream = new EventEmitter;
stream.buf = '';
stream.writable = true;
stream.write = function(chunk){
this.buf += chunk;
};
stream.end = function(){
this.buf.should.equal('{"name":"tobi"}');
done();
};
request
.post('http://localhost:5000/echo')
.send('{"name":"tobi"}')
.pipe(stream);
})
})
describe('.buffer()', function(){
it('should enable buffering', function(done){
request
.get('http://localhost:5000/custom')
.buffer()
.end(function(err, res){
assert(null == err);
assert('custom stuff' == res.text);
assert(res.buffered);
done();
});
})
})
describe('.buffer(false)', function(){
it('should disable buffering', function(done){
request
.post('http://localhost:5000/echo')
.type('application/x-dog')
.send('hello this is dog')
.buffer(false)
.end(function(err, res){
assert(null == err);
assert(null == res.text);
res.body.should.eql({});
var buf = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk });
res.on('end', function(){
buf.should.equal('hello this is dog');
done();
});
});
})
})
describe('.agent()', function(){
it('should return the defaut agent', function(done){
var req = request.post('http://localhost:5000/echo');
req.agent().should.equal(false);
done();
})
})
describe('.agent(undefined)', function(){
it('should set an agent to undefined and ensure it is chainable', function(done){
var req = request.get();
var ret = req.agent(undefined);
ret.should.equal(req);
assert(req.agent() === undefined);
done();
})
})
describe('.agent(new http.Agent())', function(){
it('should set passed agent', function(done){
var http = require('http');
var req = request.get();
var agent = new http.Agent();
var ret = req.agent(agent);
ret.should.equal(req);
req.agent().should.equal(agent)
done();
})
})
describe('with a content type other than application/json or text/*', function(){
it('should disable buffering', function(done){
request
.post('http://localhost:5000/echo')
.type('application/x-dog')
.send('hello this is dog')
.end(function(err, res){
assert(null == err);
assert(null == res.text);
res.body.should.eql({});
var buf = '';
res.setEncoding('utf8');
res.buffered.should.be.false;
res.on('data', function(chunk){ buf += chunk });
res.on('end', function(){
buf.should.equal('hello this is dog');
done();
});
});
})
})
});