res.format.js
This file
Path
test/res.format.js
124 lines · 2.8 KiB · JavaScript
Last changed by
updated tests to use "supertest"TJ Holowaychukbefore adoption0f5560e
History of this fileTake it
The walk stopped after 200 commits: lines older than cc84f40 point at it.
miscTJ Holowaychukbefore adoptioncc84f40
lines 1–46why, 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})46Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12
lines 47–55why, one step back47var 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});55miscTJ Holowaychukbefore adoptioncc84f40
lines 56–63why, one step back56describe('req', function(){57 describe('.format(obj)', function(){58 describe('with canonicalized mime types', function(){59 test(app);60 })6162 describe('with extnames', function(){63 test(app2);Added `.default()` support to `res.format()`TJ Holowaychukbefore adoption82b5b12
lines 64–72why, one step back64 })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 })miscTJ Holowaychukbefore adoptioncc84f40
lines 73–95why, one step back73 })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')82 .expect('{"message":"hey"}', done);83 })8485 it('should allow wildcard type/subtypes', function(done){86 request(app)87 .get('/')88 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')89 .expect('{"message":"hey"}', done);90 })9192 it('should default the Content-Type', function(done){93 request(app)94 .get('/')95 .set('Accept', 'text/html; q=.5, text/plain')updated tests to use "supertest"TJ Holowaychukbefore adoption0f5560e
lines 96–97why, one step back96 .expect('Content-Type', 'text/plain')97 .expect('hey', done);miscTJ Holowaychukbefore adoptioncc84f40
lines 98–103why, one step back98 })99100 it('should Vary: Accept', function(done){101 request(app)102 .get('/')103 .set('Accept', 'text/html; q=.5, text/plain')updated tests to use "supertest"TJ Holowaychukbefore adoption0f5560e
line 104why, one step back104 .expect('Vary', 'Accept', done);miscTJ Holowaychukbefore adoptioncc84f40
lines 105–119why, one step back105 })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')updated tests to use "supertest"TJ Holowaychukbefore adoption0f5560e
lines 120–121why, one step back120 .expect('Supports: text/plain, text/html, application/json')121 .expect(406, done)miscTJ Holowaychukbefore adoptioncc84f40
lines 122–124why, one step back122 })123 })124}