3 var after
= require('after')
4 var express
= require('../')
5 , request
= require('supertest')
6 , assert
= require('assert');
10 app1
.use(function(req
, res
, next
){
12 'text/plain': function(){
16 'text/html': function(){
17 res
.send('<p>hey</p>');
20 'application/json': function(a
, b
, c
){
24 res
.send({ message
: 'hey' });
29 app1
.use(function(err
, req
, res
, next
){
30 if (!err
.types
) throw err
;
31 res
.status(err
.status
)
32 res
.send('Supports: ' + err
.types
.join(', '))
37 app2
.use(function(req
, res
, next
){
39 text: function(){ res
.send('hey') },
40 html: function(){ res
.send('<p>hey</p>') },
41 json: function(){ res
.send({ message
: 'hey' }) }
45 app2
.use(function(err
, req
, res
, next
){
46 res
.status(err
.status
)
47 res
.send('Supports: ' + err
.types
.join(', '))
52 app3
.use(function(req
, res
, next
){
54 text: function(){ res
.send('hey') },
55 default: function (a
, b
, c
) {
66 app4
.get('/', function (req
, res
) {
68 text: function(){ res
.send('hey') },
69 html: function(){ res
.send('<p>hey</p>') },
70 json: function(){ res
.send({ message
: 'hey' }) }
74 app4
.use(function(err
, req
, res
, next
){
75 res
.status(err
.status
)
76 res
.send('Supports: ' + err
.types
.join(', '))
81 app5
.use(function (req
, res
, next
) {
83 default: function () { res
.send('hey') }
87 describe('res', function(){
88 describe('.format(obj)', function(){
89 describe('with canonicalized mime types', function(){
93 describe('with extnames', function(){
97 describe('with parameters', function(){
100 app
.use(function(req
, res
, next
){
102 'text/plain; charset=utf-8': function(){ res
.send('hey') },
103 'text/html; foo=bar; bar=baz': function(){ res
.send('<p>hey</p>') },
104 'application/json; q=0.5': function(){ res
.send({ message
: 'hey' }) }
108 app
.use(function(err
, req
, res
, next
){
109 res
.status(err
.status
)
110 res
.send('Supports: ' + err
.types
.join(', '))
116 describe('given .default', function(){
117 it('should be invoked instead of auto-responding', function(done
){
120 .set('Accept', 'text/html')
121 .expect('default', done
);
124 it('should work when only .default is provided', function (done
) {
127 .set('Accept', '*/*')
128 .expect('hey', done
);
131 it('should be able to invoke other formatter', function (done
) {
134 app
.use(function (req
, res
, next
) {
136 json: function () { res
.send('json') },
137 default: function () {
138 res
.header('x-default', '1')
146 .set('Accept', 'text/plain')
148 .expect('x-default', '1')
154 describe('in router', function(){
158 describe('in router', function(){
160 var router
= express
.Router();
162 router
.get('/', function (req
, res
) {
164 text: function(){ res
.send('hey') },
165 html: function(){ res
.send('<p>hey</p>') },
166 json: function(){ res
.send({ message
: 'hey' }) }
170 router
.use(function(err
, req
, res
, next
){
171 res
.status(err
.status
)
172 res
.send('Supports: ' + err
.types
.join(', '))
183 it('should utilize qvalues in negotiation', function(done
){
186 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
187 .expect({"message":"hey"}, done
);
190 it('should allow wildcard type/subtypes', function(done
){
193 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
194 .expect({"message":"hey"}, done
);
197 it('should default the Content-Type', function(done
){
200 .set('Accept', 'text/html; q=.5, text/plain')
201 .expect('Content-Type', 'text/plain; charset=utf-8')
202 .expect('hey', done
);
205 it('should set the correct charset for the Content-Type', function (done
) {
206 var cb
= after(3, done
)
210 .set('Accept', 'text/html')
211 .expect('Content-Type', 'text/html; charset=utf-8', cb
)
215 .set('Accept', 'text/plain')
216 .expect('Content-Type', 'text/plain; charset=utf-8', cb
)
220 .set('Accept', 'application/json')
221 .expect('Content-Type', 'application/json; charset=utf-8', cb
)
224 it('should Vary: Accept', function(done
){
227 .set('Accept', 'text/html; q=.5, text/plain')
228 .expect('Vary', 'Accept', done
);
231 describe('when Accept is not present', function(){
232 it('should invoke the first callback', function(done
){
235 .expect('hey', done
);
239 describe('when no match is made', function(){
240 it('should should respond with 406 not acceptable', function(done
){
243 .set('Accept', 'foo/bar')
244 .expect('Supports: text/plain, text/html, application/json')