req.accepts.js
This file
Path
test/req.accepts.js
124 lines · 2.9 KiB · JavaScript
Last changed by
remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
History of this fileTake it
The walk stopped after 200 commits: lines older than 2e68ddb point at it.
expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption643397e
line 3why, one step back3 , request = require('supertest');expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb
lines 4–124why, one step back45describe('req', function(){6 describe('.accepts(type)', function(){7 it('should return true when Accept is not present', function(done){8 var app = express();910 app.use(function(req, res, next){11 res.end(req.accepts('json') ? 'yes' : 'no');12 });1314 request(app)15 .get('/')16 .expect('yes', done);17 })1819 it('should return true when present', function(done){20 var app = express();2122 app.use(function(req, res, next){23 res.end(req.accepts('json') ? 'yes' : 'no');24 });2526 request(app)27 .get('/')28 .set('Accept', 'application/json')29 .expect('yes', done);30 })3132 it('should return false otherwise', function(done){33 var app = express();3435 app.use(function(req, res, next){36 res.end(req.accepts('json') ? 'yes' : 'no');37 });3839 request(app)40 .get('/')41 .set('Accept', 'text/html')42 .expect('no', done);43 })44 })4546 it('should accept an argument list of type names', function(done){47 var app = express();4849 app.use(function(req, res, next){50 res.end(req.accepts('json', 'html'));51 });5253 request(app)54 .get('/')55 .set('Accept', 'application/json')56 .expect('json', done);57 })5859 describe('.accepts(types)', function(){60 it('should return the first when Accept is not present', function(done){61 var app = express();6263 app.use(function(req, res, next){64 res.end(req.accepts(['json', 'html']));65 });6667 request(app)68 .get('/')69 .expect('json', done);70 })7172 it('should return the first acceptable type', function(done){73 var app = express();7475 app.use(function(req, res, next){76 res.end(req.accepts(['json', 'html']));77 });7879 request(app)80 .get('/')81 .set('Accept', 'text/html')82 .expect('html', done);83 })8485 it('should return false when no match is made', function(done){86 var app = express();8788 app.use(function(req, res, next){89 res.end(req.accepts(['text/html', 'application/json']) ? 'yup' : 'nope');90 });9192 request(app)93 .get('/')94 .set('Accept', 'foo/bar, bar/baz')95 .expect('nope', done);96 })9798 it('should take quality into account', function(done){99 var app = express();100101 app.use(function(req, res, next){102 res.end(req.accepts(['text/html', 'application/json']));103 });104105 request(app)106 .get('/')107 .set('Accept', '*/html; q=.5, application/json')108 .expect('application/json', done);109 })110111 it('should return the first acceptable type with canonical mime types', function(done){112 var app = express();113114 app.use(function(req, res, next){115 res.end(req.accepts(['application/json', 'text/html']));116 });117118 request(app)119 .get('/')120 .set('Accept', '*/html')121 .expect('text/html', done);122 })123 })124})