Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

159 lines · 3.7 KiB · JavaScript

Last changed by

tests: remove more mocking usesDouglas Christopher Wilsonbefore adoption855176b

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f4968a point at it.

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 1–83why, 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})6970describe('res', function(){71 describe('.format(obj)', function(){72 describe('with canonicalized mime types', function(){73 test(app1);74 })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('/')

tests: remove more mocking usesDouglas Christopher Wilsonbefore adoption855176b

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

3.15.1Douglas Christopher Wilsonbefore adoption9f4968a

lines 85–159why, one step back
85 .expect('default', done);
86 })87 })8889 describe('in router', function(){90 test(app4);91 })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')100 .expect({"message":"hey"}, done);101 })102103 it('should allow wildcard type/subtypes', function(done){104 request(app)105 .get('/')106 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')107 .expect({"message":"hey"}, done);108 })109110 it('should default the Content-Type', function(done){111 request(app)112 .get('/')113 .set('Accept', 'text/html; q=.5, text/plain')114 .expect('Content-Type', 'text/plain; charset=utf-8')115 .expect('hey', done);116 })117118 it('should set the correct charset for the Content-Type', function() {119 request(app)120 .get('/')121 .set('Accept', 'text/html')122 .expect('Content-Type', 'text/html; charset=utf-8');123124 request(app)125 .get('/')126 .set('Accept', 'text/plain')127 .expect('Content-Type', 'text/plain; charset=utf-8');128129 request(app)130 .get('/')131 .set('Accept', 'application/json')132 .expect('Content-Type', 'application/json');133 })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}