Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

216 lines · 5.1 KiB · JavaScript

Last changed by

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

History of this file

Take it

The walk stopped after 200 commits: lines older than 76bf96e point at it.

tests: use strict modeDouglas Christopher Wilsonbefore adoptiona659137

line 1why, one step back
1'use strict'

deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e

lines 2–216why, one step back
2
3var after = require('after')4var express = require('../')5 , request = require('supertest')6 , assert = require('assert');78var app1 = express();910app1.use(function(req, res, next){11 res.format({12 'text/plain': function(){13 res.send('hey');14 },1516 'text/html': function(){17 res.send('<p>hey</p>');18 },1920 'application/json': function(a, b, c){21 assert(req === a)22 assert(res === b)23 assert(next === c)24 res.send({ message: 'hey' });25 }26 });27});2829app1.use(function(err, req, res, next){30 if (!err.types) throw err;31 res.send(err.status, 'Supports: ' + err.types.join(', '));32})3334var app2 = express();3536app2.use(function(req, res, next){37 res.format({38 text: function(){ res.send('hey') },39 html: function(){ res.send('<p>hey</p>') },40 json: function(){ res.send({ message: 'hey' }) }41 });42});4344app2.use(function(err, req, res, next){45 res.send(err.status, 'Supports: ' + err.types.join(', '));46})4748var app3 = express();4950app3.use(function(req, res, next){51 res.format({52 text: function(){ res.send('hey') },53 default: function(){ res.send('default') }54 })55});5657var app4 = express();5859app4.get('/', function(req, res, next){60 res.format({61 text: function(){ res.send('hey') },62 html: function(){ res.send('<p>hey</p>') },63 json: function(){ res.send({ message: 'hey' }) }64 });65});6667app4.use(function(err, req, res, next){68 res.send(err.status, 'Supports: ' + err.types.join(', '));69})7071var app5 = express();7273app5.use(function (req, res, next) {74 res.format({75 default: function () { res.send('hey') }76 });77});7879describe('res', function(){80 describe('.format(obj)', function(){81 describe('with canonicalized mime types', function(){82 test(app1);83 })8485 describe('with extnames', function(){86 test(app2);87 })8889 describe('with parameters', function(){90 var app = express();9192 app.use(function(req, res, next){93 res.format({94 'text/plain; charset=utf-8': function(){ res.send('hey') },95 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },96 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }97 });98 });99100 app.use(function(err, req, res, next){101 res.send(err.status, 'Supports: ' + err.types.join(', '));102 });103104 test(app);105 })106107 describe('given .default', function(){108 it('should be invoked instead of auto-responding', function(done){109 request(app3)110 .get('/')111 .set('Accept', 'text/html')112 .expect('default', done);113 })114115 it('should work when only .default is provided', function (done) {116 request(app5)117 .get('/')118 .set('Accept', '*/*')119 .expect('hey', done);120 })121 })122123 describe('in router', function(){124 test(app4);125 })126127 describe('in router', function(){128 var app = express();129 var router = express.Router();130131 router.get('/', function(req, res, next){132 res.format({133 text: function(){ res.send('hey') },134 html: function(){ res.send('<p>hey</p>') },135 json: function(){ res.send({ message: 'hey' }) }136 });137 });138139 router.use(function(err, req, res, next){140 res.send(err.status, 'Supports: ' + err.types.join(', '));141 })142143 app.use(router)144145 test(app)146 })147 })148})149150function test(app) {151 it('should utilize qvalues in negotiation', function(done){152 request(app)153 .get('/')154 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')155 .expect({"message":"hey"}, done);156 })157158 it('should allow wildcard type/subtypes', function(done){159 request(app)160 .get('/')161 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')162 .expect({"message":"hey"}, done);163 })164165 it('should default the Content-Type', function(done){166 request(app)167 .get('/')168 .set('Accept', 'text/html; q=.5, text/plain')169 .expect('Content-Type', 'text/plain; charset=utf-8')170 .expect('hey', done);171 })172173 it('should set the correct charset for the Content-Type', function (done) {174 var cb = after(3, done)175176 request(app)177 .get('/')178 .set('Accept', 'text/html')179 .expect('Content-Type', 'text/html; charset=utf-8', cb)180181 request(app)182 .get('/')183 .set('Accept', 'text/plain')184 .expect('Content-Type', 'text/plain; charset=utf-8', cb)185186 request(app)187 .get('/')188 .set('Accept', 'application/json')189 .expect('Content-Type', 'application/json; charset=utf-8', cb)190 })191192 it('should Vary: Accept', function(done){193 request(app)194 .get('/')195 .set('Accept', 'text/html; q=.5, text/plain')196 .expect('Vary', 'Accept', done);197 })198199 describe('when Accept is not present', function(){200 it('should invoke the first callback', function(done){201 request(app)202 .get('/')203 .expect('hey', done);204 })205 })206207 describe('when no match is made', function(){208 it('should should respond with 406 not acceptable', function(done){209 request(app)210 .get('/')211 .set('Accept', 'foo/bar')212 .expect('Supports: text/plain, text/html, application/json')213 .expect(406, done)214 })215 })216}