res.format.js
This file
Path
test/res.format.js
159 lines · 3.7 KiB · JavaScript
Last changed by
set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
History of this fileTake it
The walk stopped after 200 commits: lines older than a10f695 point at it.
pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 3why, one step back3 , request = require('supertest')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 4–6why, one step back4 , utils = require('../lib/utils')5 , assert = require('assert');6tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
line 7why, one step back7var app1 = express();pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 8why, one step back8tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
line 9why, one step back9app1.use(function(req, res, next){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 10–17why, one step back10 res.format({11 'text/plain': function(){12 res.send('hey');13 },1415 'text/html': function(){16 res.send('<p>hey</p>');17 },remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 18why, one step back18pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 19–27why, one step back19 '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});27tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
line 28why, one step back28app1.use(function(err, req, res, next){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 29–55why, one step back29 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});55tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
lines 56–69why, one step back56var 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})69Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 70why, one step back70describe('res', function(){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 71–72why, one step back71 describe('.format(obj)', function(){72 describe('with canonicalized mime types', function(){tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
line 73why, one step back73 test(app1);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 74–86why, one step back74 })7576 describe('with extnames', function(){77 test(app2);78 })7980 describe('given .default', function(){81 it('should be invoked instead of auto-responding', function(done){82 request(app3)83 .get('/')84 .set('Accept: text/html')85 .expect('default', done);86 })tests: add test for res.format in routeDouglas Christopher Wilsonbefore adoption35a66d8
lines 87–90why, one step back87 })8889 describe('in router', function(){90 test(app4);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 91–99why, one step back91 })92 })93})9495function test(app) {96 it('should utilize qvalues in negotiation', function(done){97 request(app)98 .get('/')99 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 100why, one step back100 .expect({"message":"hey"}, done);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 101–106why, one step back101 })102103 it('should allow wildcard type/subtypes', function(done){104 request(app)105 .get('/')106 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')Merge pull request #1710 from hacksparrow/masterTJ Holowaychukbefore adoptiond6ecf78
line 107why, one step back107 .expect({"message":"hey"}, done);pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 108–113why, one step back108 })109110 it('should default the Content-Type', function(done){111 request(app)112 .get('/')113 .set('Accept', 'text/html; q=.5, text/plain')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
line 114why, one step back114 .expect('Content-Type', 'text/plain; charset=utf-8')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 115why, one step back115 .expect('hey', done);Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
lines 116–121why, one step back116 })117118 it('should set the correct charset for the Content-Type', function() {119 request(app)120 .get('/')121 .set('Accept', 'text/html')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
line 122why, one step back122 .expect('Content-Type', 'text/html; charset=utf-8');Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
lines 123–126why, one step back123124 request(app)125 .get('/')126 .set('Accept', 'text/plain')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
line 127why, one step back127 .expect('Content-Type', 'text/plain; charset=utf-8');Merge pull request #1747 from sorribas/masterTJ Holowaychukbefore adoption1c87e5e
lines 128–132why, one step back128129 request(app)130 .get('/')131 .set('Accept', 'application/json')132 .expect('Content-Type', 'application/json');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 133–159why, one step back133 })134135 it('should Vary: Accept', function(done){136 request(app)137 .get('/')138 .set('Accept', 'text/html; q=.5, text/plain')139 .expect('Vary', 'Accept', done);140 })141142 describe('when Accept is not present', function(){143 it('should invoke the first callback', function(done){144 request(app)145 .get('/')146 .expect('hey', done);147 })148 })149150 describe('when no match is made', function(){151 it('should should respond with 406 not acceptable', function(done){152 request(app)153 .get('/')154 .set('Accept', 'foo/bar')155 .expect('Supports: text/plain, text/html, application/json')156 .expect(406, done)157 })158 })159}