1
- /* eslint-env node */
2
- var url = require ( "url" ) ;
3
- var fs = require ( "fs" ) ;
4
- var getRawBody = require ( "raw-body" ) ;
1
+ "use strict" ;
2
+
3
+ const url = require ( "url" ) ;
4
+ const fs = require ( "fs" ) ;
5
+ const getRawBody = require ( "raw-body" ) ;
6
+
7
+ let cspLog = "" ;
5
8
6
- var cspLog = "" ;
7
9
/**
8
10
* Keep in sync with /test/mock.php
9
11
*/
10
12
function cleanCallback ( callback ) {
11
13
return callback . replace ( / [ ^ a - z 0 - 9 _ ] / gi, "" ) ;
12
14
}
13
15
14
- var mocks = {
16
+ const mocks = {
15
17
contentType : function ( req , resp ) {
16
18
resp . writeHead ( 200 , {
17
19
"content-type" : req . query . contentType
18
20
} ) ;
19
21
resp . end ( req . query . response ) ;
20
22
} ,
21
23
wait : function ( req , resp ) {
22
- var wait = Number ( req . query . wait ) * 1000 ;
24
+ const wait = Number ( req . query . wait ) * 1000 ;
23
25
setTimeout ( function ( ) {
24
26
if ( req . query . script ) {
25
27
resp . writeHead ( 200 , { "content-type" : "text/javascript" } ) ;
@@ -44,7 +46,7 @@ var mocks = {
44
46
} , next ) ;
45
47
} ,
46
48
xml : function ( req , resp , next ) {
47
- var content = "<math><calculation>5-2</calculation><result>3</result></math>" ;
49
+ const content = "<math><calculation>5-2</calculation><result>3</result></math>" ;
48
50
resp . writeHead ( 200 , { "content-type" : "text/xml" } ) ;
49
51
50
52
if ( req . query . cal === "5-2" ) {
@@ -59,7 +61,7 @@ var mocks = {
59
61
}
60
62
} , next ) ;
61
63
} ,
62
- atom : function ( req , resp , next ) {
64
+ atom : function ( _req , resp ) {
63
65
resp . writeHead ( 200 , { "content-type" : "atom+xml" } ) ;
64
66
resp . end ( "<root><element /></root>" ) ;
65
67
} ,
@@ -73,7 +75,7 @@ var mocks = {
73
75
}
74
76
resp . end ( "QUnit.assert.ok( true, \"mock executed\" );" ) ;
75
77
} ,
76
- testbar : function ( req , resp ) {
78
+ testbar : function ( _req , resp ) {
77
79
resp . writeHead ( 200 ) ;
78
80
resp . end (
79
81
"this.testBar = 'bar'; " +
@@ -96,7 +98,7 @@ var mocks = {
96
98
}
97
99
} ,
98
100
jsonp : function ( req , resp , next ) {
99
- var callback ;
101
+ let callback ;
100
102
if ( Array . isArray ( req . query . callback ) ) {
101
103
callback = Promise . resolve ( req . query . callback [ req . query . callback . length - 1 ] ) ;
102
104
} else if ( req . query . callback ) {
@@ -108,22 +110,22 @@ var mocks = {
108
110
return body . trim ( ) . replace ( "callback=" , "" ) ;
109
111
} ) ;
110
112
}
111
- var json = req . query . array ?
113
+ const json = req . query . array ?
112
114
JSON . stringify (
113
115
[ { name : "John" , age : 21 } , { name : "Peter" , age : 25 } ]
114
116
) :
115
117
JSON . stringify (
116
118
{ data : { lang : "en" , length : 25 } }
117
119
) ;
118
120
callback . then ( function ( cb ) {
119
- resp . end ( cleanCallback ( cb ) + "(" + json + ")" ) ;
121
+ resp . end ( ` ${ cleanCallback ( cb ) } ( ${ json } )` ) ;
120
122
} , next ) ;
121
123
} ,
122
124
xmlOverJsonp : function ( req , resp ) {
123
- var callback = req . query . callback ;
124
- var body = fs . readFileSync ( __dirname + " /data/with_fries.xml" ) . toString ( ) ;
125
+ const callback = req . query . callback ;
126
+ const body = fs . readFileSync ( ` ${ __dirname } /data/with_fries.xml` ) . toString ( ) ;
125
127
resp . writeHead ( 200 ) ;
126
- resp . end ( cleanCallback ( callback ) + "(" + JSON . stringify ( body ) + " )\n" ) ;
128
+ resp . end ( ` ${ cleanCallback ( callback ) } ( ${ JSON . stringify ( body ) } )\n` ) ;
127
129
} ,
128
130
error : function ( req , resp ) {
129
131
if ( req . query . json ) {
@@ -144,8 +146,8 @@ var mocks = {
144
146
"constructor" : "prototype collision (constructor)"
145
147
} ) ;
146
148
req . query . keys . split ( "|" ) . forEach ( function ( key ) {
147
- if ( req . headers [ key . toLowerCase ( ) ] ) {
148
- resp . write ( key + ": " + req . headers [ key . toLowerCase ( ) ] + "\n" ) ;
149
+ if ( key . toLowerCase ( ) in req . headers ) {
150
+ resp . write ( ` ${ key } : ${ req . headers [ key . toLowerCase ( ) ] } \n` ) ;
149
151
}
150
152
} ) ;
151
153
resp . end ( ) ;
@@ -163,16 +165,16 @@ var mocks = {
163
165
} ,
164
166
echoHtml : function ( req , resp , next ) {
165
167
resp . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
166
- resp . write ( " <div id='method'>" + req . method + " </div>" ) ;
167
- resp . write ( " <div id='query'>" + req . parsed . search . slice ( 1 ) + " </div>" ) ;
168
+ resp . write ( ` <div id='method'>${ req . method } </div>` ) ;
169
+ resp . write ( ` <div id='query'>${ req . parsed . search . slice ( 1 ) } </div>` ) ;
168
170
getBody ( req ) . then ( function ( body ) {
169
- resp . write ( " <div id='data'>" + body + " </div>" ) ;
171
+ resp . write ( ` <div id='data'>${ body } </div>` ) ;
170
172
resp . end ( body ) ;
171
173
} , next ) ;
172
174
} ,
173
175
etag : function ( req , resp ) {
174
- var hash = Number ( req . query . ts ) . toString ( 36 ) ;
175
- var etag = "W/\"" + hash + "\"" ;
176
+ const hash = Number ( req . query . ts ) . toString ( 36 ) ;
177
+ const etag = `W/" ${ hash } "` ;
176
178
if ( req . headers [ "if-none-match" ] === etag ) {
177
179
resp . writeHead ( 304 ) ;
178
180
resp . end ( ) ;
@@ -183,8 +185,8 @@ var mocks = {
183
185
} ) ;
184
186
resp . end ( ) ;
185
187
} ,
186
- ims : function ( req , resp , next ) {
187
- var ts = req . query . ts ;
188
+ ims : function ( req , resp ) {
189
+ const ts = req . query . ts ;
188
190
if ( req . headers [ "if-modified-since" ] === ts ) {
189
191
resp . writeHead ( 304 ) ;
190
192
resp . end ( ) ;
@@ -195,40 +197,44 @@ var mocks = {
195
197
} ) ;
196
198
resp . end ( ) ;
197
199
} ,
198
- status : function ( req , resp , next ) {
200
+ status : function ( req , resp ) {
199
201
resp . writeHead ( Number ( req . query . code ) ) ;
200
202
resp . end ( ) ;
201
203
} ,
202
204
testHTML : function ( req , resp ) {
203
205
resp . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
204
- var body = fs . readFileSync ( __dirname + "/data/test.include.html" ) . toString ( ) ;
205
- body = body . replace ( / { { baseURL} } / g, req . query . baseURL ) ;
206
+ const body = fs
207
+ . readFileSync ( `${ __dirname } /data/test.include.html` )
208
+ . toString ( )
209
+ . replace ( / { { baseURL} } / g, req . query . baseURL ) ;
206
210
resp . end ( body ) ;
207
211
} ,
208
- cspFrame : function ( req , resp ) {
212
+ cspFrame : function ( _req , resp ) {
209
213
resp . writeHead ( 200 , {
210
214
"Content-Type" : "text/html" ,
211
- "Content-Security-Policy" : "default-src 'self'; report-uri /base/test/data/mock.php?action=cspLog"
215
+ "Content-Security-Policy" : "default-src 'self'; " +
216
+ "report-uri /base/test/data/mock.php?action=cspLog"
212
217
} ) ;
213
- var body = fs . readFileSync ( __dirname + " /data/csp.include.html" ) . toString ( ) ;
218
+ const body = fs . readFileSync ( ` ${ __dirname } /data/csp.include.html` ) . toString ( ) ;
214
219
resp . end ( body ) ;
215
220
} ,
216
221
cspNonce : function ( req , resp ) {
217
- var testParam = req . query . test ? "-" + req . query . test : "" ;
222
+ const testParam = req . query . test ? `- ${ req . query . test } ` : "" ;
218
223
resp . writeHead ( 200 , {
219
224
"Content-Type" : "text/html" ,
220
- "Content-Security-Policy" : "script-src 'nonce-jquery+hardcoded+nonce'; report-uri /base/test/data/mock.php?action=cspLog"
225
+ "Content-Security-Policy" : "script-src 'nonce-jquery+hardcoded+nonce'; " +
226
+ "report-uri /base/test/data/mock.php?action=cspLog"
221
227
} ) ;
222
- var body = fs . readFileSync (
223
- __dirname + " /data/csp-nonce" + testParam + " .html" ) . toString ( ) ;
228
+ const body = fs . readFileSync (
229
+ ` ${ __dirname } /data/csp-nonce${ testParam } .html` ) . toString ( ) ;
224
230
resp . end ( body ) ;
225
231
} ,
226
- cspLog : function ( req , resp ) {
232
+ cspLog : function ( _req , resp ) {
227
233
cspLog = "error" ;
228
234
resp . writeHead ( 200 ) ;
229
235
resp . end ( ) ;
230
236
} ,
231
- cspClean : function ( req , resp ) {
237
+ cspClean : function ( _req , resp ) {
232
238
cspLog = "" ;
233
239
resp . writeHead ( 200 ) ;
234
240
resp . end ( ) ;
@@ -240,14 +246,14 @@ var mocks = {
240
246
resp . writeHead ( 404 , { "Content-Type" : "text/html; charset=UTF-8" } ) ;
241
247
}
242
248
if ( req . query . callback ) {
243
- resp . end ( cleanCallback ( req . query . callback ) +
244
- " ( {\ "status\ ": 404, \ "msg\ ": \ "Not Found\ "} )" ) ;
249
+ resp . end ( ` ${ cleanCallback ( req . query . callback )
250
+ } ( {"status": 404, "msg": "Not Found"} )` ) ;
245
251
} else {
246
252
resp . end ( "QUnit.assert.ok( false, \"Mock return erroneously executed\" );" ) ;
247
253
}
248
254
}
249
255
} ;
250
- var handlers = {
256
+ const handlers = {
251
257
"test/data/mock.php" : function ( req , resp , next ) {
252
258
if ( ! mocks [ req . query . action ] ) {
253
259
resp . writeHead ( 400 ) ;
@@ -257,11 +263,11 @@ var handlers = {
257
263
}
258
264
mocks [ req . query . action ] ( req , resp , next ) ;
259
265
} ,
260
- "test/data/support/csp.log" : function ( req , resp ) {
266
+ "test/data/support/csp.log" : function ( _req , resp ) {
261
267
resp . writeHead ( 200 ) ;
262
268
resp . end ( cspLog ) ;
263
269
} ,
264
- "test/data/404.txt" : function ( req , resp ) {
270
+ "test/data/404.txt" : function ( _req , resp ) {
265
271
resp . writeHead ( 404 ) ;
266
272
resp . end ( "" ) ;
267
273
}
@@ -276,21 +282,23 @@ var handlers = {
276
282
* Express versions of these (e.g. no req.path, req.query, resp.set).
277
283
*/
278
284
function MockserverMiddlewareFactory ( ) {
285
+
279
286
/**
280
287
* @param {http.IncomingMessage } req
281
288
* @param {http.ServerResponse } resp
282
289
* @param {Function } next Continue request handling
283
290
*/
284
291
return function ( req , resp , next ) {
285
- var parsed = url . parse ( req . url , /* parseQuery */ true ) ,
286
- path = parsed . pathname . replace ( / ^ \/ b a s e \/ / , "" ) ,
287
- query = parsed . query ,
288
- subReq = Object . assign ( Object . create ( req ) , {
289
- query : query ,
290
- parsed : parsed
291
- } ) ;
292
+ const parsed = url . parse ( req . url , /* parseQuery */ true ) ;
293
+ let path = parsed . pathname . replace ( / ^ \/ b a s e \/ / , "" ) ;
294
+ const query = parsed . query ;
295
+ const subReq = Object . assign ( Object . create ( req ) , {
296
+ query : query ,
297
+ parsed : parsed
298
+ } ) ;
292
299
293
300
if ( / ^ t e s t \/ d a t a \/ m o c k .p h p \/ / . test ( path ) ) {
301
+
294
302
// Support REST-like Apache PathInfo
295
303
path = "test\/data\/mock.php" ;
296
304
}
0 commit comments