我试图调用同一个 ejs 文件中的另一个函数。
async function (request, state, logger) {
var fs = require('fs');
var path = require('path');
var util = require('util');
var response;
var RspFilename = "my-success.xml";
var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);
response = fs.readFileSync((responsepath), 'utf8');
await this.sleep(1000);
return {
headers: {
"HTTP-Response-Code": 200,
"HTTP-Response-Code-Text": "OK",
"Content-Type": "text/xml"
},
"statusCode": "200",
body: response
};
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
我正在尝试这个mountebank。调用端点时,我得到以下错误
{
"errors": [
{
"code": "invalid injection",
"message": "invalid response injection",
"source": "(async function (request, state, logger) {\n var fs = require('fs');\n var path = require('path');\n var util = require('util');\n var response;\n var RspFilename = \"my-success.xml\";\n var responsepath = path.join('ConfigFiles', 'Responses', 'ABS', RspFilename);\n response = fs.readFileSync((responsepath), 'utf8');\n\n logger.info(\"---------Response Sent Successfully--------\" + RspFilename + response);\n await this.sleep(1000);\n return {\n headers: {\n \"HTTP-Response-Code\": 200,\n \"HTTP-Response-Code-Text\": \"OK\",\n \"Content-Type\": \"text/xml\"\n },\n \"statusCode\": \"200\",\n body: response\n };\n}\n\n\nfunction sleep(ms) {\n return new Promise((resolve) => {\n setTimeout(resolve, ms);\n });\n})(scope, injectState, logger, deferred.resolve, imposterState);",
"data": "Unexpected token function",
"name": "Error",
"stack": "Error\n at Object.from (C:\\Repo\\mountebank\\src\\util\\inherit.js:15:17)\n at createError (C:\\Repo\\mountebank\\src\\util\\errors.js:10:26)\n at Object.InjectionError (C:\\Repo\\mountebank\\src\\util\\errors.js:25:16)\n at inject (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:43:44)\n at processResponse (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:243:20)\n at Object.resolve (C:\\Repo\\mountebank\\src\\models\\responseResolver.js:277:16)\n at Object.resolve (C:\\Repo\\mountebank\\src\\models\\stubRepository.js:136:25)\n at F.respond (C:\\Repo\\mountebank\\src\\models\\http\\baseHttpServer.js:94:34)\n at C:\\Repo\\mountebank\\src\\models\\abstractServer.js:105:39\n at _fulfilled (C:\\Repo\\mountebank\\node_modules\\q\\q.js:854:54)"
}
]
}
实际上我的要求是为响应增加一些延迟。
顺便说一句,我尝试了 Mountebank wait,但看不到任何延迟。