Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce90a48

Browse files
authored
Build: Migrate middleware-mockserver to modern JS
The `test/middleware-mockserver.js` file used to have the same ESLintsettings applied as other test files that are directly run in testedbrowsers. Now it shares settings of other Node.js files.The file is now also written using modern JS, leveraging ES2018.Closesgh-5196
1 parentc66d470 commitce90a48

File tree

2 files changed

+80
-60
lines changed

2 files changed

+80
-60
lines changed

‎test/.eslintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
},
5959

6060
"overrides": [
61+
{
62+
"files": [
63+
"middleware-mockserver.js"
64+
],
65+
66+
"extends":"../.eslintrc-node.json"
67+
},
68+
6169
{
6270
"files": [
6371
"data/core/jquery-iterability-transpiled-es6.js",

‎test/middleware-mockserver.js

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
/* eslint-env node */
2-
varurl=require("url");
3-
varfs=require("fs");
4-
vargetRawBody=require("raw-body");
1+
"use strict";
2+
3+
consturl=require("url");
4+
constfs=require("fs");
5+
constgetRawBody=require("raw-body");
6+
7+
letcspLog="";
58

6-
varcspLog="";
79
/**
810
* Keep in sync with /test/mock.php
911
*/
1012
functioncleanCallback(callback){
1113
returncallback.replace(/[^a-z0-9_]/gi,"");
1214
}
1315

14-
varmocks={
16+
constmocks={
1517
contentType:function(req,resp){
1618
resp.writeHead(200,{
1719
"content-type":req.query.contentType
1820
});
1921
resp.end(req.query.response);
2022
},
2123
wait:function(req,resp){
22-
varwait=Number(req.query.wait)*1000;
24+
constwait=Number(req.query.wait)*1000;
2325
setTimeout(function(){
2426
if(req.query.script){
2527
resp.writeHead(200,{"content-type":"text/javascript"});
@@ -44,7 +46,7 @@ var mocks = {
4446
},next);
4547
},
4648
xml:function(req,resp,next){
47-
varcontent="<math><calculation>5-2</calculation><result>3</result></math>";
49+
constcontent="<math><calculation>5-2</calculation><result>3</result></math>";
4850
resp.writeHead(200,{"content-type":"text/xml"});
4951

5052
if(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){
6365
resp.writeHead(200,{"content-type":"atom+xml"});
6466
resp.end("<root><element /></root>");
6567
},
@@ -77,14 +79,14 @@ var mocks = {
7779
}
7880

7981
if(req.query.callback){
80-
resp.end(cleanCallback(req.query.callback)+"("+JSON.stringify({
82+
resp.end(`${cleanCallback(req.query.callback)}(${JSON.stringify({
8183
headers:req.headers
82-
})+")");
84+
})})`);
8385
}else{
8486
resp.end("QUnit.assert.ok( true, \"mock executed\" );");
8587
}
8688
},
87-
testbar:function(req,resp){
89+
testbar:function(_req,resp){
8890
resp.writeHead(200);
8991
resp.end(
9092
"this.testBar = 'bar'; "+
@@ -110,7 +112,7 @@ var mocks = {
110112
}
111113
},
112114
jsonp:function(req,resp,next){
113-
varcallback;
115+
letcallback;
114116
if(Array.isArray(req.query.callback)){
115117
callback=Promise.resolve(req.query.callback[req.query.callback.length-1]);
116118
}elseif(req.query.callback){
@@ -122,22 +124,22 @@ var mocks = {
122124
returnbody.trim().replace("callback=","");
123125
});
124126
}
125-
varjson=req.query.array ?
127+
constjson=req.query.array ?
126128
JSON.stringify(
127129
[{name:"John",age:21},{name:"Peter",age:25}]
128130
) :
129131
JSON.stringify(
130132
{data:{lang:"en",length:25}}
131133
);
132134
callback.then(function(cb){
133-
resp.end(cleanCallback(cb)+"("+json+")");
135+
resp.end(`${cleanCallback(cb)}(${json})`);
134136
},next);
135137
},
136138
xmlOverJsonp:function(req,resp){
137-
varcallback=req.query.callback;
138-
varbody=fs.readFileSync(__dirname+"/data/with_fries.xml").toString();
139+
constcallback=req.query.callback;
140+
constbody=fs.readFileSync(`${__dirname}/data/with_fries.xml`).toString();
139141
resp.writeHead(200);
140-
resp.end(cleanCallback(callback)+"("+JSON.stringify(body)+")\n");
142+
resp.end(`${cleanCallback(callback)}(${JSON.stringify(body)})\n`);
141143
},
142144
error:function(req,resp){
143145
if(req.query.json){
@@ -159,7 +161,7 @@ var mocks = {
159161
});
160162
req.query.keys.split("|").forEach(function(key){
161163
if(key.toLowerCase()inreq.headers){
162-
resp.write(key+": "+req.headers[key.toLowerCase()]+"\n");
164+
resp.write(`${key}:${req.headers[key.toLowerCase()]}\n`);
163165
}
164166
});
165167
resp.end();
@@ -177,16 +179,16 @@ var mocks = {
177179
},
178180
echoHtml:function(req,resp,next){
179181
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>`);
182184
getBody(req).then(function(body){
183-
resp.write("<div id='data'>"+body+"</div>");
185+
resp.write(`<div id='data'>${body}</div>`);
184186
resp.end(body);
185187
},next);
186188
},
187189
etag:function(req,resp){
188-
varhash=Number(req.query.ts).toString(36);
189-
varetag="W/\""+hash+"\"";
190+
consthash=Number(req.query.ts).toString(36);
191+
constetag=`W/"${hash}"`;
190192
if(req.headers["if-none-match"]===etag){
191193
resp.writeHead(304);
192194
resp.end();
@@ -197,8 +199,8 @@ var mocks = {
197199
});
198200
resp.end();
199201
},
200-
ims:function(req,resp,next){
201-
varts=req.query.ts;
202+
ims:function(req,resp){
203+
constts=req.query.ts;
202204
if(req.headers["if-modified-since"]===ts){
203205
resp.writeHead(304);
204206
resp.end();
@@ -209,67 +211,75 @@ var mocks = {
209211
});
210212
resp.end();
211213
},
212-
status:function(req,resp,next){
214+
status:function(req,resp){
213215
resp.writeHead(Number(req.query.code));
214216
resp.end();
215217
},
216218
testHTML:function(req,resp){
217219
resp.writeHead(200,{"Content-Type":"text/html"});
218-
varbody=fs.readFileSync(__dirname+"/data/test.include.html").toString();
219-
body=body.replace(/{{baseURL}}/g,req.query.baseURL);
220+
constbody=fs
221+
.readFileSync(`${__dirname}/data/test.include.html`)
222+
.toString()
223+
.replace(/{{baseURL}}/g,req.query.baseURL);
220224
resp.end(body);
221225
},
222-
cspFrame:function(req,resp){
226+
cspFrame:function(_req,resp){
223227
resp.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-
varbody=fs.readFileSync(__dirname+"/data/csp.include.html").toString();
232+
constbody=fs.readFileSync(`${__dirname}/data/csp.include.html`).toString();
228233
resp.end(body);
229234
},
230235
cspNonce:function(req,resp){
231-
vartestParam=req.query.test ?"-"+req.query.test :"";
236+
consttestParam=req.query.test ?`-${req.query.test}` :"";
232237
resp.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-
varbody=fs.readFileSync(
237-
__dirname+"/data/csp-nonce"+testParam+".html").toString();
242+
constbody=fs.readFileSync(
243+
`${__dirname}/data/csp-nonce${testParam}.html`).toString();
238244
resp.end(body);
239245
},
240-
cspAjaxScript:function(req,resp){
246+
cspAjaxScript:function(_req,resp){
241247
resp.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-
varbody=fs.readFileSync(
246-
__dirname+"/data/csp-ajax-script.html").toString();
252+
constbody=fs.readFileSync(
253+
`${__dirname}/data/csp-ajax-script.html`).toString();
247254
resp.end(body);
248255
},
249-
cspLog:function(req,resp){
256+
cspLog:function(_req,resp){
250257
cspLog="error";
251258
resp.writeHead(200);
252259
resp.end();
253260
},
254-
cspClean:function(req,resp){
261+
cspClean:function(_req,resp){
255262
cspLog="";
256263
resp.writeHead(200);
257264
resp.end();
258265
},
259-
trustedHtml:function(req,resp){
266+
trustedHtml:function(_req,resp){
260267
resp.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-
varbody=fs.readFileSync(__dirname+"/data/trusted-html.html").toString();
272+
constbody=fs.readFileSync(`${__dirname}/data/trusted-html.html`).toString();
265273
resp.end(body);
266274
},
267-
trustedTypesAttributes:function(req,resp){
275+
trustedTypesAttributes:function(_req,resp){
268276
resp.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-
varbody=fs.readFileSync(__dirname+"/data/trusted-types-attributes.html").toString();
281+
constbody=fs.readFileSync(
282+
`${__dirname}/data/trusted-types-attributes.html`).toString();
273283
resp.end(body);
274284
},
275285
errorWithScript:function(req,resp){
@@ -279,14 +289,14 @@ var mocks = {
279289
resp.writeHead(404,{"Content-Type":"text/html; charset=UTF-8"});
280290
}
281291
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"} )`);
284294
}else{
285295
resp.end("QUnit.assert.ok( false, \"Mock return erroneously executed\" );");
286296
}
287297
}
288298
};
289-
varhandlers={
299+
consthandlers={
290300
"test/data/mock.php":function(req,resp,next){
291301
if(!mocks[req.query.action]){
292302
resp.writeHead(400);
@@ -296,11 +306,11 @@ var handlers = {
296306
}
297307
mocks[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){
300310
resp.writeHead(200);
301311
resp.end(cspLog);
302312
},
303-
"test/data/404.txt":function(req,resp){
313+
"test/data/404.txt":function(_req,resp){
304314
resp.writeHead(404);
305315
resp.end("");
306316
}
@@ -315,21 +325,23 @@ var handlers = {
315325
* Express versions of these (e.g. no req.path, req.query, resp.set).
316326
*/
317327
functionMockserverMiddlewareFactory(){
328+
318329
/**
319330
*@param {http.IncomingMessage} req
320331
*@param {http.ServerResponse} resp
321332
*@param {Function} next Continue request handling
322333
*/
323334
returnfunction(req,resp,next){
324-
varparsed=url.parse(req.url,/* parseQuery */true),
325-
path=parsed.pathname.replace(/^\/base\//,""),
326-
query=parsed.query,
327-
subReq=Object.assign(Object.create(req),{
328-
query:query,
329-
parsed:parsed
330-
});
335+
constparsed=url.parse(req.url,/* parseQuery */true);
336+
letpath=parsed.pathname.replace(/^\/base\//,"");
337+
constquery=parsed.query;
338+
constsubReq=Object.assign(Object.create(req),{
339+
query:query,
340+
parsed:parsed
341+
});
331342

332343
if(/^test\/data\/mock.php\//.test(path)){
344+
333345
// Support REST-like Apache PathInfo
334346
path="test\/data\/mock.php";
335347
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp