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 = "" ;
58
6- var cspLog = "" ;
79/**
810 * Keep in sync with /test/mock.php
911 */
1012function cleanCallback ( callback ) {
1113return callback . replace ( / [ ^ a - z 0 - 9 _ ] / gi, "" ) ;
1214}
1315
14- var mocks = {
16+ const mocks = {
1517contentType :function ( req , resp ) {
1618resp . writeHead ( 200 , {
1719"content-type" :req . query . contentType
1820} ) ;
1921resp . end ( req . query . response ) ;
2022} ,
2123wait :function ( req , resp ) {
22- var wait = Number ( req . query . wait ) * 1000 ;
24+ const wait = Number ( req . query . wait ) * 1000 ;
2325setTimeout ( function ( ) {
2426if ( req . query . script ) {
2527resp . writeHead ( 200 , { "content-type" :"text/javascript" } ) ;
@@ -44,7 +46,7 @@ var mocks = {
4446} , next ) ;
4547} ,
4648xml :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>" ;
4850resp . writeHead ( 200 , { "content-type" :"text/xml" } ) ;
4951
5052if ( req . query . cal === "5-2" ) {
@@ -59,7 +61,7 @@ var mocks = {
5961}
6062} , next ) ;
6163} ,
62- atom :function ( req , resp , next ) {
64+ atom :function ( _req , resp ) {
6365resp . writeHead ( 200 , { "content-type" :"atom+xml" } ) ;
6466resp . end ( "<root><element /></root>" ) ;
6567} ,
@@ -77,14 +79,14 @@ var mocks = {
7779}
7880
7981if ( req . query . callback ) {
80- resp . end ( cleanCallback ( req . query . callback ) + "(" + JSON . stringify ( {
82+ resp . end ( ` ${ cleanCallback ( req . query . callback ) } ( ${ JSON . stringify ( {
8183headers :req . headers
82- } ) + ")" ) ;
84+ } ) } )` ) ;
8385} else {
8486resp . end ( "QUnit.assert.ok( true, \"mock executed\" );" ) ;
8587}
8688} ,
87- testbar :function ( req , resp ) {
89+ testbar :function ( _req , resp ) {
8890resp . writeHead ( 200 ) ;
8991resp . end (
9092"this.testBar = 'bar'; " +
@@ -110,7 +112,7 @@ var mocks = {
110112}
111113} ,
112114jsonp :function ( req , resp , next ) {
113- var callback ;
115+ let callback ;
114116if ( Array . isArray ( req . query . callback ) ) {
115117callback = Promise . resolve ( req . query . callback [ req . query . callback . length - 1 ] ) ;
116118} else if ( req . query . callback ) {
@@ -122,22 +124,22 @@ var mocks = {
122124return body . trim ( ) . replace ( "callback=" , "" ) ;
123125} ) ;
124126}
125- var json = req . query . array ?
127+ const json = req . query . array ?
126128JSON . stringify (
127129[ { name :"John" , age :21 } , { name :"Peter" , age :25 } ]
128130) :
129131JSON . stringify (
130132{ data :{ lang :"en" , length :25 } }
131133) ;
132134callback . then ( function ( cb ) {
133- resp . end ( cleanCallback ( cb ) + "(" + json + ")" ) ;
135+ resp . end ( ` ${ cleanCallback ( cb ) } ( ${ json } )` ) ;
134136} , next ) ;
135137} ,
136138xmlOverJsonp :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 ( ) ;
139141resp . writeHead ( 200 ) ;
140- resp . end ( cleanCallback ( callback ) + "(" + JSON . stringify ( body ) + " )\n" ) ;
142+ resp . end ( ` ${ cleanCallback ( callback ) } ( ${ JSON . stringify ( body ) } )\n` ) ;
141143} ,
142144error :function ( req , resp ) {
143145if ( req . query . json ) {
@@ -159,7 +161,7 @@ var mocks = {
159161} ) ;
160162req . query . keys . split ( "|" ) . forEach ( function ( key ) {
161163if ( key . toLowerCase ( ) in req . headers ) {
162- resp . write ( key + ": " + req . headers [ key . toLowerCase ( ) ] + "\n" ) ;
164+ resp . write ( ` ${ key } : ${ req . headers [ key . toLowerCase ( ) ] } \n` ) ;
163165}
164166} ) ;
165167resp . end ( ) ;
@@ -177,16 +179,16 @@ var mocks = {
177179} ,
178180echoHtml :function ( req , resp , next ) {
179181resp . 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>` ) ;
182184getBody ( req ) . then ( function ( body ) {
183- resp . write ( " <div id='data'>" + body + " </div>" ) ;
185+ resp . write ( ` <div id='data'>${ body } </div>` ) ;
184186resp . end ( body ) ;
185187} , next ) ;
186188} ,
187189etag :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 } "` ;
190192if ( req . headers [ "if-none-match" ] === etag ) {
191193resp . writeHead ( 304 ) ;
192194resp . end ( ) ;
@@ -197,8 +199,8 @@ var mocks = {
197199} ) ;
198200resp . end ( ) ;
199201} ,
200- ims :function ( req , resp , next ) {
201- var ts = req . query . ts ;
202+ ims :function ( req , resp ) {
203+ const ts = req . query . ts ;
202204if ( req . headers [ "if-modified-since" ] === ts ) {
203205resp . writeHead ( 304 ) ;
204206resp . end ( ) ;
@@ -209,67 +211,75 @@ var mocks = {
209211} ) ;
210212resp . end ( ) ;
211213} ,
212- status :function ( req , resp , next ) {
214+ status :function ( req , resp ) {
213215resp . writeHead ( Number ( req . query . code ) ) ;
214216resp . end ( ) ;
215217} ,
216218testHTML :function ( req , resp ) {
217219resp . 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 ) ;
220224resp . end ( body ) ;
221225} ,
222- cspFrame :function ( req , resp ) {
226+ cspFrame :function ( _req , resp ) {
223227resp . writeHead ( 200 , {
224228"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"
226231} ) ;
227- var body = fs . readFileSync ( __dirname + " /data/csp.include.html" ) . toString ( ) ;
232+ const body = fs . readFileSync ( ` ${ __dirname } /data/csp.include.html` ) . toString ( ) ;
228233resp . end ( body ) ;
229234} ,
230235cspNonce :function ( req , resp ) {
231- var testParam = req . query . test ?"-" + req . query . test :"" ;
236+ const testParam = req . query . test ?`- ${ req . query . test } ` :"" ;
232237resp . writeHead ( 200 , {
233238"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"
235241} ) ;
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 ( ) ;
238244resp . end ( body ) ;
239245} ,
240- cspAjaxScript :function ( req , resp ) {
246+ cspAjaxScript :function ( _req , resp ) {
241247resp . writeHead ( 200 , {
242248"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"
244251} ) ;
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 ( ) ;
247254resp . end ( body ) ;
248255} ,
249- cspLog :function ( req , resp ) {
256+ cspLog :function ( _req , resp ) {
250257cspLog = "error" ;
251258resp . writeHead ( 200 ) ;
252259resp . end ( ) ;
253260} ,
254- cspClean :function ( req , resp ) {
261+ cspClean :function ( _req , resp ) {
255262cspLog = "" ;
256263resp . writeHead ( 200 ) ;
257264resp . end ( ) ;
258265} ,
259- trustedHtml :function ( req , resp ) {
266+ trustedHtml :function ( _req , resp ) {
260267resp . writeHead ( 200 , {
261268"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"
263271} ) ;
264- var body = fs . readFileSync ( __dirname + " /data/trusted-html.html" ) . toString ( ) ;
272+ const body = fs . readFileSync ( ` ${ __dirname } /data/trusted-html.html` ) . toString ( ) ;
265273resp . end ( body ) ;
266274} ,
267- trustedTypesAttributes :function ( req , resp ) {
275+ trustedTypesAttributes :function ( _req , resp ) {
268276resp . writeHead ( 200 , {
269277"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"
271280} ) ;
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 ( ) ;
273283resp . end ( body ) ;
274284} ,
275285errorWithScript :function ( req , resp ) {
@@ -279,14 +289,14 @@ var mocks = {
279289resp . writeHead ( 404 , { "Content-Type" :"text/html; charset=UTF-8" } ) ;
280290}
281291if ( 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"} )` ) ;
284294} else {
285295resp . end ( "QUnit.assert.ok( false, \"Mock return erroneously executed\" );" ) ;
286296}
287297}
288298} ;
289- var handlers = {
299+ const handlers = {
290300"test/data/mock.php" :function ( req , resp , next ) {
291301if ( ! mocks [ req . query . action ] ) {
292302resp . writeHead ( 400 ) ;
@@ -296,11 +306,11 @@ var handlers = {
296306}
297307mocks [ req . query . action ] ( req , resp , next ) ;
298308} ,
299- "test/data/support/csp.log" :function ( req , resp ) {
309+ "test/data/support/csp.log" :function ( _req , resp ) {
300310resp . writeHead ( 200 ) ;
301311resp . end ( cspLog ) ;
302312} ,
303- "test/data/404.txt" :function ( req , resp ) {
313+ "test/data/404.txt" :function ( _req , resp ) {
304314resp . writeHead ( 404 ) ;
305315resp . end ( "" ) ;
306316}
@@ -315,21 +325,23 @@ var handlers = {
315325 * Express versions of these (e.g. no req.path, req.query, resp.set).
316326 */
317327function MockserverMiddlewareFactory ( ) {
328+
318329/**
319330 *@param {http.IncomingMessage } req
320331 *@param {http.ServerResponse } resp
321332 *@param {Function } next Continue request handling
322333 */
323334return 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+ } ) ;
331342
332343if ( / ^ t e s t \/ d a t a \/ m o c k .p h p \/ / . test ( path ) ) {
344+
333345// Support REST-like Apache PathInfo
334346path = "test\/data\/mock.php" ;
335347}