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
} ,
@@ -77,14 +79,14 @@ var mocks = {
77
79
}
78
80
79
81
if ( req . query . callback ) {
80
- resp . end ( cleanCallback ( req . query . callback ) + "(" + JSON . stringify ( {
82
+ resp . end ( ` ${ cleanCallback ( req . query . callback ) } ( ${ JSON . stringify ( {
81
83
headers : req . headers
82
- } ) + ")" ) ;
84
+ } ) } )` ) ;
83
85
} else {
84
86
resp . end ( "QUnit.assert.ok( true, \"mock executed\" );" ) ;
85
87
}
86
88
} ,
87
- testbar : function ( req , resp ) {
89
+ testbar : function ( _req , resp ) {
88
90
resp . writeHead ( 200 ) ;
89
91
resp . end (
90
92
"this.testBar = 'bar'; " +
@@ -110,7 +112,7 @@ var mocks = {
110
112
}
111
113
} ,
112
114
jsonp : function ( req , resp , next ) {
113
- var callback ;
115
+ let callback ;
114
116
if ( Array . isArray ( req . query . callback ) ) {
115
117
callback = Promise . resolve ( req . query . callback [ req . query . callback . length - 1 ] ) ;
116
118
} else if ( req . query . callback ) {
@@ -122,22 +124,22 @@ var mocks = {
122
124
return body . trim ( ) . replace ( "callback=" , "" ) ;
123
125
} ) ;
124
126
}
125
- var json = req . query . array ?
127
+ const json = req . query . array ?
126
128
JSON . stringify (
127
129
[ { name : "John" , age : 21 } , { name : "Peter" , age : 25 } ]
128
130
) :
129
131
JSON . stringify (
130
132
{ data : { lang : "en" , length : 25 } }
131
133
) ;
132
134
callback . then ( function ( cb ) {
133
- resp . end ( cleanCallback ( cb ) + "(" + json + ")" ) ;
135
+ resp . end ( ` ${ cleanCallback ( cb ) } ( ${ json } )` ) ;
134
136
} , next ) ;
135
137
} ,
136
138
xmlOverJsonp : function ( req , resp ) {
137
- var callback = req . query . callback ;
138
- var body = fs . readFileSync ( __dirname + " /data/with_fries.xml" ) . toString ( ) ;
139
+ const callback = req . query . callback ;
140
+ const body = fs . readFileSync ( ` ${ __dirname } /data/with_fries.xml` ) . toString ( ) ;
139
141
resp . writeHead ( 200 ) ;
140
- resp . end ( cleanCallback ( callback ) + "(" + JSON . stringify ( body ) + " )\n" ) ;
142
+ resp . end ( ` ${ cleanCallback ( callback ) } ( ${ JSON . stringify ( body ) } )\n` ) ;
141
143
} ,
142
144
error : function ( req , resp ) {
143
145
if ( req . query . json ) {
@@ -159,7 +161,7 @@ var mocks = {
159
161
} ) ;
160
162
req . query . keys . split ( "|" ) . forEach ( function ( key ) {
161
163
if ( key . toLowerCase ( ) in req . headers ) {
162
- resp . write ( key + ": " + req . headers [ key . toLowerCase ( ) ] + "\n" ) ;
164
+ resp . write ( ` ${ key } : ${ req . headers [ key . toLowerCase ( ) ] } \n` ) ;
163
165
}
164
166
} ) ;
165
167
resp . end ( ) ;
@@ -177,16 +179,16 @@ var mocks = {
177
179
} ,
178
180
echoHtml : function ( req , resp , next ) {
179
181
resp . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
180
- resp . write ( " <div id='method'>" + req . method + " </div>" ) ;
181
- resp . write ( " <div id='query'>" + req . parsed . search . slice ( 1 ) + " </div>" ) ;
182
+ resp . write ( ` <div id='method'>${ req . method } </div>` ) ;
183
+ resp . write ( ` <div id='query'>${ req . parsed . search . slice ( 1 ) } </div>` ) ;
182
184
getBody ( req ) . then ( function ( body ) {
183
- resp . write ( " <div id='data'>" + body + " </div>" ) ;
185
+ resp . write ( ` <div id='data'>${ body } </div>` ) ;
184
186
resp . end ( body ) ;
185
187
} , next ) ;
186
188
} ,
187
189
etag : function ( req , resp ) {
188
- var hash = Number ( req . query . ts ) . toString ( 36 ) ;
189
- var etag = "W/\"" + hash + "\"" ;
190
+ const hash = Number ( req . query . ts ) . toString ( 36 ) ;
191
+ const etag = `W/" ${ hash } "` ;
190
192
if ( req . headers [ "if-none-match" ] === etag ) {
191
193
resp . writeHead ( 304 ) ;
192
194
resp . end ( ) ;
@@ -197,8 +199,8 @@ var mocks = {
197
199
} ) ;
198
200
resp . end ( ) ;
199
201
} ,
200
- ims : function ( req , resp , next ) {
201
- var ts = req . query . ts ;
202
+ ims : function ( req , resp ) {
203
+ const ts = req . query . ts ;
202
204
if ( req . headers [ "if-modified-since" ] === ts ) {
203
205
resp . writeHead ( 304 ) ;
204
206
resp . end ( ) ;
@@ -209,67 +211,75 @@ var mocks = {
209
211
} ) ;
210
212
resp . end ( ) ;
211
213
} ,
212
- status : function ( req , resp , next ) {
214
+ status : function ( req , resp ) {
213
215
resp . writeHead ( Number ( req . query . code ) ) ;
214
216
resp . end ( ) ;
215
217
} ,
216
218
testHTML : function ( req , resp ) {
217
219
resp . writeHead ( 200 , { "Content-Type" : "text/html" } ) ;
218
- var body = fs . readFileSync ( __dirname + "/data/test.include.html" ) . toString ( ) ;
219
- body = body . replace ( / { { baseURL} } / g, req . query . baseURL ) ;
220
+ const body = fs
221
+ . readFileSync ( `${ __dirname } /data/test.include.html` )
222
+ . toString ( )
223
+ . replace ( / { { baseURL} } / g, req . query . baseURL ) ;
220
224
resp . end ( body ) ;
221
225
} ,
222
- cspFrame : function ( req , resp ) {
226
+ cspFrame : function ( _req , resp ) {
223
227
resp . writeHead ( 200 , {
224
228
"Content-Type" : "text/html" ,
225
- "Content-Security-Policy" : "default-src 'self'; require-trusted-types-for 'script'; report-uri /base/test/data/mock.php?action=cspLog"
229
+ "Content-Security-Policy" : "default-src 'self'; require-trusted-types-for 'script'; " +
230
+ "report-uri /base/test/data/mock.php?action=cspLog"
226
231
} ) ;
227
- var body = fs . readFileSync ( __dirname + " /data/csp.include.html" ) . toString ( ) ;
232
+ const body = fs . readFileSync ( ` ${ __dirname } /data/csp.include.html` ) . toString ( ) ;
228
233
resp . end ( body ) ;
229
234
} ,
230
235
cspNonce : function ( req , resp ) {
231
- var testParam = req . query . test ? "-" + req . query . test : "" ;
236
+ const testParam = req . query . test ? `- ${ req . query . test } ` : "" ;
232
237
resp . writeHead ( 200 , {
233
238
"Content-Type" : "text/html" ,
234
- "Content-Security-Policy" : "script-src 'nonce-jquery+hardcoded+nonce'; report-uri /base/test/data/mock.php?action=cspLog"
239
+ "Content-Security-Policy" : "script-src 'nonce-jquery+hardcoded+nonce'; " +
240
+ "report-uri /base/test/data/mock.php?action=cspLog"
235
241
} ) ;
236
- var body = fs . readFileSync (
237
- __dirname + " /data/csp-nonce" + testParam + " .html" ) . toString ( ) ;
242
+ const body = fs . readFileSync (
243
+ ` ${ __dirname } /data/csp-nonce${ testParam } .html` ) . toString ( ) ;
238
244
resp . end ( body ) ;
239
245
} ,
240
- cspAjaxScript : function ( req , resp ) {
246
+ cspAjaxScript : function ( _req , resp ) {
241
247
resp . writeHead ( 200 , {
242
248
"Content-Type" : "text/html" ,
243
- "Content-Security-Policy" : "script-src 'self'; report-uri /base/test/data/mock.php?action=cspLog"
249
+ "Content-Security-Policy" : "script-src 'self'; " +
250
+ "report-uri /base/test/data/mock.php?action=cspLog"
244
251
} ) ;
245
- var body = fs . readFileSync (
246
- __dirname + " /data/csp-ajax-script.html" ) . toString ( ) ;
252
+ const body = fs . readFileSync (
253
+ ` ${ __dirname } /data/csp-ajax-script.html` ) . toString ( ) ;
247
254
resp . end ( body ) ;
248
255
} ,
249
- cspLog : function ( req , resp ) {
256
+ cspLog : function ( _req , resp ) {
250
257
cspLog = "error" ;
251
258
resp . writeHead ( 200 ) ;
252
259
resp . end ( ) ;
253
260
} ,
254
- cspClean : function ( req , resp ) {
261
+ cspClean : function ( _req , resp ) {
255
262
cspLog = "" ;
256
263
resp . writeHead ( 200 ) ;
257
264
resp . end ( ) ;
258
265
} ,
259
- trustedHtml : function ( req , resp ) {
266
+ trustedHtml : function ( _req , resp ) {
260
267
resp . writeHead ( 200 , {
261
268
"Content-Type" : "text/html" ,
262
- "Content-Security-Policy" : "require-trusted-types-for 'script'; report-uri /base/test/data/mock.php?action=cspLog"
269
+ "Content-Security-Policy" : "require-trusted-types-for 'script'; " +
270
+ "report-uri /base/test/data/mock.php?action=cspLog"
263
271
} ) ;
264
- var body = fs . readFileSync ( __dirname + " /data/trusted-html.html" ) . toString ( ) ;
272
+ const body = fs . readFileSync ( ` ${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
265
273
resp . end ( body ) ;
266
274
} ,
267
- trustedTypesAttributes : function ( req , resp ) {
275
+ trustedTypesAttributes : function ( _req , resp ) {
268
276
resp . writeHead ( 200 , {
269
277
"Content-Type" : "text/html" ,
270
- "Content-Security-Policy" : "require-trusted-types-for 'script'; report-uri /base/test/data/mock.php?action=cspLog"
278
+ "Content-Security-Policy" : "require-trusted-types-for 'script'; " +
279
+ "report-uri /base/test/data/mock.php?action=cspLog"
271
280
} ) ;
272
- var body = fs . readFileSync ( __dirname + "/data/trusted-types-attributes.html" ) . toString ( ) ;
281
+ const body = fs . readFileSync (
282
+ `${ __dirname } /data/trusted-types-attributes.html` ) . toString ( ) ;
273
283
resp . end ( body ) ;
274
284
} ,
275
285
errorWithScript : function ( req , resp ) {
@@ -279,14 +289,14 @@ var mocks = {
279
289
resp . writeHead ( 404 , { "Content-Type" : "text/html; charset=UTF-8" } ) ;
280
290
}
281
291
if ( req . query . callback ) {
282
- resp . end ( cleanCallback ( req . query . callback ) +
283
- " ( {\ "status\ ": 404, \ "msg\ ": \ "Not Found\ "} )" ) ;
292
+ resp . end ( ` ${ cleanCallback ( req . query . callback )
293
+ } ( {"status": 404, "msg": "Not Found"} )` ) ;
284
294
} else {
285
295
resp . end ( "QUnit.assert.ok( false, \"Mock return erroneously executed\" );" ) ;
286
296
}
287
297
}
288
298
} ;
289
- var handlers = {
299
+ const handlers = {
290
300
"test/data/mock.php" : function ( req , resp , next ) {
291
301
if ( ! mocks [ req . query . action ] ) {
292
302
resp . writeHead ( 400 ) ;
@@ -296,11 +306,11 @@ var handlers = {
296
306
}
297
307
mocks [ req . query . action ] ( req , resp , next ) ;
298
308
} ,
299
- "test/data/support/csp.log" : function ( req , resp ) {
309
+ "test/data/support/csp.log" : function ( _req , resp ) {
300
310
resp . writeHead ( 200 ) ;
301
311
resp . end ( cspLog ) ;
302
312
} ,
303
- "test/data/404.txt" : function ( req , resp ) {
313
+ "test/data/404.txt" : function ( _req , resp ) {
304
314
resp . writeHead ( 404 ) ;
305
315
resp . end ( "" ) ;
306
316
}
@@ -315,21 +325,23 @@ var handlers = {
315
325
* Express versions of these (e.g. no req.path, req.query, resp.set).
316
326
*/
317
327
function MockserverMiddlewareFactory ( ) {
328
+
318
329
/**
319
330
* @param {http.IncomingMessage } req
320
331
* @param {http.ServerResponse } resp
321
332
* @param {Function } next Continue request handling
322
333
*/
323
334
return function ( req , resp , next ) {
324
- var parsed = url . parse ( req . url , /* parseQuery */ true ) ,
325
- path = parsed . pathname . replace ( / ^ \/ b a s e \/ / , "" ) ,
326
- query = parsed . query ,
327
- subReq = Object . assign ( Object . create ( req ) , {
328
- query : query ,
329
- parsed : parsed
330
- } ) ;
335
+ const parsed = url . parse ( req . url , /* parseQuery */ true ) ;
336
+ let path = parsed . pathname . replace ( / ^ \/ b a s e \/ / , "" ) ;
337
+ const query = parsed . query ;
338
+ const subReq = Object . assign ( Object . create ( req ) , {
339
+ query : query ,
340
+ parsed : parsed
341
+ } ) ;
331
342
332
343
if ( / ^ t e s t \/ d a t a \/ m o c k .p h p \/ / . test ( path ) ) {
344
+
333
345
// Support REST-like Apache PathInfo
334
346
path = "test\/data\/mock.php" ;
335
347
}
0 commit comments