javascript - UI5 自定义中间件在通过 Karma 测试套件访问时通常无法解析 JSON 主体
问题描述
好的,这是我一个多星期以来一直在努力解决的一个非常具体的问题。我正在开发一个 SAP UI5 应用程序,其中 UI 测试是通过 Opa5/QUnit 完成的。直接通过 npm 为应用程序提供服务确实没有问题,但是,使用 Karma(针对无头方法)出现了两个问题,这似乎是由使用的自定义中间件引起的:
res.status()
/res.header()
不工作(只有本机节点方法,如res.setHeader()
)- 使用正文解析器(无论是否
express.json()
已弃用bodyparser.json()
),解析器中间件似乎一直在工作,直到浏览器请求在 20 或 40 秒后失败(Chrome 仅显示“停滞”时间栏)。这种情况经常发生,但并非总是如此。
虽然第一个问题有一个解决方法(但仍然 - 知道为什么这只发生在 Karma 上会很有趣)我找不到失败请求的解决方案。我尝试更改 Karma 使用的浏览器,从 HTML 更改为脚本模式,包括几个插件,还通过 Wireshark 分析数据包,因为浏览器显示正常执行和 Karma 执行之间根本没有区别。
通过 Wireshark,我发现 Karma 浏览器在请求完成后会继续关闭 Websocket,而普通浏览器则不会(即使应用程序是通过 Karma 提供服务的)。此外,在极少数情况下,工作 POST JSON 请求,内容长度或处理时间似乎没有影响。
业力.conf.js:
module.exports = function(config) {
"use strict";
config.set({
frameworks: ['ui5'],
reporters: ["progress"],
browsers: ["Chrome_without_security"],
ui5: {
mode: "html",
testpage: "webapp/test/integration/opaTests.qunit.html",
configPath: "ui5-testing.yaml",
},
customLaunchers: {
Chrome_without_security: {
base: 'Chrome',
flags: ['--disable-web-security', '--no-sandbox']
}
},
singleRun: true,
browserNoActivityTimeout: 400000,
//logLevel: config.LOG_DEBUG,
});
};
ui5-testing.yaml:
specVersion: '2.1'
metadata:
name: grunt-build
type: application
framework:
name: SAPUI5
version: "1.84.0"
libraries:
- name: sap.m
- name: sap.ui.core
- name: sap.ui.layout
- name: sap.ui.support
development: true
- name: sap.ui.table
- name: sap.ui.unified
#- name: sap.ui.model
- name: sap.ushell
development: true
- name: themelib_sap_fiori_3
optional: true
- name: themelib_sap_belize
optional: true
#- name: themelib_sap_bluecrystal
# optional: true
- name: sap.f
- name: sap.tnt
resources:
configuration:
paths:
webapp: /webapp
server:
customMiddleware:
- name: proxy
beforeMiddleware: serveResources
configuration:
testing: true
---
specVersion: '2.1'
kind: extension
type: server-middleware
metadata:
name: proxy
middleware:
path: lib/middleware/proxy.js
代理.js:
const express = require('express')
module.exports = function ({
resources,
middlewareUtil,
options
}) {
require('dotenv').config();
const axios = require('axios')
var admin = require('firebase-admin');
const app = express();
...
app.use(express.json());
app.use((req, res, next) => { // Most POST Requests with application/json header do not enter this!
...
}
return app;
};
请求方法(示例):
upsert: function (aElements, iTimeout) {
let that = this;
return new Promise((resolve, reject) => {
let sBody = JSON.stringify(aElements);
let xhr = new XMLHttpRequest();
xhr.open('POST', UPSERT_URL, true);
xhr.onload = function (oResponse) {
that.proceedResponse(oResponse, this)
.then(() => resolve())
.catch(iStatus => reject(iStatus));
};
xhr.onerror = function (oError) {
reject(oError);
};
xhr.ontimeout = function (oError) {
console.error(`The request for ${UPSERT_URL} timed out.`);
reject(oError);
};
xhr.timeout = iTimeout;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(sBody);
});
},
正常通话:ui5 serve -p 8080 -o /test/integration/opaTests.qunit.html --config ui5-testing.yaml
因果报应:karma start
也许有人可以在这里帮助我,非常感谢!
解决方案
推荐阅读
- php - 向 PayPal 发送一个值并返回
- python - 来自不同类的 Tkinter 配置
- python - 如何在 Python Selenium 中将变量从一个函数调用到另一个函数
- database - MongoDB 查询优化 ($lookup)
- performance - 如何在 Octave 中对方法进行基准测试?
- ios - 我无法在后台收到来自 Firebase Messaging 的通知
- r - 使用 pivot_longer 将长格式数据集转换为长格式数据集
- memory-management - SwiftUI - 内存管理
- c# - 在端口 443 上使用 ALPN 将 M2MQTT 客户端库连接到 AWS IoT 时出现问题
- mongodb - mongodb查询中键的通配符