Skip to content

Commit 9a76240

Browse files
committed
Make some parameters configurable over env
1 parent e609311 commit 9a76240

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

index.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,10 @@ app.use(function(req, res, next){
5555
next();
5656
}));
5757
});
58+
5859
//Handle all paths
5960
app.all('*', (req, res) => {
60-
61-
if(process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH){
62-
// Path is relative to current directory
63-
res.sendFile(process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH, { root : __dirname});
64-
return;
65-
}
61+
6662

6763
const echo = {
6864
path: req.path,
@@ -90,17 +86,17 @@ app.all('*', (req, res) => {
9086
let newHeaders = {...req.headers};
9187

9288
// req.headers is in lowercase, processed, deduplicated. req.rawHeaders is not.
93-
// Match on the preserved case of the header name, populate newHeaders with preserved case and processed value.
89+
// Match on the preserved case of the header name, populate newHeaders with preserved case and processed value.
9490
for (let i = 0; i < req.rawHeaders.length; i += 2) {
9591
let preservedHeaderName = req.rawHeaders[i];
9692
if (preservedHeaderName == preservedHeaderName.toLowerCase()) { continue; }
97-
93+
9894
newHeaders[preservedHeaderName] = req.header(preservedHeaderName);
9995
delete newHeaders[preservedHeaderName.toLowerCase()];
10096
}
10197
echo.headers = newHeaders;
10298
}
103-
99+
104100

105101
//Add client certificate details to the output, if present
106102
//This only works if `requestCert` is true when starting the server.
@@ -135,17 +131,22 @@ app.all('*', (req, res) => {
135131
}
136132

137133
//Set the status code to what the user wants
138-
const setResponseStatusCode = parseInt(req.headers["x-set-response-status-code"] || req.query["x-set-response-status-code"], 10)
134+
const setResponseStatusCode = parseInt(req.headers["x-set-response-status-code"] || req.query["x-set-response-status-code"] || process.env.RESPONSE_STATUS_CODE, 10)
139135
if (100 <= setResponseStatusCode && setResponseStatusCode < 600) {
140136
res.status(setResponseStatusCode)
141137
}
142138

143139
//Delay the response for a user defined time
144-
const sleepTime = parseInt(req.headers["x-set-response-delay-ms"] || req.query["x-set-response-delay-ms"], 0)
140+
const sleepTime = parseInt(req.headers["x-set-response-delay-ms"]
141+
|| req.query["x-set-response-delay-ms"]
142+
|| process.env.RESPONSE_DELAY_MS
143+
|| "0")
145144
sleep(sleepTime).then(() => {
146145

147146
//Set the response content type to what the user wants
148-
const setResponseContentType = req.headers["x-set-response-content-type"] || req.query["x-set-response-content-type"];
147+
const setResponseContentType = req.headers["x-set-response-content-type"]
148+
|| req.query["x-set-response-content-type"]
149+
|| process.env.RESPONSE_CONTENT_TYPE;
149150

150151
if(setResponseContentType){
151152
res.contentType(setResponseContentType);
@@ -169,8 +170,17 @@ app.all('*', (req, res) => {
169170
if (process.env.ECHO_BACK_TO_CLIENT != undefined && process.env.ECHO_BACK_TO_CLIENT == "false"){
170171
res.end();
171172
}
173+
//Ability to send an content of file as response body
174+
else if (process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH){
175+
// Path is relative to current directory
176+
res.sendFile(process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH, { root : __dirname});
177+
}
178+
//Ability to send an content of environment variable as response body
179+
else if (process.env.OVERRIDE_RESPONSE_BODY){
180+
res.send(process.env.OVERRIDE_RESPONSE_BODY);
181+
}
172182
//Ability to send just the request body in the response, nothing else
173-
else if ("response_body_only" in req.query && req.query["response_body_only"] == "true") {
183+
else if ((req.query["response_body_only"] === "true") || (process.env.RESPONSE_BODY_ONLY === "true")) {
174184
res.send(req.body);
175185
}
176186
//Normal behavior, send everything back
@@ -238,4 +248,4 @@ function shutDown(){
238248
process.exit()
239249
});
240250
});
241-
}
251+
}

0 commit comments

Comments
 (0)