res.format.js
This file
Path
test/res.format.js
124 lines · 2.8 KiB · JavaScript
Last changed by
Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f20a5e point at it.
add CORS exampleTJ Holowaychukbefore adoption0f20a5e
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(){add CORS exampleTJ Holowaychukbefore adoption0f20a5e
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);add CORS exampleTJ Holowaychukbefore adoption0f20a5e
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);add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 90–124why, 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')96 .expect('Content-Type', 'text/plain')97 .expect('hey', done);98 })99100 it('should Vary: Accept', function(done){101 request(app)102 .get('/')103 .set('Accept', 'text/html; q=.5, text/plain')104 .expect('Vary', 'Accept', done);105 })106107 describe('when Accept is not present', function(){108 it('should invoke the first callback', function(done){109 request(app)110 .get('/')111 .expect('hey', done);112 })113 })114115 describe('when no match is made', function(){116 it('should should respond with 406 not acceptable', function(done){117 request(app)118 .get('/')119 .set('Accept', 'foo/bar')120 .expect('Supports: text/plain, text/html, application/json')121 .expect(406, done)122 })123 })124}