Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

198 lines · 4.7 KiB · JavaScript

Last changed by

Merge tag '3.19.1'Douglas Christopher Wilsonbefore adoption82689d6

History of this file

Take it

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 1–101why, one step back
1
2var express = require('../')3 , request = require('supertest')4 , utils = require('../lib/utils')5 , assert = require('assert');67var app1 = express();89app1.use(function(req, res, next){10 res.format({11 'text/plain': function(){12 res.send('hey');13 },1415 'text/html': function(){16 res.send('<p>hey</p>');17 },1819 'application/json': function(a, b, c){20 assert(req == a);21 assert(res == b);22 assert(next == c);23 res.send({ message: 'hey' });24 }25 });26});2728app1.use(function(err, req, res, next){29 if (!err.types) throw err;30 res.send(err.status, 'Supports: ' + err.types.join(', '));31})3233var app2 = express();3435app2.use(function(req, res, next){36 res.format({37 text: function(){ res.send('hey') },38 html: function(){ res.send('<p>hey</p>') },39 json: function(){ res.send({ message: 'hey' }) }40 });41});4243app2.use(function(err, req, res, next){44 res.send(err.status, 'Supports: ' + err.types.join(', '));45})4647var app3 = express();4849app3.use(function(req, res, next){50 res.format({51 text: function(){ res.send('hey') },52 default: function(){ res.send('default') }53 })54});5556var app4 = express();5758app4.get('/', function(req, res, next){59 res.format({60 text: function(){ res.send('hey') },61 html: function(){ res.send('<p>hey</p>') },62 json: function(){ res.send({ message: 'hey' }) }63 });64});6566app4.use(function(err, req, res, next){67 res.send(err.status, 'Supports: ' + err.types.join(', '));68})6970describe('res', function(){71 describe('.format(obj)', function(){72 describe('with canonicalized mime types', function(){73 test(app1);74 })7576 describe('with extnames', function(){77 test(app2);78 })7980 describe('with parameters', function(){81 var app = express();8283 app.use(function(req, res, next){84 res.format({85 'text/plain; charset=utf-8': function(){ res.send('hey') },86 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },87 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }88 });89 });9091 app.use(function(err, req, res, next){92 res.send(err.status, 'Supports: ' + err.types.join(', '));93 });9495 test(app);96 })9798 describe('given .default', function(){99 it('should be invoked instead of auto-responding', function(done){100 request(app3)101 .get('/')

Merge tag '3.19.1'Douglas Christopher Wilsonbefore adoption82689d6

line 102why, one step back
102 .set('Accept', 'text/html')

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 103–198why, one step back
103 .expect('default', done);
104 })105 })106107 describe('in router', function(){108 test(app4);109 })110111 describe('in router', function(){112 var app = express();113 var router = express.Router();114115 router.get('/', function(req, res, next){116 res.format({117 text: function(){ res.send('hey') },118 html: function(){ res.send('<p>hey</p>') },119 json: function(){ res.send({ message: 'hey' }) }120 });121 });122123 router.use(function(err, req, res, next){124 res.send(err.status, 'Supports: ' + err.types.join(', '));125 })126127 app.use(router)128129 test(app)130 })131 })132})133134function test(app) {135 it('should utilize qvalues in negotiation', function(done){136 request(app)137 .get('/')138 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')139 .expect({"message":"hey"}, done);140 })141142 it('should allow wildcard type/subtypes', function(done){143 request(app)144 .get('/')145 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')146 .expect({"message":"hey"}, done);147 })148149 it('should default the Content-Type', function(done){150 request(app)151 .get('/')152 .set('Accept', 'text/html; q=.5, text/plain')153 .expect('Content-Type', 'text/plain; charset=utf-8')154 .expect('hey', done);155 })156157 it('should set the correct charset for the Content-Type', function() {158 request(app)159 .get('/')160 .set('Accept', 'text/html')161 .expect('Content-Type', 'text/html; charset=utf-8');162163 request(app)164 .get('/')165 .set('Accept', 'text/plain')166 .expect('Content-Type', 'text/plain; charset=utf-8');167168 request(app)169 .get('/')170 .set('Accept', 'application/json')171 .expect('Content-Type', 'application/json');172 })173174 it('should Vary: Accept', function(done){175 request(app)176 .get('/')177 .set('Accept', 'text/html; q=.5, text/plain')178 .expect('Vary', 'Accept', done);179 })180181 describe('when Accept is not present', function(){182 it('should invoke the first callback', function(done){183 request(app)184 .get('/')185 .expect('hey', done);186 })187 })188189 describe('when no match is made', function(){190 it('should should respond with 406 not acceptable', function(done){191 request(app)192 .get('/')193 .set('Accept', 'foo/bar')194 .expect('Supports: text/plain, text/html, application/json')195 .expect(406, done)196 })197 })198}