Merge pull request #1812 from yigaldviri/brotli · forwardemail/superagent@de9d760
@@ -36,18 +36,38 @@ app.get('/binary', (request_, res) => {
3636res.send(buf);
3737});
3838});
39+40+app.get('/binary-brotli', (request_, res) => {
41+zlib.brotliCompress(subject, (error, buf) => {
42+res.set('Content-Encoding', 'br');
43+res.send(buf);
44+});
45+});
46+3947app.get('/corrupt', (request_, res) => {
4048res.set('Content-Encoding', 'gzip');
4149res.send('blah');
4250});
435152+app.get('/corrupt-brotli', (request_, res) => {
53+res.set('Content-Encoding', 'br');
54+res.send('blah');
55+});
56+4457app.get('/nocontent', (request_, res, next) => {
4558res.statusCode = 204;
4659res.set('Content-Type', 'text/plain');
4760res.set('Content-Encoding', 'gzip');
4861res.send('');
4962});
506364+app.get('/nocontent-brotli', (request_, res, next) => {
65+res.statusCode = 204;
66+res.set('Content-Type', 'text/plain');
67+res.set('Content-Encoding', 'br');
68+res.send('');
69+});
70+5171app.get('/', (request_, res, next) => {
5272zlib.deflate(subject, (error, buf) => {
5373res.set('Content-Type', 'text/plain');
@@ -65,6 +85,15 @@ app.get('/junk', (request_, res) => {
6585});
6686});
678788+app.get('/junk-brotli', (request_, res) => {
89+zlib.brotliCompress(subject, (error, buf) => {
90+res.set('Content-Type', 'text/plain');
91+res.set('Content-Encoding', 'br');
92+res.write(buf);
93+res.end(' 0 junk');
94+});
95+});
96+6897app.get('/chopped', (request_, res) => {
6998zlib.deflate(`${subject}123456`, (error, buf) => {
7099res.set('Content-Type', 'text/plain');
@@ -73,6 +102,14 @@ app.get('/chopped', (request_, res) => {
73102});
74103});
75104105+app.get('/chopped-brotli', (request_, res) => {
106+zlib.brotliCompress(`${subject}123456`, (error, buf) => {
107+res.set('Content-Type', 'text/plain');
108+res.set('Content-Encoding', 'br');
109+res.send(buf.slice(0, -1));
110+});
111+});
112+76113describe('zlib', () => {
77114it('should deflate the content', (done) => {
78115request.get(base).end((error, res) => {
@@ -106,6 +143,14 @@ describe('zlib', () => {
106143});
107144});
108145146+it('should ignore trailing junk-brotli', (done) => {
147+request.get(`${base}/junk-brotli`).end((error, res) => {
148+res.should.have.status(200);
149+res.text.should.equal(subject);
150+done();
151+});
152+});
153+109154it('should ignore missing data', (done) => {
110155request.get(`${base}/chopped`).end((error, res) => {
111156assert.equal(undefined, error);
@@ -115,6 +160,15 @@ describe('zlib', () => {
115160});
116161});
117162163+it('should ignore missing brotli data', (done) => {
164+request.get(`${base}/chopped-brotli`).end((error, res) => {
165+assert.equal(undefined, error);
166+res.should.have.status(200);
167+res.text.should.startWith(subject);
168+done();
169+});
170+});
171+118172it('should handle corrupted responses', (done) => {
119173request.get(`${base}/corrupt`).end((error, res) => {
120174assert(error, 'missing error');
@@ -123,6 +177,13 @@ describe('zlib', () => {
123177});
124178});
125179180+it('should handle brotli corrupted responses', (done) => {
181+request.get(`${base}/corrupt-brotli`).end((error, res) => {
182+res.text.should.equal('');
183+done();
184+});
185+});
186+126187it('should handle no content with gzip header', (done) => {
127188request.get(`${base}/nocontent`).end((error, res) => {
128189assert.ifError(error);
@@ -134,6 +195,17 @@ describe('zlib', () => {
134195});
135196});
136197198+it('should handle no content with gzip header', (done) => {
199+request.get(`${base}/nocontent-brotli`).end((error, res) => {
200+assert.ifError(error);
201+assert(res);
202+res.should.have.status(204);
203+res.text.should.equal('');
204+res.headers.should.not.have.property('content-length');
205+done();
206+});
207+});
208+137209describe('without encoding set', () => {
138210it('should buffer if asked', () => {
139211return request
@@ -147,6 +219,18 @@ describe('zlib', () => {
147219});
148220});
149221222+it('should buffer Brotli if asked', () => {
223+return request
224+.get(`${base}/binary-brotli`)
225+.buffer(true)
226+.then((res) => {
227+res.should.have.status(200);
228+assert(res.headers['content-length']);
229+assert(res.body.byteLength);
230+assert.equal(subject, res.body.toString());
231+});
232+});
233+150234it('should emit buffers', (done) => {
151235request.get(`${base}/binary`).end((error, res) => {
152236res.should.have.status(200);