【问题标题】:Mock services using server.js and proxy.config.json using Protractor使用 server.js 和 proxy.config.json 使用 Protractor 模拟服务
【发布时间】:2019-09-25 03:59:02
【问题描述】:

我正在尝试使用 server.js 和 proxy.config.json 文件模拟服务,以便可以在量角器测试中使用该模拟服务。我的方法是拥有这样的测试文件:

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 文件如下:

{
    "/xyz": {
        "target": "http://localhost:3000/abc",
        "changeOrigin": true,
        "secure": false,
        "logLevel": "debug",
        "pathRewrite": {
            "^/xyz": ""
        }
    }
}

然后我通过调用来运行我的测试

ng e2e --proxy-config proxy.config.json

但是,我没有成功,因为我的应用程序在 http://localhost:49156 中运行,而我希望模拟的服务在 https://localhost:8443/xyz 中运行。如果我希望模拟和代理来自与应用程序相同的端口/协议(49156 和 HTTP)的东西,我可以这样做,但是,对于在端口 8443 和 https 中运行的服务,我不能这样做。有人可以帮我弄这个吗?我究竟做错了什么? 感谢您的宝贵时间。

【问题讨论】:

标签: javascript angular typescript protractor angular-cli


【解决方案1】:

我终于能够通过创建一个 environmentE2E.ts 文件来解决我最初的问题:

export const environment = {
  production: false,
  baseURL: 'http://localhost:49156'
};

在 angular-cli.json 文件中添加以下内容:

      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "e2e": "environments/environmentE2E.ts"  // Added only this line, the others were already there
      }

并使用以下命令调用测试:

ng e2e --proxy-config proxy.config.json --environment e2e

希望它可以帮助其他人。 谢谢。

【讨论】:

    猜你喜欢
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 2023-03-10
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多