Skip to content

Commit c4520e1

Browse files
committed
basic handling of gzip content-encoding on requests as per #31 - new test added to tests.sh
1 parent 4388bb8 commit c4520e1

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const express = require('express')
77
const concat = require('concat-stream');
88
const { promisify } = require('util');
99
const promBundle = require("express-prom-bundle");
10+
const zlib = require("zlib");
1011

1112
const {
1213
PROMETHEUS_ENABLED = false,
@@ -40,11 +41,16 @@ if(process.env.DISABLE_REQUEST_LOGS !== 'true'){
4041

4142
app.use(function(req, res, next){
4243
req.pipe(concat(function(data){
43-
req.body = data.toString('utf8');
44+
45+
if (req.get("Content-Encoding") === "gzip") {
46+
req.body = zlib.gunzipSync(data).toString('utf8');
47+
}
48+
else {
49+
req.body = data.toString('utf8');
50+
}
4451
next();
4552
}));
4653
});
47-
4854
//Handle all paths
4955
app.all('*', (req, res) => {
5056

tests.sh

+12
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ else
155155
exit 1
156156
fi
157157

158+
159+
message " Make JSON request with gzip Content-Encoding, and test that json is in the output. "
160+
REQUEST=$(echo -n '{"a":"b"}' | gzip | curl -s -X POST -H "Content-Encoding: gzip" -H "Content-Type: application/json" --data-binary @- http://localhost:8080/)
161+
if [ $(echo $REQUEST | jq -r '.json.a') == 'b' ]
162+
then
163+
passed "JSON test passed."
164+
else
165+
failed "JSON test failed."
166+
echo $REQUEST | jq
167+
exit 1
168+
fi
169+
158170
REQUEST=$(curl -s -X POST -H "Content-Type: application/json" -d 'not-json' http://localhost:8080)
159171
if [ $(echo $REQUEST | jq -r '.json') == 'null' ]; then
160172
passed "JSON with Invalid Body test passed."

0 commit comments

Comments
 (0)