basic.js
11.3 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
var fs = require('fs')
var should = require('should')
var unirest = require('../index')
var express = require('express')
var bodyParser = require('body-parser')
// Mock Server
var app = express()
var server
describe('Unirest', function () {
describe('Cookie Jar', function () {
it('should contain both add and getCookieString methods', function (done) {
var jar = unirest.jar()
jar.should.have.property('add')
jar.should.have.property('getCookieString')
jar.add.should.be.a.Function
jar.getCookieString.should.be.a.Function
done()
})
})
describe('GET request', function () {
it('should correctly parse JSON.', function (done) {
unirest.get('http://mockbin.com/request').set('Accept', 'application/json').end(function (response) {
should(response.status).equal(200)
should(response.body).have.type('object')
done()
})
})
it('should correctly parse GZIPPED data.', function (done) {
unirest.get('http://mockbin.com/gzip/request').set('Accept-Encoding', 'gzip').end(function (response) {
should(response.status).equal(200)
should(response.body).have.type('object')
done()
})
})
it('should correctly handle redirects.', function (done) {
unirest.get('http://mockbin.com/redirect/302').timeout(2500).end(function (response) {
should(response.status).equal(200)
should(response.body).equal('redirect finished')
done()
})
})
it('should correctly handle timeouts.', function (done) {
unirest.get('http://mockbin.com/redirect/3').timeout(20).end(function (response) {
response.error.should.exist
response.error.code.should.equal('ETIMEDOUT')
done()
})
})
it('should correctly handle refused connections.', function (done) {
unirest.get('http://localhost:9999').timeout(200).end(function (response) {
response.error.should.exist
response.error.code.should.equal('ECONNREFUSED')
done()
})
})
it('should be able to work like other unirest libraries', function (done) {
unirest.get('http://mockbin.com/gzip/request', { 'Accept-Encoding': 'gzip' }, 'Hello World', function (response) {
should(response.status).equal(200)
should(response.body).have.type('object')
done()
})
})
it('should be able to return the request time', function (done) {
unirest.get('http://mockbin.com').time(true).end(function (response) {
should(typeof response.elapsedTime).equal('number')
should((response.elapsedTime > 0), true)
done()
})
})
})
describe('GET request', function () {
var host, port, url
var fixture = {
message: 'some message under a json object'
}
before(function(done) {
app.use(bodyParser.json({
type: 'application/vnd.api+json'
}))
app.get('/', function handleRoot(req, res) {
res.set('content-type', 'application/vnd.api+json')
res.send(fixture)
})
server = app.listen(3000, function liftServer () {
host = server.address().address
port = server.address().port
url = 'http://localhost:3000'
done()
})
})
after(function afterAll (done) {
server.close(function closeCallback () {
done()
})
})
it('should get a json from the main route', function jsonTest (done) {
unirest.get(url).type('json').end(function endJsonTest (response) {
response.body.should.eql(fixture)
done()
})
})
})
describe('POST request', function () {
it('should correctly post FORM data.', function (done) {
var data = {
is: 'unirest',
my: 'name',
hello: 'world'
}
unirest.post('http://mockbin.com/request').send(data).end(function (response) {
should(response.status).equal(200)
should(response.body.postData.params).have.type('object')
should(response.body.postData.params).have.property('is', data.is)
should(response.body.postData.params).have.property('my', data.my)
should(response.body.postData.params).have.property('hello', data.hello)
should(response.body.headers['content-length']).equal('30')
should(response.body.headers['content-type']).equal('application/x-www-form-urlencoded')
done()
})
})
it('should not fail when content-type header is specified.', function (done) {
var data = {
is: 'unirest',
my: 'name',
hello: 'world',
failing_to_encode_data: 'this is a test'
}
unirest.post('http://mockbin.com/request').header('Content-Type', 'application/x-www-form-urlencoded').send(data).end(function (response) {
should(response.status).equal(200)
should(response.body.postData.params).have.type('object')
should(response.body.postData.params).have.property('is', data.is)
should(response.body.postData.params).have.property('my', data.my)
should(response.body.postData.params).have.property('hello', data.hello)
should(response.body.postData.params).have.property('failing_to_encode_data', data.failing_to_encode_data)
should(response.body.headers['content-length']).equal('74')
should(response.body.headers['content-type']).equal('application/x-www-form-urlencoded')
done()
})
})
it('should correctly transform FORM data.', function (done) {
var data = {
lebron: false,
jordan: 23,
testing: { key: 'value' },
scores: [
1,
3,
2,
1,
3
]
}
try {
var request = unirest.post('http://mockbin.com/request').set('Accept', 'application/json')
for (var key in data) {
request.field(key, data[key])
}
request.end(function (response) {
should(response.status).equal(200)
should(response.body.postData.params).have.property('jordan', data.jordan.toString())
should(response.body.postData.params).have.property('lebron', data.lebron.toString())
should(response.body.postData.params).have.property('testing', JSON.stringify(data.testing))
should(response.body.postData.params).have.property('scores', ['1', '3', '2', '1', '3'])
})
} catch (e) {
done(e)
} finally {
done()
}
})
it('should correctly post MULTIFORM data.', function (done) {
var request = unirest.post('http://mockbin.com/request')
var file = __dirname + '/../README.md'
var data = {
a: 'foo',
b: 'bar',
c: undefined
}
request.attach('u', file)
for (var key in data) {
request.field(key, data[key])
}
request.end(function (response) {
should(response.status).equal(200)
should(response.body.headers['content-type']).startWith('multipart/form-data')
done()
})
})
it('should correctly post MULTIFORM data using fs.createReadStream.', function (done) {
var request = unirest.post('http://mockbin.com/request')
var file = __dirname + '/../README.md'
var data = {
a: 'foo',
b: 'bar',
c: undefined
}
request.attach({'u': fs.createReadStream(file)})
for (var key in data) {
request.field(key, data[key])
}
request.end(function (response) {
should(response.status).equal(200)
should(response.body.headers['content-type']).startWith('multipart/form-data')
done()
})
})
it('should correctly post MULTIFORM data with port number', function (done) {
var request = unirest.post('http://mockbin.com:80/request')
var file = __dirname + '/../README.md'
var data = {
a: 'foo',
b: 'bar',
c: undefined
}
request.attach('u', file)
for (var key in data) {
request.field(key, data[key])
}
request.end(function (response) {
should(response.status).equal(200)
should(response.body.headers['content-type']).startWith('multipart/form-data')
done()
})
})
it('should correctly post JSON data.', function (done) {
var data = {
is: 'unirest',
my: 'name',
hello: 'world'
}
unirest.post('http://mockbin.com/echo').header('Content-Type', 'application/json').send(data).end(function (response) {
should(response.status).equal(200)
should(response.body).have.type('object')
should(JSON.stringify(response.body)).equal(JSON.stringify(data))
should(response.headers['content-type']).startWith('application/json')
done()
})
})
it('should correctly post JSON array.', function (done) {
var data = [{
is: 'unirest',
my: 'name',
hello: 'world'
}]
unirest.post('http://mockbin.com/echo').header('Content-Type', 'application/json').send(data).end(function (response) {
should(response.status).equal(200)
should(response.body).have.type('object')
should(JSON.stringify(response.body)).equal(JSON.stringify(data))
should(response.headers['content-type']).startWith('application/json')
done()
})
})
it('should check for buffers', function (done) {
unirest.post('http://mockbin.com/request')
.headers({ 'Accept': 'application/json' })
.send(new Buffer([1, 2, 3]))
.end(function (response) {
should(response.body.bodySize).exists
should(response.body.bodySize).equal(3)
done()
})
})
it('should correctly post a buffer with mime-type', function (done) {
unirest.post('http://mockbin.com/request')
.headers({ 'Content-Type': 'img/svg+xml' })
.send(new Buffer('<svg></svg>'))
.end(function (response) {
should(response.body.headers['content-type']).equal('img/svg+xml')
done()
})
})
})
describe('bodyparser', function unirestDescribe () {
var host, port, url
var postCall, exampleRequest
before(function beforeAll (done) {
exampleRequest = {
data: {
id: 1,
type: 'Person',
name: 'Nico'
}
}
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
app.post('/', function handleRoot (req, res) {
res.send(req.body)
})
server = app.listen(3000, function liftServer () {
host = server.address().address
port = server.address().port
url = 'http://localhost:3000'
done()
})
})
after(function afterAll (done) {
server.close(function closeCallback () {
done()
})
})
it('should send a custom content type as a header using the type function', function jsonTest (done) {
unirest
.post(url + '/')
.type('application/vnd.api+json')
.send(exampleRequest)
.end(function endJsonTest (response) {
response.body.should.eql(exampleRequest)
done()
})
})
it('should send a custom header with the header function', function jsonTest (done) {
unirest
.post(url + '/')
.header('Content-Type', 'application/vnd.api+json')
.send(exampleRequest)
.end(function endJsonTest (response) {
response.body.should.eql(exampleRequest)
done()
})
})
})
})