res.format.js
This file
Path
test/res.format.js
215 lines · 5.1 KiB · JavaScript
Last changed by
lint: remove usages of "=="Alejandro Estradabefore adoption40e04ec
History of this fileTake it
The walk stopped after 200 commits: lines older than 43dff4c point at it.
docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
line 1why, one step back1tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2
line 2why, one step back2var after = require('after')docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 3–19why, one step back3var express = require('../')4 , request = require('supertest')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){lint: remove usages of "=="Alejandro Estradabefore adoption40e04ec
lines 20–22why, one step back20 assert(req === a)21 assert(res === b)22 assert(next === c)docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 23–171why, one step back23 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})6970var app5 = express();7172app5.use(function (req, res, next) {73 res.format({74 default: function () { res.send('hey') }75 });76});7778describe('res', function(){79 describe('.format(obj)', function(){80 describe('with canonicalized mime types', function(){81 test(app1);82 })8384 describe('with extnames', function(){85 test(app2);86 })8788 describe('with parameters', function(){89 var app = express();9091 app.use(function(req, res, next){92 res.format({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' }) }96 });97 });9899 app.use(function(err, req, res, next){100 res.send(err.status, 'Supports: ' + err.types.join(', '));101 });102103 test(app);104 })105106 describe('given .default', function(){107 it('should be invoked instead of auto-responding', function(done){108 request(app3)109 .get('/')110 .set('Accept', 'text/html')111 .expect('default', done);112 })113114 it('should work when only .default is provided', function (done) {115 request(app5)116 .get('/')117 .set('Accept', '*/*')118 .expect('hey', done);119 })120 })121122 describe('in router', function(){123 test(app4);124 })125126 describe('in router', function(){127 var app = express();128 var router = express.Router();129130 router.get('/', function(req, res, next){131 res.format({132 text: function(){ res.send('hey') },133 html: function(){ res.send('<p>hey</p>') },134 json: function(){ res.send({ message: 'hey' }) }135 });136 });137138 router.use(function(err, req, res, next){139 res.send(err.status, 'Supports: ' + err.types.join(', '));140 })141142 app.use(router)143144 test(app)145 })146 })147})148149function test(app) {150 it('should utilize qvalues in negotiation', function(done){151 request(app)152 .get('/')153 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')154 .expect({"message":"hey"}, done);155 })156157 it('should allow wildcard type/subtypes', function(done){158 request(app)159 .get('/')160 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')161 .expect({"message":"hey"}, done);162 })163164 it('should default the Content-Type', function(done){165 request(app)166 .get('/')167 .set('Accept', 'text/html; q=.5, text/plain')168 .expect('Content-Type', 'text/plain; charset=utf-8')169 .expect('hey', done);170 })171tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2
lines 172–174why, one step back172 it('should set the correct charset for the Content-Type', function (done) {173 var cb = after(3, done)174docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 175–177why, one step back175 request(app)176 .get('/')177 .set('Accept', 'text/html')tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2
line 178why, one step back178 .expect('Content-Type', 'text/html; charset=utf-8', cb)docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 179–182why, one step back179180 request(app)181 .get('/')182 .set('Accept', 'text/plain')tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2
line 183why, one step back183 .expect('Content-Type', 'text/plain; charset=utf-8', cb)docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 184–187why, one step back184185 request(app)186 .get('/')187 .set('Accept', 'application/json')tests: run mocha with --no-exit to detect hangsDouglas Christopher Wilsonbefore adoptionde129c2
line 188why, one step back188 .expect('Content-Type', 'application/json; charset=utf-8', cb)docs: fix GitHub capitalizationPiper Chesterbefore adoption43dff4c
lines 189–215why, one step back189 })190191 it('should Vary: Accept', function(done){192 request(app)193 .get('/')194 .set('Accept', 'text/html; q=.5, text/plain')195 .expect('Vary', 'Accept', done);196 })197198 describe('when Accept is not present', function(){199 it('should invoke the first callback', function(done){200 request(app)201 .get('/')202 .expect('hey', done);203 })204 })205206 describe('when no match is made', function(){207 it('should should respond with 406 not acceptable', function(done){208 request(app)209 .get('/')210 .set('Accept', 'foo/bar')211 .expect('Supports: text/plain, text/html, application/json')212 .expect(406, done)213 })214 })215}