req.is.js
This file
Path
test/req.is.js
169 lines · 3.7 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170'use strict'
var express = require('..')var request = require('supertest')
describe('req.is()', function () { describe('when given a mime type', function () { it('should return the type when matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('application/json')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, '"application/json"', done) })
it('should return false when not matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('image/jpeg')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, 'false', done) })
it('should ignore charset', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('application/json')) })
request(app) .post('/') .type('application/json; charset=UTF-8') .send('{}') .expect(200, '"application/json"', done) }) })
describe('when content-type is not present', function(){ it('should return false', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('application/json')) })
request(app) .post('/') .send('{}') .expect(200, 'false', done) }) })
describe('when given an extension', function(){ it('should lookup the mime type', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('json')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, '"json"', done) }) })
describe('when given */subtype', function(){ it('should return the full type when matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('*/json')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, '"application/json"', done) })
it('should return false when not matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('*/html')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, 'false', done) })
it('should ignore charset', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('*/json')) })
request(app) .post('/') .type('application/json; charset=UTF-8') .send('{}') .expect(200, '"application/json"', done) }) })
describe('when given type/*', function(){ it('should return the full type when matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('application/*')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, '"application/json"', done) })
it('should return false when not matching', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('text/*')) })
request(app) .post('/') .type('application/json') .send('{}') .expect(200, 'false', done) })
it('should ignore charset', function (done) { var app = express()
app.use(function (req, res) { res.json(req.is('application/*')) })
request(app) .post('/') .type('application/json; charset=UTF-8') .send('{}') .expect(200, '"application/json"', done) }) })})