Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

248 lines · 5.8 KiB · JavaScript

Last changed by

fix(test): remove duplicate word (#6456)dufucunbefore adoptionf9954dd

History of this file

Take it

The walk stopped after 200 commits: lines older than 2027b87 point at it.

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 1–5why, one step back
1'use strict'
23var after = require('after')4var express = require('../')5 , request = require('supertest')

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 6why, one step back
6 , assert = require('node:assert');

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 7–239why, one step back
7
8var app1 = express();910app1.use(function(req, res, next){11 res.format({12 'text/plain': function(){13 res.send('hey');14 },1516 'text/html': function(){17 res.send('<p>hey</p>');18 },1920 'application/json': function(a, b, c){21 assert(req === a)22 assert(res === b)23 assert(next === c)24 res.send({ message: 'hey' });25 }26 });27});2829app1.use(function(err, req, res, next){30 if (!err.types) throw err;31 res.status(err.status)32 res.send('Supports: ' + err.types.join(', '))33})3435var app2 = express();3637app2.use(function(req, res, next){38 res.format({39 text: function(){ res.send('hey') },40 html: function(){ res.send('<p>hey</p>') },41 json: function(){ res.send({ message: 'hey' }) }42 });43});4445app2.use(function(err, req, res, next){46 res.status(err.status)47 res.send('Supports: ' + err.types.join(', '))48})4950var app3 = express();5152app3.use(function(req, res, next){53 res.format({54 text: function(){ res.send('hey') },55 default: function (a, b, c) {56 assert(req === a)57 assert(res === b)58 assert(next === c)59 res.send('default')60 }61 })62});6364var app4 = express();6566app4.get('/', function (req, res) {67 res.format({68 text: function(){ res.send('hey') },69 html: function(){ res.send('<p>hey</p>') },70 json: function(){ res.send({ message: 'hey' }) }71 });72});7374app4.use(function(err, req, res, next){75 res.status(err.status)76 res.send('Supports: ' + err.types.join(', '))77})7879var app5 = express();8081app5.use(function (req, res, next) {82 res.format({83 default: function () { res.send('hey') }84 });85});8687describe('res', function(){88 describe('.format(obj)', function(){89 describe('with canonicalized mime types', function(){90 test(app1);91 })9293 describe('with extnames', function(){94 test(app2);95 })9697 describe('with parameters', function(){98 var app = express();99100 app.use(function(req, res, next){101 res.format({102 'text/plain; charset=utf-8': function(){ res.send('hey') },103 'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },104 'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }105 });106 });107108 app.use(function(err, req, res, next){109 res.status(err.status)110 res.send('Supports: ' + err.types.join(', '))111 });112113 test(app);114 })115116 describe('given .default', function(){117 it('should be invoked instead of auto-responding', function(done){118 request(app3)119 .get('/')120 .set('Accept', 'text/html')121 .expect('default', done);122 })123124 it('should work when only .default is provided', function (done) {125 request(app5)126 .get('/')127 .set('Accept', '*/*')128 .expect('hey', done);129 })130131 it('should be able to invoke other formatter', function (done) {132 var app = express()133134 app.use(function (req, res, next) {135 res.format({136 json: function () { res.send('json') },137 default: function () {138 res.header('x-default', '1')139 this.json()140 }141 })142 })143144 request(app)145 .get('/')146 .set('Accept', 'text/plain')147 .expect(200)148 .expect('x-default', '1')149 .expect('json')150 .end(done)151 })152 })153154 describe('in router', function(){155 test(app4);156 })157158 describe('in router', function(){159 var app = express();160 var router = express.Router();161162 router.get('/', function (req, res) {163 res.format({164 text: function(){ res.send('hey') },165 html: function(){ res.send('<p>hey</p>') },166 json: function(){ res.send({ message: 'hey' }) }167 });168 });169170 router.use(function(err, req, res, next){171 res.status(err.status)172 res.send('Supports: ' + err.types.join(', '))173 })174175 app.use(router)176177 test(app)178 })179 })180})181182function test(app) {183 it('should utilize qvalues in negotiation', function(done){184 request(app)185 .get('/')186 .set('Accept', 'text/html; q=.5, application/json, */*; q=.1')187 .expect({"message":"hey"}, done);188 })189190 it('should allow wildcard type/subtypes', function(done){191 request(app)192 .get('/')193 .set('Accept', 'text/html; q=.5, application/*, */*; q=.1')194 .expect({"message":"hey"}, done);195 })196197 it('should default the Content-Type', function(done){198 request(app)199 .get('/')200 .set('Accept', 'text/html; q=.5, text/plain')201 .expect('Content-Type', 'text/plain; charset=utf-8')202 .expect('hey', done);203 })204205 it('should set the correct charset for the Content-Type', function (done) {206 var cb = after(3, done)207208 request(app)209 .get('/')210 .set('Accept', 'text/html')211 .expect('Content-Type', 'text/html; charset=utf-8', cb)212213 request(app)214 .get('/')215 .set('Accept', 'text/plain')216 .expect('Content-Type', 'text/plain; charset=utf-8', cb)217218 request(app)219 .get('/')220 .set('Accept', 'application/json')221 .expect('Content-Type', 'application/json; charset=utf-8', cb)222 })223224 it('should Vary: Accept', function(done){225 request(app)226 .get('/')227 .set('Accept', 'text/html; q=.5, text/plain')228 .expect('Vary', 'Accept', done);229 })230231 describe('when Accept is not present', function(){232 it('should invoke the first callback', function(done){233 request(app)234 .get('/')235 .expect('hey', done);236 })237 })238239 describe('when no match is made', function(){

fix(test): remove duplicate word (#6456)dufucunbefore adoptionf9954dd

line 240why, one step back
240 it('should respond with 406 not acceptable', function(done){

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 241–248why, one step back
241 request(app)
242 .get('/')243 .set('Accept', 'foo/bar')244 .expect('Supports: text/plain, text/html, application/json')245 .expect(406, done)246 })247 })248}