2
2
3
3
var express = require ( '..' )
4
4
var request = require ( 'supertest' )
5
+ var after = require ( 'after' )
5
6
6
7
describe ( 'req.is()' , function ( ) {
7
8
describe ( 'when given a mime type' , function ( ) {
@@ -27,10 +28,24 @@ describe('req.is()', function () {
27
28
} )
28
29
29
30
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 )
34
49
} )
35
50
36
51
it ( 'should ignore charset' , function ( done ) {
@@ -41,15 +56,15 @@ describe('req.is()', function () {
41
56
} )
42
57
43
58
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 )
48
63
} )
49
64
} )
50
65
51
66
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 ) {
53
68
var app = express ( )
54
69
55
70
app . use ( function ( req , res ) {
@@ -61,6 +76,19 @@ describe('req.is()', function () {
61
76
. send ( '{}' )
62
77
. expect ( 200 , 'false' , done )
63
78
} )
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
+ } )
64
92
} )
65
93
66
94
describe ( 'when given an extension' , function ( ) {
@@ -77,6 +105,27 @@ describe('req.is()', function () {
77
105
. send ( '{}' )
78
106
. expect ( 200 , '"json"' , done )
79
107
} )
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
+ } )
80
129
} )
81
130
82
131
describe ( 'when given */subtype' , function ( ) {
@@ -166,4 +215,31 @@ describe('req.is()', function () {
166
215
. expect ( 200 , '"application/json"' , done )
167
216
} )
168
217
} )
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
+ } )
169
245
} )
0 commit comments