Skip to content
Myr

res.format.js

This file

Path

test/res.format.js

248 lines · 5.8 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 2a0c35a point at it.

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 2why, one step back
2

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 3why, one step back
3var after = require('after')

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 4–5why, one step back
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');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 7–20why, 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){

Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615

lines 21–23why, one step back
21 assert(req === a)
22 assert(res === b)23 assert(next === c)

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 24–30why, one step back
24 res.send({ message: 'hey' });
25 }26 });27});2829app1.use(function(err, req, res, next){30 if (!err.types) throw err;

Remove res.send(status, body) signatureMike Tunnicliffebefore adoptione3bd14d

lines 31–32why, one step back
31 res.status(err.status)
32 res.send('Supports: ' + err.types.join(', '))

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 33–45why, one step back
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){

Remove res.send(status, body) signatureMike Tunnicliffebefore adoptione3bd14d

lines 46–47why, one step back
46 res.status(err.status)
47 res.send('Supports: ' + err.types.join(', '))

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 48–54why, one step back
48})
4950var app3 = express();5152app3.use(function(req, res, next){53 res.format({54 text: function(){ res.send('hey') },

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 55–60why, one step back
55 default: function (a, b, c) {
56 assert(req === a)57 assert(res === b)58 assert(next === c)59 res.send('default')60 }

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 61–65why, one step back
61 })
62});6364var app4 = express();65

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 66why, one step back
66app4.get('/', function (req, res) {

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 67–74why, one step back
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){

Remove res.send(status, body) signatureMike Tunnicliffebefore adoptione3bd14d

lines 75–76why, one step back
75 res.status(err.status)
76 res.send('Supports: ' + err.types.join(', '))

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 77–78why, one step back
77})
78

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 79–86why, one step back
79var app5 = express();
8081app5.use(function (req, res, next) {82 res.format({83 default: function () { res.send('hey') }84 });85});86

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 87–108why, one step back
87describe('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){

Remove res.send(status, body) signatureMike Tunnicliffebefore adoptione3bd14d

lines 109–110why, one step back
109 res.status(err.status)
110 res.send('Supports: ' + err.types.join(', '))

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 111–119why, one step back
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('/')

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 122–128why, one step back
122 })
123124 it('should work when only .default is provided', function (done) {125 request(app5)126 .get('/')127 .set('Accept', '*/*')128 .expect('hey', done);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 129why, one step back
129 })

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 130–151why, one step back
130
131 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 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 152–161why, one step back
152 })
153154 describe('in router', function(){155 test(app4);156 })157158 describe('in router', function(){159 var app = express();160 var router = express.Router();161

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 162why, one step back
162 router.get('/', function (req, res) {

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 163–170why, one step back
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){

Remove res.send(status, body) signatureMike Tunnicliffebefore adoptione3bd14d

lines 171–172why, one step back
171 res.status(err.status)
172 res.send('Supports: ' + err.types.join(', '))

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 173–204why, one step back
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 })204

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 205–207why, one step back
205 it('should set the correct charset for the Content-Type', function (done) {
206 var cb = after(3, done)207

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 208–210why, one step back
208 request(app)
209 .get('/')210 .set('Accept', 'text/html')

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 212–215why, one step back
212
213 request(app)214 .get('/')215 .set('Accept', 'text/plain')

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 217–220why, one step back
217
218 request(app)219 .get('/')220 .set('Accept', 'application/json')

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 221why, one step back
221 .expect('Content-Type', 'application/json; charset=utf-8', cb)

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 222–248why, one step back
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(){240 it('should should respond with 406 not acceptable', function(done){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}