res.format.js
This file
Path
test/res.format.js
141 lines · 3.3 KiB · JavaScript
Last changed by
Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
History of this fileTake it
The walk stopped after 200 commits: lines older than 82731da point at it.
Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
lines 1–55why, one step back12var express = require('../')3 , request = require('./support/http')4 , utils = require('../lib/utils')5 , assert = require('assert');67var app = express();89app.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 },18 19 '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});2728app.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});55Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 56why, one step back56describe('res', function(){Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
lines 57–81why, one step back57 describe('.format(obj)', function(){58 describe('with canonicalized mime types', function(){59 test(app);60 })6162 describe('with extnames', function(){63 test(app2);64 })6566 describe('given .default', function(){67 it('should be invoked instead of auto-responding', function(done){68 request(app3)69 .get('/')70 .set('Accept: text/html')71 .expect('default', done);72 })73 })74 })75})7677function test(app) {78 it('should utilize qvalues in negotiation', function(done){79 request(app)80 .get('/')81 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 82why, one step back82 .expect({"message":"hey"}, done);Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
lines 83–88why, one step back83 })8485 it('should allow wildcard type/subtypes', function(done){86 request(app)87 .get('/')88 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 89why, one step back89 .expect({"message":"hey"}, done);Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
lines 90–95why, one step back90 })9192 it('should default the Content-Type', function(done){93 request(app)94 .get('/')95 .set('Accept', 'text/html; q=.5, text/plain')Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
line 96why, one step back96 .expect('Content-Type', 'text/plain; charset=UTF-8')Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
line 97why, one step back97 .expect('hey', done);Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
lines 98–114why, one step back98 })99100 it('should set the correct charset for the Content-Type', function() {101 request(app)102 .get('/')103 .set('Accept', 'text/html')104 .expect('Content-Type', 'text/html; charset=UTF-8');105106 request(app)107 .get('/')108 .set('Accept', 'text/plain')109 .expect('Content-Type', 'text/plain; charset=UTF-8');110111 request(app)112 .get('/')113 .set('Accept', 'application/json')114 .expect('Content-Type', 'application/json');Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da
lines 115–141why, one step back115 })116117 it('should Vary: Accept', function(done){118 request(app)119 .get('/')120 .set('Accept', 'text/html; q=.5, text/plain')121 .expect('Vary', 'Accept', done);122 })123124 describe('when Accept is not present', function(){125 it('should invoke the first callback', function(done){126 request(app)127 .get('/')128 .expect('hey', done);129 })130 })131132 describe('when no match is made', function(){133 it('should should respond with 406 not acceptable', function(done){134 request(app)135 .get('/')136 .set('Accept', 'foo/bar')137 .expect('Supports: text/plain, text/html, application/json')138 .expect(406, done)139 })140 })141}