Skip to content

Commit 277ac0d

Browse files
committed
Node:支持 CORS 跨域;新增 /test/compare 前后对比断言接口 和 /test/ml 机器学习断言接口
1 parent 05b8e76 commit 277ac0d

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

index.html

-18
Original file line numberDiff line numberDiff line change
@@ -899,23 +899,5 @@
899899

900900
<script src="md/editormd.js"></script>
901901

902-
903-
<!-- 百度统计 <<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
904-
<script>
905-
var _hmt = _hmt || [];
906-
(function() {
907-
try {
908-
var hm = document.createElement("script");
909-
// hm.src = "https://hm.baidu.com/hm.js?b8fc421d7c0dc354c6c8fb5a2c4f7729"; //cn
910-
hm.src = "https://hm.baidu.com/hm.js?965ec968d2d53b729b90efc7ffb3ead2"; //org
911-
var s = document.getElementsByTagName("script")[0];
912-
s.parentNode.insertBefore(hm, s);
913-
} catch (e) {
914-
console.log('BaiduStatistics: catch\n' + e.message)
915-
}
916-
})();
917-
</script>
918-
<!-- 百度统计 >>>>>>>>>>>>>>>>>>>>>>>>>>>>> -->
919-
920902
</body>
921903
</html>

js/server.js

+59-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Koa = require('koa');
33
const {getRequestFromURL, App} = require('./main');
44
// const { createBundleRenderer } = require('vue-server-renderer')
55

6-
// const JSONResponse = require('../apijson/JSONResponse');
6+
const JSONResponse = require('../apijson/JSONResponse');
77
const StringUtil = require('../apijson/StringUtil');
88

99
var isCrossEnabled = true; // false;
@@ -49,8 +49,8 @@ function update() {
4949
deepProgress = App.deepDoneCount >= App.deepAllCount ? 1 : (App.deepDoneCount/App.deepAllCount).toFixed(2);
5050
randomProgress = App.randomDoneCount >= App.randomAllCount ? 1 : (App.randomDoneCount/App.randomAllCount).toFixed(2);
5151
// progress = accountProgress*testCaseProgress*deepProgress*randomProgress;
52-
progress = accountProgress >= 1 ? 1 : (accountProgress + (accountAllCount <= 0 ? 1 : 1/accountAllCount)*(testCaseProgress
53-
+ (App.allCount <= 0 ? 1 : 1/App.allCount)*(deepProgress + (App.deepAllCount <= 0 ? 1 : 1/App.deepAllCount)*randomProgress)));
52+
progress = accountProgress >= 1 ? 1 : (accountProgress + (accountAllCount <= 0 ? 1 : 1/accountAllCount*(testCaseProgress
53+
+ (App.allCount <= 0 ? 1 : 1/App.allCount*(deepProgress + (App.deepAllCount <= 0 ? 1 : 1/App.deepAllCount*randomProgress))))));
5454

5555
if (progress >= 1) {
5656
isLoading = false;
@@ -66,14 +66,29 @@ function update() {
6666
const PORT = 3002;
6767

6868
const app = new Koa();
69+
var done = false;
70+
6971
app.use(async ctx => {
70-
console.log(ctx)
72+
console.log(ctx);
73+
var origin = ctx.get('Origin') || ctx.get('origin');
74+
console.log('origin = ' + origin);
75+
ctx.set('Access-Control-Max-Age', "1000000"); // "-1");
76+
ctx.set('Access-Control-Allow-Origin', origin);
77+
ctx.set('Access-Control-Allow-Headers', "*");
78+
ctx.set('Access-Control-Allow-Credentials', 'true');
79+
ctx.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,PUT,DELETE,OPTIONS,TRACE');
80+
// ctx.set('Access-Control-Expose-Headers', "*");
81+
82+
if (ctx.method == null || ctx.method.toUpperCase() == 'OPTIONS') {
83+
ctx.status = 200;
84+
return;
85+
}
7186

7287
if (ctx.path == '/test/start' || (isLoading != true && ctx.path == '/test')) {
7388
if (isLoading && ctx.path == '/test/start') {
74-
ctx.body = 'Already started auto testing in node, please wait for minutes...';
75-
ctx.status = 200
76-
return
89+
ctx.status = 200;
90+
ctx.body = ctx.response.body = 'Already started auto testing in node, please wait for minutes...';
91+
return;
7792
}
7893

7994
App.isCrossEnabled = isCrossEnabled; // isCrossEnabled = App.isCrossEnabled;
@@ -115,7 +130,7 @@ app.use(async ctx => {
115130
isCrossEnabled = App.isCrossEnabled;
116131

117132
ctx.status = ctx.response.status = 200; // 302;
118-
ctx.body = 'Auto testing in node...';
133+
ctx.body = ctx.response.body = 'Auto testing in node...';
119134

120135
// setTimeout(function () { // 延迟无效
121136
ctx.redirect('/test/status');
@@ -129,7 +144,42 @@ app.use(async ctx => {
129144
}
130145

131146
ctx.status = ctx.response.status = 200; // progress >= 1 ? 200 : 302;
132-
ctx.body = (message || (progress < 1 || isLoading ? 'Auto testing in node...' : 'Done auto testing in node.')) + timeMsg + progressMsg;
147+
ctx.body = ctx.response.body = (message || (progress < 1 || isLoading ? 'Auto testing in node...' : 'Done auto testing in node.')) + timeMsg + progressMsg;
148+
}
149+
else if (ctx.path == '/test/compare' || ctx.path == '/test/ml') {
150+
done = false;
151+
var json = '';
152+
ctx.req.addListener('data', (data) => {
153+
json += data;
154+
})
155+
ctx.req.addListener('end', function() {
156+
console.log(json);
157+
// })
158+
159+
var body = JSON.parse(json) || ctx.body || ctx.req.body || ctx.request.body || {};
160+
console.log(body);
161+
var isML = ctx.path == '/test/ml' || body.isML;
162+
var res = body.response;
163+
var stdd = body.standard;
164+
165+
var response = typeof res != 'string' ? res : (StringUtil.isEmpty(res, true) ? null : JSON.parse(res));
166+
var standard = typeof stdd != 'string' ? stdd : (StringUtil.isEmpty(stdd, true) ? null : JSON.parse(stdd));
167+
168+
console.log('\n\nresponse = ' + JSON.stringify(response));
169+
console.log('\n\nstdd = ' + JSON.stringify(stdd));
170+
var compare = JSONResponse.compareResponse(standard, response || {}, '', isML, null, null, false) || {}
171+
console.log('\n\ncompare = ' + JSON.stringify(compare));
172+
173+
ctx.status = ctx.response.status = 200;
174+
ctx.body = ctx.response.body = compare == null ? '' : JSON.stringify(compare);
175+
done = true;
176+
})
177+
178+
while (true) {
179+
if (done) {
180+
break;
181+
}
182+
}
133183
}
134184
});
135185

0 commit comments

Comments
 (0)