testing - 使用 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
}
}
]
}
}
]
}
]
}'
有什么建议么?
解决方案
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'] = '*';
}
推荐阅读
- typescript - Typescript 中奇怪的类型推断行为
- python - 通过按钮绘制轴数据交换
- apache-superset - Apache-Superset 可变过滤器图表大小
- botframework - 有没有办法在没有 Ngrok 的情况下测试本地托管的机器人?
- java - Java - 异步等待?
- javascript - 在 type="number" 输入上滚动以更改其值:如何停止父级的滚动
- docker - docker alpine 容器启动时挂载 webdav 共享
- node.js - 即使我已将当前工作目录挂载为卷,文件内容也不会改变
- r - 在 Plotly R 中为条形图添加注释
- reactjs - 每个孩子都应该有一个道具警告