forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsers.js
More file actions
74 lines (64 loc) · 1.7 KB
/
parsers.js
File metadata and controls
74 lines (64 loc) · 1.7 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
var request = require('../..')
, express = require('express')
, assert = require('better-assert')
, fs = require('fs')
, app = express();
app.get('/manny', function(req, res){
res.status(200).json({name:"manny"});
});
var img = fs.readFileSync(__dirname + '/fixtures/test.png');
app.get('/image', function(req, res){
res.writeHead(200, {'Content-Type': 'image/png' });
res.end(img, 'binary');
});
app.listen(3033);
describe('req.parse(fn)', function(){
it('should take precedence over default parsers', function(done){
request
.get('http://localhost:3033/manny')
.parse(request.parse['application/json'])
.end(function(err, res){
assert(res.ok);
assert('{"name":"manny"}' == res.text);
assert('manny' == res.body.name);
done();
});
})
it('should be the only parser', function(done){
request
.get('http://localhost:3033/image')
.parse(function(res, fn) {
res.on('data', function() {});
})
.end(function(err, res){
assert(res.ok);
assert(res.text === undefined);
res.body.should.eql({});
done();
});
})
it('should emit error if parser throws', function(done){
request
.get('http://localhost:3033/manny')
.parse(function() {
throw new Error('I am broken');
})
.on('error', function(err) {
err.message.should.equal('I am broken');
done();
})
.end();
})
it('should emit error if parser returns an error', function(done){
request
.get('http://localhost:3033/manny')
.parse(function(res, fn) {
fn(new Error('I am broken'));
})
.on('error', function(err) {
err.message.should.equal('I am broken');
done();
})
.end()
})
})