Skip to content

Commit 3f8432b

Browse files
committed
test: enhance req.is() tests with additional cases
Signed-off-by: Sebastian Beltran <[email protected]>
1 parent 7a93112 commit 3f8432b

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
],
9191
"scripts": {
9292
"lint": "eslint .",
93-
"test": "mocha --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
93+
"test": "mocha --require test/support/env --exit --reporter spec --check-leaks test/ test/acceptance/",
9494
"test-ci": "nyc --exclude examples --exclude test --exclude benchmarks --reporter=lcovonly --reporter=text npm test",
9595
"test-cov": "nyc --exclude examples --exclude test --exclude benchmarks --reporter=html --reporter=text npm test",
9696
"test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"

test/req.is.js

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var express = require('..')
44
var request = require('supertest')
5+
var after = require('after')
56

67
describe('req.is()', function () {
78
describe('when given a mime type', function () {
@@ -27,10 +28,24 @@ describe('req.is()', function () {
2728
})
2829

2930
request(app)
30-
.post('/')
31-
.type('application/json')
32-
.send('{}')
33-
.expect(200, 'false', done)
31+
.post('/')
32+
.type('application/json')
33+
.send('{}')
34+
.expect(200, 'false', done)
35+
})
36+
37+
it('should return false when none in list matches', function (done) {
38+
var app = express()
39+
40+
app.use(function (req, res) {
41+
res.json(req.is(['image/jpeg', 'text/html']))
42+
})
43+
44+
request(app)
45+
.post('/')
46+
.type('application/json')
47+
.send('{}')
48+
.expect(200, 'false', done)
3449
})
3550

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

4358
request(app)
44-
.post('/')
45-
.type('application/json; charset=UTF-8')
46-
.send('{}')
47-
.expect(200, '"application/json"', done)
59+
.post('/')
60+
.type('application/json; charset=UTF-8')
61+
.send('{}')
62+
.expect(200, '"application/json"', done)
4863
})
4964
})
5065

5166
describe('when content-type is not present', function(){
52-
it('should return false', function (done) {
67+
it('should return false for single type', function (done) {
5368
var app = express()
5469

5570
app.use(function (req, res) {
@@ -61,6 +76,19 @@ describe('req.is()', function () {
6176
.send('{}')
6277
.expect(200, 'false', done)
6378
})
79+
80+
it('should return false for multiple types', function (done) {
81+
var app = express()
82+
83+
app.use(function (req, res) {
84+
res.json(req.is(['application/json', 'image/jpeg']))
85+
})
86+
87+
request(app)
88+
.post('/')
89+
.send('{}')
90+
.expect(200, 'false', done)
91+
})
6492
})
6593

6694
describe('when given an extension', function(){
@@ -77,6 +105,27 @@ describe('req.is()', function () {
77105
.send('{}')
78106
.expect(200, '"json"', done)
79107
})
108+
109+
it('should lookup the first matching extension from list', function (done) {
110+
var app = express()
111+
var cb = after(2, done)
112+
113+
app.use(function (req, res) {
114+
res.json(req.is(['json', 'html']))
115+
})
116+
117+
request(app)
118+
.post('/')
119+
.type('application/json')
120+
.send('{}')
121+
.expect(200, '"json"', cb)
122+
123+
request(app)
124+
.post('/')
125+
.type('text/html')
126+
.send('{}')
127+
.expect(200, '"html"', cb)
128+
})
80129
})
81130

82131
describe('when given */subtype', function(){
@@ -166,4 +215,31 @@ describe('req.is()', function () {
166215
.expect(200, '"application/json"', done)
167216
})
168217
})
218+
219+
it('should match wildcards in list and return full type or false', function (done){
220+
var app = express()
221+
var cb = after(3, done)
222+
223+
app.use(function (req, res) {
224+
res.json(req.is(['application/*', '*/jpeg']))
225+
})
226+
227+
request(app)
228+
.post('/')
229+
.type('image/jpeg')
230+
.send('{}')
231+
.expect(200, '"image/jpeg"', cb)
232+
233+
request(app)
234+
.post('/')
235+
.type('text/html')
236+
.send('{}')
237+
.expect(200, 'false', cb)
238+
239+
request(app)
240+
.post('/')
241+
.type('application/json')
242+
.send('{}')
243+
.expect(200, '"application/json"', cb)
244+
})
169245
})

0 commit comments

Comments
 (0)