fix(deps)!: mime-types@^3.0.0 (#5882)
[express.git] / test / req.accepts.js
blob2066fb51859531cd32187144c1f82a71b768ba2d
1 'use strict'
3 var express = require('../')
4 , request = require('supertest');
6 describe('req', function(){
7 describe('.accepts(type)', function(){
8 it('should return true when Accept is not present', function(done){
9 var app = express();
11 app.use(function(req, res, next){
12 res.end(req.accepts('json') ? 'yes' : 'no');
13 });
15 request(app)
16 .get('/')
17 .expect('yes', done);
20 it('should return true when present', function(done){
21 var app = express();
23 app.use(function(req, res, next){
24 res.end(req.accepts('json') ? 'yes' : 'no');
25 });
27 request(app)
28 .get('/')
29 .set('Accept', 'application/json')
30 .expect('yes', done);
33 it('should return false otherwise', function(done){
34 var app = express();
36 app.use(function(req, res, next){
37 res.end(req.accepts('json') ? 'yes' : 'no');
38 });
40 request(app)
41 .get('/')
42 .set('Accept', 'text/html')
43 .expect('no', done);
47 it('should accept an argument list of type names', function(done){
48 var app = express();
50 app.use(function(req, res, next){
51 res.end(req.accepts('json', 'html'));
52 });
54 request(app)
55 .get('/')
56 .set('Accept', 'application/json')
57 .expect('json', done);
60 describe('.accepts(types)', function(){
61 it('should return the first when Accept is not present', function(done){
62 var app = express();
64 app.use(function(req, res, next){
65 res.end(req.accepts(['json', 'html']));
66 });
68 request(app)
69 .get('/')
70 .expect('json', done);
73 it('should return the first acceptable type', function(done){
74 var app = express();
76 app.use(function(req, res, next){
77 res.end(req.accepts(['json', 'html']));
78 });
80 request(app)
81 .get('/')
82 .set('Accept', 'text/html')
83 .expect('html', done);
86 it('should return false when no match is made', function(done){
87 var app = express();
89 app.use(function(req, res, next){
90 res.end(req.accepts(['text/html', 'application/json']) ? 'yup' : 'nope');
91 });
93 request(app)
94 .get('/')
95 .set('Accept', 'foo/bar, bar/baz')
96 .expect('nope', done);
99 it('should take quality into account', function(done){
100 var app = express();
102 app.use(function(req, res, next){
103 res.end(req.accepts(['text/html', 'application/json']));
106 request(app)
107 .get('/')
108 .set('Accept', '*/html; q=.5, application/json')
109 .expect('application/json', done);
112 it('should return the first acceptable type with canonical mime types', function(done){
113 var app = express();
115 app.use(function(req, res, next){
116 res.end(req.accepts(['application/json', 'text/html']));
119 request(app)
120 .get('/')
121 .set('Accept', '*/html')
122 .expect('text/html', done);