I set up a mountebank predicate to proxy a downstream server. The response from the server does not have the Access-Control-Allow-Origin
set to *
. I can definitely record the responses from the downstream server and then spin up a new mountebank instance with the allowCORS
option that allows my browser to consume from this test double without CORS issue.
I was wondering if it is possible to directly use the proxy predicate to modify the response headers from the downstream servers to add "Access-Control-Allow-Origin": "*",
. This way I can only use one instance of mountebank with the proxyOnce
option and allow my browser to interact with this test double. For my use case this helps me move from a record and replay
to just use proxy
to downstream.
I tried to also stub the OPTIONS
method but it doesn't work.
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
}
}
]
}
}
]
}
]
}'
Any suggestions?
For anyone looking into this for reference, I was able to achieve this by using the
decorate
behaviors.My stub was setup as:
And the
decorate
behaviour was as simple as this: