【问题标题】:mountebank predicate proxy with CORS使用 CORS 的 mountebank 谓词代理
【发布时间】:2020-12-02 18:58:17
【问题描述】:

我设置了一个 mountebank 谓词来代理下游服务器。来自服务器的响应没有将Access-Control-Allow-Origin 设置为*。我绝对可以记录来自下游服务器的响应,然后使用 allowCORS 选项启动一个新的 mountebank 实例,该选项允许我的浏览器从这个测试替身中消费而不会出现 CORS 问题。

我想知道是否可以直接使用代理谓词来修改来自下游服务器的响应头以添加"Access-Control-Allow-Origin": "*",。这样,我只能使用带有 proxyOnce 选项的 mountebank 实例,并允许我的浏览器与这个测试替身进行交互。对于我的用例,这有助于我从 record and replay 转移到仅使用 proxy 到下游。

我也尝试存根 OPTIONS 方法,但它不起作用。

curl --location --request POST 'http://localhost:2525/imposters' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "port": 4500,
    "protocol": "http",
    "name": "Proxy Request",  
    "allowCORS": true,
    "stubs": [
        {
            "predicates": [
                {
                    "equals": {
                        "method": "OPTIONS"
                    }
                }
            ],
            "responses": [
                {
                    "is": {
                        "headers": {
                            "Access-Control-Allow-Origin": "*",
                            "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE",
                            "Access-Control-Allow-Headers": "${ALLOW-HEADERS}"
                        }
                    },
                    "_behaviors": {
                        "copy": [
                            {
                                "from": {
                                    "headers": "Access-Control-Request-Headers"
                                },
                                "into": "${ALLOW-HEADERS}",
                                "using": {
                                    "method": "regex",
                                    "selector": ".+"
                                }
                            }
                        ]
                    }
                }
            ]
        },        
        {
            "responses": [
                {
                    "proxy": {
                        "to": "https://downstream.api.com",
                        "mode": "proxyOnce",
                        "predicateGenerators": [
                            {
                                "matches": {
                                    "method": true,
                                    "path": true,
                                    "query": true
                                }
                            }
                        ]
                    }
                }
            ]
        }
    ]
}'

有什么建议吗?

【问题讨论】:

  • 作为后续,我什至将此添加到responses,但它也不起作用"addDecorateBehavior": "config => { config.response.headers = {...config.response.headers, 'Access-Control-Allow-Origin': '*'} }",。不确定我是否正在尝试一些不可能的事情。

标签: testing mountebank


【解决方案1】:

对于任何研究此作为参考的人,我可以通过使用 decorate 行为来实现这一点。

我的存根设置为:

{
  "responses": [
    {
      "proxy": {
        "to": "https://downstream.api.com",
        "mode": "proxyOnce",
        "predicateGenerators": [
          {
            "matches": {
              "method": true,
              "path": true,
              "query": true
            }
          }
        ]
      },
      "behaviors": [
        {
          "decorate": "<%- stringify('../templates/add_access_control_allow_origin_header.ejs') %>"
        }
      ]
    }
  ]
}

decorate 的行为就这么简单:

config => {
  config.response.headers['Access-Control-Allow-Origin'] = '*';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多