req.accepts.js
This file
Path
test/req.accepts.js
137 lines · 3.2 KiB · JavaScript
Last changed by
remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
var express = require('../') , request = require('supertest');
describe('req', function(){ describe('.accepts(type)', function(){ it('should return true when Accept is not present', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts('json') ? 'yes' : 'no'); });
request(app) .get('/') .expect('yes', done); })
it('should return true when present', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts('json') ? 'yes' : 'no'); });
request(app) .get('/') .set('Accept', 'application/json') .expect('yes', done); })
it('should return false otherwise', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts('json') ? 'yes' : 'no'); });
request(app) .get('/') .set('Accept', 'text/html') .expect('no', done); }) })
it('should accept a comma-delimited list of types', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts('json, html')); });
request(app) .get('/') .set('Accept', 'text/html') .expect('html', done); })
it('should accept an argument list of type names', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts('json', 'html')); });
request(app) .get('/') .set('Accept', 'application/json') .expect('json', done); })
describe('.accepts(types)', function(){ it('should return the first when Accept is not present', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['json', 'html'])); });
request(app) .get('/') .expect('json', done); })
it('should return the first acceptable type', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['json', 'html'])); });
request(app) .get('/') .set('Accept', 'text/html') .expect('html', done); })
it('should return false when no match is made', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['text/html', 'application/json']) ? 'yup' : 'nope'); });
request(app) .get('/') .set('Accept', 'foo/bar, bar/baz') .expect('nope', done); })
it('should take quality into account', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['text/html', 'application/json'])); });
request(app) .get('/') .set('Accept', '*/html; q=.5, application/json') .expect('application/json', done); })
it('should return the first acceptable type with canonical mime types', function(done){ var app = express();
app.use(function(req, res, next){ res.end(req.accepts(['application/json', 'text/html'])); });
request(app) .get('/') .set('Accept', '*/html') .expect('text/html', done); }) })})