Skip to content

Commit 4732a95

Browse files
committed
Implementing configurable CORS settings for HTTP echo server
1 parent 46601de commit 4732a95

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ docker run -d --rm -e ECHO_INCLUDE_ENV_VARS=1 --name http-echo-tests -p 8080:808
247247

248248
Then do a normal request via curl or browser, and you will see the `env` property in the response body.
249249

250+
## Setting CORS(Cross-Origin Resource Sharing) headers in the response
251+
252+
Enable CORS headers in response by setting the environment variable `CORS_ALLOW_ORIGIN` to the list of allowed origins.
253+
CORS configuration can be further fine-tuned by using the following environment variables:
254+
255+
- **`CORS_ALLOW_METHODS`**: List of Http methods allowed.
256+
- **`CORS_ALLOW_HEADERS`**: List of headers allowed.
257+
- **`CORS_ALLOW_CREDENTIALS`**: Comma-separated list of origin URLs from which the policy allows credentials to be sent.
258+
259+
None of these can be set without setting the `CORS_ALLOW_ORIGIN` variable first. By default, they will all be missing when only the `CORS_ALLOW_ORIGIN` is set and need to be explicitly set.
260+
250261

251262
## Client certificate details (mTLS) in the response
252263

‎index.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ app.all('*', (req, res) => {
114114
res.contentType(setResponseContentType);
115115
}
116116

117+
//Set the CORS policy
118+
if (process.env.CORS_ALLOW_ORIGIN){
119+
res.header('Access-Control-Allow-Origin', process.env.CORS_ALLOW_ORIGIN);
120+
if (process.env.CORS_ALLOW_METHODS) {
121+
res.header('Access-Control-Allow-Methods', process.env.CORS_ALLOW_METHODS);
122+
}
123+
if (process.env.CORS_ALLOW_HEADERS) {
124+
res.header('Access-Control-Allow-Headers', process.env.CORS_ALLOW_HEADERS);
125+
}
126+
if (process.env.CORS_ALLOW_CREDENTIALS) {
127+
res.header('Access-Control-Allow-Credentials', process.env.CORS_ALLOW_CREDENTIALS);
128+
}
129+
}
130+
117131
//Ability to send an empty response back
118132
if (process.env.ECHO_BACK_TO_CLIENT != undefined && process.env.ECHO_BACK_TO_CLIENT == "false"){
119133
res.end();

‎tests.sh‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ else
271271
exit 1
272272
fi
273273

274+
message " Stop containers "
275+
docker stop http-echo-tests
276+
sleep 5
277+
278+
message " Start container with CORS_CONFIG"
279+
docker run -d --rm \
280+
-e CORS_ALLOW_ORIGIN="http://example.com" -e CORS_ALLOW_HEADERS="x-custom-test-header" \
281+
--name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo
282+
sleep 5
283+
# Check if the expected CORS headers are present in the response
284+
if curl -s -i http://localhost:8080/ 2>&1 | grep -q -E \
285+
"Access-Control-Allow-Headers: x-custom-test-header" &&
286+
curl -s -i http://localhost:8080/ 2>&1 | grep -q -E \
287+
"Access-Control-Allow-Origin: http://example.com"; then
288+
passed "CORS_CONFIG expected CORS headers found in response"
289+
else
290+
failed "CORS_CONFIG failed."
291+
docker logs http-echo-tests
292+
exit 1
293+
fi
274294

275295
message " Stop containers "
276296
docker stop http-echo-tests

0 commit comments

Comments
 (0)