Compare commits

...

2 Commits

Author SHA1 Message Date
Sebastian Beltran
a865e5402d fix lint error
Signed-off-by: Sebastian Beltran <bjohansebas@gmail.com>
2025-08-12 18:00:28 -05:00
Sebastian Beltran
786fbf417a test: enhance req.is() tests with additional cases
Signed-off-by: Sebastian Beltran <bjohansebas@gmail.com>
2025-08-12 17:59:17 -05:00
2 changed files with 86 additions and 10 deletions

View File

@@ -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/"

View File

@@ -2,6 +2,7 @@
var express = require('..')
var request = require('supertest')
var after = require('after')
describe('req.is()', function () {
describe('when given a mime type', function () {
@@ -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) {
@@ -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) {
@@ -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(){
@@ -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(){
@@ -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)
})
})