Files
express/test/res.format.js

249 lines
5.8 KiB
JavaScript
Raw Normal View History

2022-02-09 01:07:08 -05:00
'use strict'
2012-02-18 17:13:40 -08:00
var after = require('after')
2012-02-18 17:13:40 -08:00
var express = require('../')
2014-03-05 22:06:14 -08:00
, request = require('supertest')
, assert = require('node:assert');
2012-02-18 17:13:40 -08:00
var app1 = express();
2012-02-18 17:13:40 -08:00
app1.use(function(req, res, next){
res.format({
2012-02-18 17:13:40 -08:00
'text/plain': function(){
res.send('hey');
2012-02-18 17:13:40 -08:00
},
'text/html': function(){
res.send('<p>hey</p>');
},
2014-03-05 22:06:14 -08:00
2012-02-21 10:23:20 -08:00
'application/json': function(a, b, c){
assert(req === a)
assert(res === b)
assert(next === c)
2012-02-18 17:13:40 -08:00
res.send({ message: 'hey' });
}
});
});
app1.use(function(err, req, res, next){
2012-03-23 17:58:15 -07:00
if (!err.types) throw err;
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
})
var app2 = express();
app2.use(function(req, res, next){
res.format({
text: function(){ res.send('hey') },
html: function(){ res.send('<p>hey</p>') },
json: function(){ res.send({ message: 'hey' }) }
});
});
app2.use(function(err, req, res, next){
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
2012-02-18 17:13:40 -08:00
})
var app3 = express();
app3.use(function(req, res, next){
res.format({
text: function(){ res.send('hey') },
default: function (a, b, c) {
assert(req === a)
assert(res === b)
assert(next === c)
res.send('default')
}
})
});
var app4 = express();
app4.get('/', function (req, res) {
res.format({
text: function(){ res.send('hey') },
html: function(){ res.send('<p>hey</p>') },
json: function(){ res.send({ message: 'hey' }) }
});
});
app4.use(function(err, req, res, next){
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
})
var app5 = express();
app5.use(function (req, res, next) {
res.format({
default: function () { res.send('hey') }
});
});
2013-08-04 20:32:08 +05:30
describe('res', function(){
describe('.format(obj)', function(){
describe('with canonicalized mime types', function(){
test(app1);
})
describe('with extnames', function(){
test(app2);
2012-02-18 17:13:40 -08:00
})
2014-06-06 00:38:14 -04:00
describe('with parameters', function(){
var app = express();
app.use(function(req, res, next){
res.format({
'text/plain; charset=utf-8': function(){ res.send('hey') },
'text/html; foo=bar; bar=baz': function(){ res.send('<p>hey</p>') },
'application/json; q=0.5': function(){ res.send({ message: 'hey' }) }
});
});
app.use(function(err, req, res, next){
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
2014-06-06 00:38:14 -04:00
});
test(app);
})
describe('given .default', function(){
it('should be invoked instead of auto-responding', function(done){
request(app3)
.get('/')
2015-01-21 03:17:30 -05:00
.set('Accept', 'text/html')
.expect('default', done);
})
it('should work when only .default is provided', function (done) {
request(app5)
.get('/')
.set('Accept', '*/*')
.expect('hey', done);
})
it('should be able to invoke other formatter', function (done) {
var app = express()
app.use(function (req, res, next) {
res.format({
json: function () { res.send('json') },
default: function () {
res.header('x-default', '1')
this.json()
}
})
})
request(app)
.get('/')
.set('Accept', 'text/plain')
.expect(200)
.expect('x-default', '1')
.expect('json')
.end(done)
})
})
describe('in router', function(){
test(app4);
})
2014-05-09 17:33:26 -04:00
describe('in router', function(){
var app = express();
var router = express.Router();
router.get('/', function (req, res) {
res.format({
text: function(){ res.send('hey') },
html: function(){ res.send('<p>hey</p>') },
json: function(){ res.send({ message: 'hey' }) }
});
});
router.use(function(err, req, res, next){
res.status(err.status)
res.send('Supports: ' + err.types.join(', '))
})
app.use(router)
test(app)
})
})
})
2012-02-18 17:13:40 -08:00
function test(app) {
it('should utilize qvalues in negotiation', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, application/json, */*; q=.1')
2013-08-04 20:32:08 +05:30
.expect({"message":"hey"}, done);
})
it('should allow wildcard type/subtypes', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, application/*, */*; q=.1')
2013-08-04 20:32:08 +05:30
.expect({"message":"hey"}, done);
})
it('should default the Content-Type', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, text/plain')
2014-03-07 16:46:54 -08:00
.expect('Content-Type', 'text/plain; charset=utf-8')
2012-06-26 17:14:07 -07:00
.expect('hey', done);
})
it('should set the correct charset for the Content-Type', function (done) {
var cb = after(3, done)
request(app)
.get('/')
.set('Accept', 'text/html')
.expect('Content-Type', 'text/html; charset=utf-8', cb)
request(app)
.get('/')
.set('Accept', 'text/plain')
.expect('Content-Type', 'text/plain; charset=utf-8', cb)
request(app)
.get('/')
.set('Accept', 'application/json')
.expect('Content-Type', 'application/json; charset=utf-8', cb)
})
it('should Vary: Accept', function(done){
request(app)
.get('/')
.set('Accept', 'text/html; q=.5, text/plain')
2012-06-26 17:14:07 -07:00
.expect('Vary', 'Accept', done);
})
describe('when Accept is not present', function(){
it('should invoke the first callback', function(done){
2012-02-18 17:13:40 -08:00
request(app)
.get('/')
.expect('hey', done);
2012-02-18 17:13:40 -08:00
})
})
2012-02-18 17:13:40 -08:00
describe('when no match is made', function(){
it('should respond with 406 not acceptable', function(done){
request(app)
.get('/')
.set('Accept', 'foo/bar')
2012-06-26 17:14:07 -07:00
.expect('Supports: text/plain, text/html, application/json')
.expect(406, done)
})
2012-02-18 17:13:40 -08:00
})
}