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.send(err.status, 'Supports: ' + err.types.join(', '));
36 app2.use(function(req, res, next){
38 text: function(){ res.send('hey') },
39 html: function(){ res.send('<p>hey</p>') },
40 json: function(){ res.send({ message: 'hey' }) }
44 app2.use(function(err, req, res, next){
45 res.send(err.status, 'Supports: ' + err.types.join(', '));
50 app3.use(function(req, res, next){
52 text: function(){ res.send('hey') },
53 default: function (a, b, c) {
64 app4.get('/', function(req, res, next){
66 text: function(){ res.send('hey') },
67 html: function(){ res.send('<p>hey</p>') },
68 json: function(){ res.send({ message: 'hey' }) }
72 app4.use(function(err, req, res, next){
73 res.send(err.status, 'Supports: ' + err.types.join(', '));
78 app5.use(function (req, res, next) {
80 default: function () { res.send('hey') }
84 describe('res', function(){
85 describe('.format(obj)', function(){
86 describe('with canonicalized mime types', function(){
90 describe('with extnames', function(){
94 describe('with parameters', function(){
97 app.use(function(req, res, next){
99 'text/plain; charset=utf-8': function(){ res.send('hey') },
100 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },
101 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }
105 app.use(function(err, req, res, next){
106 res.send(err.status, 'Supports: ' + err.types.join(', '));
112 describe('given .default', function(){
113 it('should be invoked instead of auto-responding', function(done){
116 .set('Accept', 'text/html')
117 .expect('default', done);
120 it('should work when only .default is provided', function (done) {
123 .set('Accept', '*/*')
124 .expect('hey', done);
127 it('should be able to invoke other formatter', function (done) {
130 app.use(function (req, res, next) {
132 json: function () { res.send('json') },
133 default: function () {
134 res.header('x-default', '1')
142 .set('Accept', 'text/plain')
144 .expect('x-default', '1')
150 describe('in router', function(){
154 describe('in router', function(){
156 var router = express.Router();
158 router.get('/', function(req, res, next){
160 text: function(){ res.send('hey') },
161 html: function(){ res.send('<p>hey</p>') },
162 json: function(){ res.send({ message: 'hey' }) }
166 router.use(function(err, req, res, next){
167 res.send(err.status, 'Supports: ' + err.types.join(', '));
178 it('should utilize qvalues in negotiation', function(done){
181 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
182 .expect({"message":"hey"}, done);
185 it('should allow wildcard type/subtypes', function(done){
188 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
189 .expect({"message":"hey"}, done);
192 it('should default the Content-Type', function(done){
195 .set('Accept', 'text/html; q=.5, text/plain')
196 .expect('Content-Type', 'text/plain; charset=utf-8')
197 .expect('hey', done);
200 it('should set the correct charset for the Content-Type', function (done) {
201 var cb = after(3, done)
205 .set('Accept', 'text/html')
206 .expect('Content-Type', 'text/html; charset=utf-8', cb)
210 .set('Accept', 'text/plain')
211 .expect('Content-Type', 'text/plain; charset=utf-8', cb)
215 .set('Accept', 'application/json')
216 .expect('Content-Type', 'application/json; charset=utf-8', cb)
219 it('should Vary: Accept', function(done){
222 .set('Accept', 'text/html; q=.5, text/plain')
223 .expect('Vary', 'Accept', done);
226 describe('when Accept is not present', function(){
227 it('should invoke the first callback', function(done){
230 .expect('hey', done);
234 describe('when no match is made', function(){
235 it('should should respond with 406 not acceptable', function(done){
238 .set('Accept', 'foo/bar')
239 .expect('Supports: text/plain, text/html, application/json')