Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

198 lines · 4.7 KiB · JavaScript

Last changed by

tests: add more testsDouglas Christopher Wilsonbefore adoption5019f38

History of this file

Take it

The walk stopped after 200 commits: lines older than 34c83d7 point at it.

3.4.7Jonathan Ongbefore adoption34c83d7

lines 1–2why, one step back
1
2var express = require('../')

remove unnecessary test/support/httpJonathan Ongbefore adoption643397e

line 3why, one step back
3 , request = require('supertest')

3.4.7Jonathan Ongbefore adoption34c83d7

lines 4–6why, one step back
4 , utils = require('../lib/utils')
5 , assert = require('assert');6

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 7why, one step back
7var app1 = express();

3.4.7Jonathan Ongbefore adoption34c83d7

line 8why, one step back
8

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 9why, one step back
9app1.use(function(req, res, next){

3.4.7Jonathan Ongbefore adoption34c83d7

lines 10–17why, one step back
10 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 adoption643397e

line 18why, one step back
18

3.4.7Jonathan Ongbefore adoption34c83d7

lines 19–27why, one step back
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});27

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 28why, one step back
28app1.use(function(err, req, res, next){

3.4.7Jonathan Ongbefore adoption34c83d7

lines 29–55why, one step back
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});55

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

lines 56–69why, one step back
56var 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

3.4.7Jonathan Ongbefore adoption34c83d7

lines 70–72why, one step back
70describe('res', function(){
71 describe('.format(obj)', function(){72 describe('with canonicalized mime types', function(){

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 73why, one step back
73 test(app1);

3.4.7Jonathan Ongbefore adoption34c83d7

lines 74–79why, one step back
74 })
7576 describe('with extnames', function(){77 test(app2);78 })79

tests: add more testsDouglas Christopher Wilsonbefore adoption5019f38

lines 80–97why, one step back
80 describe('with parameters', function(){
81 var app = express();8283 app.use(function(req, res, next){84 res.format({85 'text/plain; charset=utf-8': function(){ res.send('hey') },86 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },87 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }88 });89 });9091 app.use(function(err, req, res, next){92 res.send(err.status, 'Supports: ' + err.types.join(', '));93 });9495 test(app);96 })97

3.4.7Jonathan Ongbefore adoption34c83d7

lines 98–104why, one step back
98 describe('given .default', function(){
99 it('should be invoked instead of auto-responding', function(done){100 request(app3)101 .get('/')102 .set('Accept: text/html')103 .expect('default', done);104 })

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

lines 105–108why, one step back
105 })
106107 describe('in router', function(){108 test(app4);

3.4.7Jonathan Ongbefore adoption34c83d7

line 109why, one step back
109 })

Fix req.next when inside router instanceDouglas Christopher Wilsonbefore adoptionbc9bcb0

lines 110–130why, one step back
110
111 describe('in router', function(){112 var app = express();113 var router = express.Router();114115 router.get('/', function(req, res, next){116 res.format({117 text: function(){ res.send('hey') },118 html: function(){ res.send('<p>hey</p>') },119 json: function(){ res.send({ message: 'hey' }) }120 });121 });122123 router.use(function(err, req, res, next){124 res.send(err.status, 'Supports: ' + err.types.join(', '));125 })126127 app.use(router)128129 test(app)130 })

3.4.7Jonathan Ongbefore adoption34c83d7

lines 131–152why, one step back
131 })
132})133134function test(app) {135 it('should utilize qvalues in negotiation', function(done){136 request(app)137 .get('/')138 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')139 .expect({"message":"hey"}, done);140 })141142 it('should allow wildcard type/subtypes', function(done){143 request(app)144 .get('/')145 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')146 .expect({"message":"hey"}, done);147 })148149 it('should default the Content-Type', function(done){150 request(app)151 .get('/')152 .set('Accept', 'text/html; q=.5, text/plain')

remove res.charset usageJonathan Ongbefore adoption2064f41

line 153why, one step back
153 .expect('Content-Type', 'text/plain; charset=utf-8')

3.4.7Jonathan Ongbefore adoption34c83d7

lines 154–160why, one step back
154 .expect('hey', done);
155 })156157 it('should set the correct charset for the Content-Type', function() {158 request(app)159 .get('/')160 .set('Accept', 'text/html')

remove res.charset usageJonathan Ongbefore adoption2064f41

line 161why, one step back
161 .expect('Content-Type', 'text/html; charset=utf-8');

3.4.7Jonathan Ongbefore adoption34c83d7

lines 162–165why, one step back
162
163 request(app)164 .get('/')165 .set('Accept', 'text/plain')

remove res.charset usageJonathan Ongbefore adoption2064f41

line 166why, one step back
166 .expect('Content-Type', 'text/plain; charset=utf-8');

3.4.7Jonathan Ongbefore adoption34c83d7

lines 167–198why, one step back
167
168 request(app)169 .get('/')170 .set('Accept', 'application/json')171 .expect('Content-Type', 'application/json');172 })173174 it('should Vary: Accept', function(done){175 request(app)176 .get('/')177 .set('Accept', 'text/html; q=.5, text/plain')178 .expect('Vary', 'Accept', done);179 })180181 describe('when Accept is not present', function(){182 it('should invoke the first callback', function(done){183 request(app)184 .get('/')185 .expect('hey', done);186 })187 })188189 describe('when no match is made', function(){190 it('should should respond with 406 not acceptable', function(done){191 request(app)192 .get('/')193 .set('Accept', 'foo/bar')194 .expect('Supports: text/plain, text/html, application/json')195 .expect(406, done)196 })197 })198}