forked from expressjs/express
/
res.respondTo.js
65 lines (55 loc) · 1.58 KB
/
res.respondTo.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
var express = require('../')
, request = require('./support/http')
, utils = require('../lib/utils');
var app = express();
app.use(function(req, res, next){
res.respondTo({
'text/plain': function(){
res.type('text').send('hey');
},
'text/html': function(){
res.send('<p>hey</p>');
},
'appliation/json': function(){
res.send({ message: 'hey' });
}
});
});
app.use(function(err, req, res, next){
res.send(406, 'Supports: ' + err.types.join(', '));
})
describe('req', function(){
describe('.respondTo(obj)', function(){
it('should utilize qvalues in negotiation', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, appliation/json, */*; q=.1')
.expect('{"message":"hey"}', done);
})
it('should allow wildcard type/subtypes', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, appliation/*, */*; q=.1')
.expect('{"message":"hey"}', done);
})
describe('when Accept is not present', function(){
it('should invoke the first callback', function(done){
request(app)
.get('/')
.expect('hey', done);
})
})
describe('when no match is made', function(){
it('should should respond with 406 not acceptable', function(done){
request(app)
.get('/')
.set('Accept', 'foo/bar')
.end(function(res){
res.should.have.status(406);
res.body.should.equal('Supports: text/plain, text/html, appliation/json');
done();
});
})
})
})
})