首页 > 解决方案 > 使用 CORS 的 mountebank 谓词代理

问题描述

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

我想知道是否可以直接使用代理谓词来修改来自下游服务器的响应头以添加"Access-Control-Allow-Origin": "*",. 这样我只能使用一个带有proxyOnce选项的 mountebank 实例,并允许我的浏览器与这个测试替身进行交互。对于我的用例,这有助于我从 arecord 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
                                }
                            }
                        ]
                    }
                }
            ]
        }
    ]
}'

有什么建议么?

标签: testingmountebank

解决方案


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'] = '*';
}

推荐阅读