fix(deps)!: mime-types@^3.0.0 (#5882)
[express.git] / test / res.format.js
blob59205bfaf423edd08684fea2309e98c4774c77de
1 'use strict'
3 var after = require('after')
4 var express = require('../')
5 , request = require('supertest')
6 , assert = require('assert');
8 var app1 = express();
10 app1.use(function(req, res, next){
11 res.format({
12 'text/plain': function(){
13 res.send('hey');
16 'text/html': function(){
17 res.send('<p>hey</p>');
20 'application/json': function(a, b, c){
21 assert(req === a)
22 assert(res === b)
23 assert(next === c)
24 res.send({ message: 'hey' });
26 });
27 });
29 app1.use(function(err, req, res, next){
30 if (!err.types) throw err;
31 res.status(err.status)
32 res.send('Supports: ' + err.types.join(', '))
35 var app2 = express();
37 app2.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 });
45 app2.use(function(err, req, res, next){
46 res.status(err.status)
47 res.send('Supports: ' + err.types.join(', '))
50 var app3 = express();
52 app3.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')
62 });
64 var app4 = express();
66 app4.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 });
74 app4.use(function(err, req, res, next){
75 res.status(err.status)
76 res.send('Supports: ' + err.types.join(', '))
79 var app5 = express();
81 app5.use(function (req, res, next) {
82 res.format({
83 default: function () { res.send('hey') }
84 });
85 });
87 describe('res', function(){
88 describe('.format(obj)', function(){
89 describe('with canonicalized mime types', function(){
90 test(app1);
93 describe('with extnames', function(){
94 test(app2);
97 describe('with parameters', function(){
98 var app = express();
100 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' }) }
108 app.use(function(err, req, res, next){
109 res.status(err.status)
110 res.send('Supports: ' + err.types.join(', '))
113 test(app);
116 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);
124 it('should work when only .default is provided', function (done) {
125 request(app5)
126 .get('/')
127 .set('Accept', '*/*')
128 .expect('hey', done);
131 it('should be able to invoke other formatter', function (done) {
132 var app = express()
134 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()
144 request(app)
145 .get('/')
146 .set('Accept', 'text/plain')
147 .expect(200)
148 .expect('x-default', '1')
149 .expect('json')
150 .end(done)
154 describe('in router', function(){
155 test(app4);
158 describe('in router', function(){
159 var app = express();
160 var router = express.Router();
162 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' }) }
170 router.use(function(err, req, res, next){
171 res.status(err.status)
172 res.send('Supports: ' + err.types.join(', '))
175 app.use(router)
177 test(app)
182 function 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);
190 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);
197 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);
205 it('should set the correct charset for the Content-Type', function (done) {
206 var cb = after(3, done)
208 request(app)
209 .get('/')
210 .set('Accept', 'text/html')
211 .expect('Content-Type', 'text/html; charset=utf-8', cb)
213 request(app)
214 .get('/')
215 .set('Accept', 'text/plain')
216 .expect('Content-Type', 'text/plain; charset=utf-8', cb)
218 request(app)
219 .get('/')
220 .set('Accept', 'application/json')
221 .expect('Content-Type', 'application/json; charset=utf-8', cb)
224 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);
231 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);
239 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)