1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
'use strict';
require('should');
require('should-http');
const assert = require('assert');
const zlib = require('zlib');
let http = require('http');
const getPort = require('get-port');
const express = require('../support/express');
const request = require('../support/client');
if (process.env.HTTP2_TEST) {
http = require('http2');
}
const app = express();
const subject = 'some long long long long string';
let base = 'http://localhost';
let server;
before(function listen(done) {
server = http.createServer(app);
getPort().then((port) => {
server = server.listen(port, function listening() {
base += `:${server.address().port}`;
done();
});
});
});
app.get('/binary', (request_, res) => {
zlib.deflate(subject, (error, buf) => {
res.set('Content-Encoding', 'gzip');
res.send(buf);
});
});
app.get('/corrupt', (request_, res) => {
res.set('Content-Encoding', 'gzip');
res.send('blah');
});
app.get('/nocontent', (request_, res, next) => {
res.statusCode = 204;
res.set('Content-Type', 'text/plain');
res.set('Content-Encoding', 'gzip');
res.send('');
});
app.get('/', (request_, res, next) => {
zlib.deflate(subject, (error, buf) => {
res.set('Content-Type', 'text/plain');
res.set('Content-Encoding', 'gzip');
res.send(buf);
});
});
app.get('/junk', (request_, res) => {
zlib.deflate(subject, (error, buf) => {
res.set('Content-Type', 'text/plain');
res.set('Content-Encoding', 'gzip');
res.write(buf);
res.end(' 0 junk');
});
});
app.get('/chopped', (request_, res) => {
zlib.deflate(`${subject}123456`, (error, buf) => {
res.set('Content-Type', 'text/plain');
res.set('Content-Encoding', 'gzip');
res.send(buf.slice(0, -1));
});
});
describe('zlib', () => {
it('should deflate the content', (done) => {
request.get(base).end((error, res) => {
res.should.have.status(200);
res.text.should.equal(subject);
res.headers['content-length'].should.be.below(subject.length);
done();
});
});
it('should protect from zip bombs', (done) => {
request
.get(base)
.buffer(true)
.maxResponseSize(1)
.end((error, res) => {
try {
assert.equal('Maximum response size reached', error && error.message);
done();
} catch (err) {
done(err);
}
});
});
it('should ignore trailing junk', (done) => {
request.get(`${base}/junk`).end((error, res) => {
res.should.have.status(200);
res.text.should.equal(subject);
done();
});
});
it('should ignore missing data', (done) => {
request.get(`${base}/chopped`).end((error, res) => {
assert.equal(undefined, error);
res.should.have.status(200);
res.text.should.startWith(subject);
done();
});
});
it('should handle corrupted responses', (done) => {
request.get(`${base}/corrupt`).end((error, res) => {
assert(error, 'missing error');
assert(!res, 'response should not be defined');
done();
});
});
it('should handle no content with gzip header', (done) => {
request.get(`${base}/nocontent`).end((error, res) => {
assert.ifError(error);
assert(res);
res.should.have.status(204);
res.text.should.equal('');
res.headers.should.not.have.property('content-length');
done();
});
});
describe('without encoding set', () => {
it('should buffer if asked', () => {
return request
.get(`${base}/binary`)
.buffer(true)
.then((res) => {
res.should.have.status(200);
assert(res.headers['content-length']);
assert(res.body.byteLength);
assert.equal(subject, res.body.toString());
});
});
it('should emit buffers', (done) => {
request.get(`${base}/binary`).end((error, res) => {
res.should.have.status(200);
res.headers['content-length'].should.be.below(subject.length);
res.on('data', (chunk) => {
chunk.should.have.length(subject.length);
});
res.on('end', done);
});
});
});
});
|