2 var after = require('after')
3 var express = require('../')
4 , request = require('supertest')
5 , assert = require('assert');
9 app1.use(function(req, res, next){
11 'text/plain': function(){
15 'text/html': function(){
16 res.send('<p>hey</p>');
19 'application/json': function(a, b, c){
23 res.send({ message: 'hey' });
28 app1.use(function(err, req, res, next){
29 if (!err.types) throw err;
30 res.send(err.status, 'Supports: ' + err.types.join(', '));
35 app2.use(function(req, res, next){
37 text: function(){ res.send('hey') },
38 html: function(){ res.send('<p>hey</p>') },
39 json: function(){ res.send({ message: 'hey' }) }
43 app2.use(function(err, req, res, next){
44 res.send(err.status, 'Supports: ' + err.types.join(', '));
49 app3.use(function(req, res, next){
51 text: function(){ res.send('hey') },
52 default: function(){ res.send('default') }
58 app4.get('/', function(req, res, next){
60 text: function(){ res.send('hey') },
61 html: function(){ res.send('<p>hey</p>') },
62 json: function(){ res.send({ message: 'hey' }) }
66 app4.use(function(err, req, res, next){
67 res.send(err.status, 'Supports: ' + err.types.join(', '));
72 app5.use(function (req, res, next) {
74 default: function () { res.send('hey') }
78 describe('res', function(){
79 describe('.format(obj)', function(){
80 describe('with canonicalized mime types', function(){
84 describe('with extnames', function(){
88 describe('with parameters', function(){
91 app.use(function(req, res, next){
93 'text/plain; charset=utf-8': function(){ res.send('hey') },
94 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },
95 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }
99 app.use(function(err, req, res, next){
100 res.send(err.status, 'Supports: ' + err.types.join(', '));
106 describe('given .default', function(){
107 it('should be invoked instead of auto-responding', function(done){
110 .set('Accept', 'text/html')
111 .expect('default', done);
114 it('should work when only .default is provided', function (done) {
117 .set('Accept', '*/*')
118 .expect('hey', done);
122 describe('in router', function(){
126 describe('in router', function(){
128 var router = express.Router();
130 router.get('/', function(req, res, next){
132 text: function(){ res.send('hey') },
133 html: function(){ res.send('<p>hey</p>') },
134 json: function(){ res.send({ message: 'hey' }) }
138 router.use(function(err, req, res, next){
139 res.send(err.status, 'Supports: ' + err.types.join(', '));
150 it('should utilize qvalues in negotiation', function(done){
153 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
154 .expect({"message":"hey"}, done);
157 it('should allow wildcard type/subtypes', function(done){
160 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
161 .expect({"message":"hey"}, done);
164 it('should default the Content-Type', function(done){
167 .set('Accept', 'text/html; q=.5, text/plain')
168 .expect('Content-Type', 'text/plain; charset=utf-8')
169 .expect('hey', done);
172 it('should set the correct charset for the Content-Type', function (done) {
173 var cb = after(3, done)
177 .set('Accept', 'text/html')
178 .expect('Content-Type', 'text/html; charset=utf-8', cb)
182 .set('Accept', 'text/plain')
183 .expect('Content-Type', 'text/plain; charset=utf-8', cb)
187 .set('Accept', 'application/json')
188 .expect('Content-Type', 'application/json; charset=utf-8', cb)
191 it('should Vary: Accept', function(done){
194 .set('Accept', 'text/html; q=.5, text/plain')
195 .expect('Vary', 'Accept', done);
198 describe('when Accept is not present', function(){
199 it('should invoke the first callback', function(done){
202 .expect('hey', done);
206 describe('when no match is made', function(){
207 it('should should respond with 406 not acceptable', function(done){
210 .set('Accept', 'foo/bar')
211 .expect('Supports: text/plain, text/html, application/json')