Skip to content

Commit 4de5ccf

Browse files
committed
test: gzip
1 parent 91884d5 commit 4de5ccf

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default [
1111
globals: globals.node,
1212
parserOptions: {
1313
sourceType: 'module',
14-
ecmaVersion: 2022
14+
ecmaVersion: 2024
1515
}
1616
},
1717
rules: {

lib/node-static.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ class Server {
228228
const enable = this.options.gzip;
229229
if(enable &&
230230
(typeof enable === 'boolean' ||
231-
(contentType && (enable instanceof RegExp) && enable.test(contentType)))) {
231+
(contentType && (enable instanceof RegExp) &&
232+
enable.test(contentType)))
233+
) {
232234
const acceptEncoding = req.headers['accept-encoding'];
233235
return acceptEncoding && acceptEncoding.includes('gzip');
234236
}

test/integration/binary.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,33 @@ describe('node-static (CLI)', function () {
240240
assert.equal(text, 'hello world', 'should respond with hello world');
241241
});
242242

243+
it('serving file within directory and gzip but without gzip accept request', async function () {
244+
const {response /* , stdout */} = await spawnConditional(binFile, [
245+
'-p', this.port, fixturePath, '--gzip'
246+
], timeout - 9000, {
247+
condition: /serving ".*?"/,
248+
action: (/* err, stdout */) => {
249+
return fetch(
250+
`http://localhost:${this.port}/hello.txt`, {
251+
headers: {
252+
'accept-encoding': 'nothing'
253+
}
254+
}
255+
);
256+
}
257+
});
258+
259+
const {status} = response;
260+
const contentType = response.headers.get('content-type');
261+
const contentEncoding = response.headers.get('content-encoding');
262+
const text = await response.text();
263+
264+
assert.equal(status, 200, 'should respond with 200');
265+
assert.equal(contentType, 'text/plain', 'should respond with text/plain');
266+
assert.equal(contentEncoding, null, 'should not respond with gzip encoding');
267+
assert.equal(text, 'hello world', 'should respond with hello world');
268+
});
269+
243270
it('serves custom cache', async function () {
244271
const {response: responses /* , stdout */} = await spawnConditional(binFile, [
245272
'-p', this.port, '--cache', JSON.stringify({

test/integration/node-static-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ describe('node-static', function () {
158158
assert.equal(response.headers.get('server'), 'own header', 'should contain custom server header');
159159
assert.equal(await response.text(), 'hello world', 'should respond with hello world');
160160
});
161+
162+
it('serving hello.txt with non-accepting gzip', async function (){
163+
fileServer = new statik.Server(__dirname + '/../fixtures', {
164+
gzip: /special-encoding/v
165+
});
166+
167+
const response = await fetch(this.getTestServer() + '/hello.txt', {
168+
headers: {
169+
'accept-encoding': 'gzip'
170+
}
171+
});
172+
const contentEncoding = response.headers.get('content-encoding');
173+
174+
assert.equal(response.status, 200, 'should respond with 200');
175+
assert.equal(response.headers.get('content-type'), 'text/plain', 'should respond with text/plain');
176+
assert.equal(contentEncoding, null, 'should not respond with gzip encoding');
177+
assert.equal(await response.text(), 'hello world', 'should respond with hello world');
178+
});
179+
161180
it('serving first 5 bytes of hello.txt', async function () {
162181
fileServer = new statik.Server(__dirname + '/../fixtures');
163182
const options = {

0 commit comments

Comments
 (0)