Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

213 lines · 5.0 KiB · JavaScript

Last changed by

Fix res.format error when only default providedBehcet Uyarbefore adoption24d1c98

History of this file

Take it

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 1–69why, 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})69

Fix res.format error when only default providedBehcet Uyarbefore adoption24d1c98

lines 70–77why, one step back
70var app5 = express();
7172app5.use(function (req, res, next) {73 res.format({74 default: function () { res.send('hey') }75 });76});77

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 78–109why, one step back
78describe('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('/')

Merge tag '3.19.1'Douglas Christopher Wilsonbefore adoption82689d6

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

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

line 111why, one step back
111 .expect('default', done);

Fix res.format error when only default providedBehcet Uyarbefore adoption24d1c98

lines 112–118why, one step back
112 })
113114 it('should work when only .default is provided', function (done) {115 request(app5)116 .get('/')117 .set('Accept', '*/*')118 .expect('hey', done);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 119–213why, one step back
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 })171172 it('should set the correct charset for the Content-Type', function() {173 request(app)174 .get('/')175 .set('Accept', 'text/html')176 .expect('Content-Type', 'text/html; charset=utf-8');177178 request(app)179 .get('/')180 .set('Accept', 'text/plain')181 .expect('Content-Type', 'text/plain; charset=utf-8');182183 request(app)184 .get('/')185 .set('Accept', 'application/json')186 .expect('Content-Type', 'application/json');187 })188189 it('should Vary: Accept', function(done){190 request(app)191 .get('/')192 .set('Accept', 'text/html; q=.5, text/plain')193 .expect('Vary', 'Accept', done);194 })195196 describe('when Accept is not present', function(){197 it('should invoke the first callback', function(done){198 request(app)199 .get('/')200 .expect('hey', done);201 })202 })203204 describe('when no match is made', function(){205 it('should should respond with 406 not acceptable', function(done){206 request(app)207 .get('/')208 .set('Accept', 'foo/bar')209 .expect('Supports: text/plain, text/html, application/json')210 .expect(406, done)211 })212 })213}