Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "mocha --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
"test": "mocha --require test/support/env --exit --reporter spec --check-leaks test/ test/acceptance/",
"test-ci": "nyc --exclude examples --exclude test --exclude benchmarks --reporter=lcovonly --reporter=text npm test",
"test-cov": "nyc --exclude examples --exclude test --exclude benchmarks --reporter=html --reporter=text npm test",
"test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
Expand Down
94 changes: 85 additions & 9 deletions test/req.is.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var express = require('..')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var express = require('..')
const express = require('..')

var request = require('supertest')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var request = require('supertest')
const request = require('supertest')

var after = require('after')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var after = require('after')
const after = require('after')

// in modern JS is better to use const instead of var!


describe('req.is()', function () {
describe('when given a mime type', function () {
Expand All @@ -27,10 +28,24 @@ describe('req.is()', function () {
})

request(app)
.post('/')
.type('application/json')
.send('{}')
.expect(200, 'false', done)
.post('/')
.type('application/json')
.send('{}')
.expect(200, 'false', done)
})

it('should return false when none in list matches', function (done) {
var app = express()

app.use(function (req, res) {
res.json(req.is(['image/jpeg', 'text/html']))
})

request(app)
.post('/')
.type('application/json')
.send('{}')
.expect(200, 'false', done)
})

it('should ignore charset', function (done) {
Expand All @@ -41,15 +56,15 @@ describe('req.is()', function () {
})

request(app)
.post('/')
.type('application/json; charset=UTF-8')
.send('{}')
.expect(200, '"application/json"', done)
.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) {
it('should return false for single type', function (done) {
var app = express()

app.use(function (req, res) {
Expand All @@ -61,6 +76,19 @@ describe('req.is()', function () {
.send('{}')
.expect(200, 'false', done)
})

it('should return false for multiple types', function (done) {
var app = express()

app.use(function (req, res) {
res.json(req.is(['application/json', 'image/jpeg']))
})

request(app)
.post('/')
.send('{}')
.expect(200, 'false', done)
})
})

describe('when given an extension', function(){
Expand All @@ -77,6 +105,27 @@ describe('req.is()', function () {
.send('{}')
.expect(200, '"json"', done)
})

it('should lookup the first matching extension from list', function (done) {
var app = express()
var cb = after(2, done)

app.use(function (req, res) {
res.json(req.is(['json', 'html']))
})

request(app)
.post('/')
.type('application/json')
.send('{}')
.expect(200, '"json"', cb)

request(app)
.post('/')
.type('text/html')
.send('{}')
.expect(200, '"html"', cb)
})
})

describe('when given */subtype', function(){
Expand Down Expand Up @@ -166,4 +215,31 @@ describe('req.is()', function () {
.expect(200, '"application/json"', done)
})
})

it('should match wildcards in list and return full type or false', function (done){
var app = express()
var cb = after(3, done)

app.use(function (req, res) {
res.json(req.is(['application/*', '*/jpeg']))
})

request(app)
.post('/')
.type('image/jpeg')
.send('{}')
.expect(200, '"image/jpeg"', cb)

request(app)
.post('/')
.type('text/html')
.send('{}')
.expect(200, 'false', cb)

request(app)
.post('/')
.type('application/json')
.send('{}')
.expect(200, '"application/json"', cb)
})
})
Loading