使用量角器使用 server.js 和 proxy.config.json 模拟服务 [英] Mock services using server.js and proxy.config.json using Protractor
问题描述
我正在尝试使用 server.js 和 proxy.config.json 文件模拟服务,以便我可以在量角器测试中使用该模拟服务.我要处理的方法是使用这样的测试文件:
I am trying to mock service using server.js and the proxy.config.json file so I can use that mocked service in a Protractor test. The way I'm going about it is having the test file like this:
const server = require('server');
const { get, post } = server.router;
const { json } = server.reply;
server({ port: 3000 }, [
get('/abc', ctx => {
return json({
foo: "bar"
})
})
]);
和proxy.config.json文件如下:
and the proxy.config.json file like this:
{
"/xyz": {
"target": "http://localhost:3000/abc",
"changeOrigin": true,
"secure": false,
"logLevel": "debug",
"pathRewrite": {
"^/xyz": ""
}
}
}
然后我通过调用运行我的测试
Then I run my tests by calling
ng e2e --proxy-config proxy.config.json
但是,我没有成功,因为我的应用程序在例如 http://localhost:49156 中运行我希望模拟的服务在 https://localhost:8443/xyz 中运行.如果我希望模拟和代理来自与应用程序(49156 和 HTTP)相同的端口/协议的某些内容,我可以这样做,但是,对于在端口 8443 和 https 中运行的服务,我无法这样做.有人可以帮我弄这个吗?我究竟做错了什么?感谢您抽出宝贵时间.
However, I am not being successful because my application runs, for example, in http://localhost:49156 and the service I wish to mock runs in https://localhost:8443/xyz. If I wish to mock and proxy something coming from the same port/protocol as the application (49156 and HTTP) I am able to do so, however, for the service running in port 8443 and https I am not able to. Can someone help me with this? What am I doing wrong? Thank you for your time.
推荐答案
我终于能够通过创建一个 environmentE2E.ts 文件来解决我最初的问题:
I was finally able to solve my initial issue by creating an environmentE2E.ts file as such:
export const environment = {
production: false,
baseURL: 'http://localhost:49156'
};
在 angular-cli.json 文件中添加以下内容:
adding the following in angular-cli.json file:
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts",
"e2e": "environments/environmentE2E.ts" // Added only this line, the others were already there
}
并使用以下命令调用测试:
and called the tests with the following command:
ng e2e --proxy-config proxy.config.json --environment e2e
希望它可以帮助其他人.谢谢.
Hope it helps others. Thank you.
这篇关于使用量角器使用 server.js 和 proxy.config.json 模拟服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!