compression_spec.js
3.58 KB
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
var should = require('should'),
needle = require('./../'),
http = require('http'),
zlib = require('zlib'),
stream = require('stream'),
port = 11123,
server;
describe('compression', function(){
require.bind(null, 'zlib').should.not.throw()
var jsonData = '{"foo":"bar"}';
describe('when server supports compression', function(){
before(function(){
server = http.createServer(function(req, res) {
var raw = new stream.PassThrough();
var acceptEncoding = req.headers['accept-encoding'];
if (!acceptEncoding) {
acceptEncoding = '';
}
if (acceptEncoding.match(/\bdeflate\b/)) {
res.setHeader('Content-Encoding', 'deflate');
raw.pipe(zlib.createDeflate()).pipe(res);
} else if (acceptEncoding.match(/\bgzip\b/)) {
res.setHeader('Content-Encoding', 'gzip');
raw.pipe(zlib.createGzip()).pipe(res);
} else if (acceptEncoding.match(/\bbr\b/)) {
res.setHeader('Content-Encoding', 'br');
raw.pipe(zlib.createBrotliCompress()).pipe(res);
} else {
raw.pipe(res);
}
res.setHeader('Content-Type', 'application/json')
if (req.headers['with-bad']) {
res.end('foo'); // end, no deflate data
} else {
raw.end(jsonData)
}
})
server.listen(port);
});
after(function(done){
server.close(done);
})
describe('and client requests no compression', function() {
it('should have the body decompressed', function(done){
needle.get('localhost:' + port, function(err, response, body){
should.ifError(err);
body.should.have.property('foo', 'bar');
response.bytes.should.equal(jsonData.length);
done();
})
})
})
describe('and client requests gzip compression', function() {
it('should have the body decompressed', function(done){
needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'gzip'}}, function(err, response, body){
should.ifError(err);
body.should.have.property('foo', 'bar');
response.bytes.should.not.equal(jsonData.length);
done();
})
})
})
describe('and client requests deflate compression', function() {
it('should have the body decompressed', function(done){
needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate'}}, function(err, response, body){
should.ifError(err);
body.should.have.property('foo', 'bar');
response.bytes.should.not.equal(jsonData.length);
done();
})
})
it('should rethrow errors from decompressors', function(done){
needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate', 'With-Bad': 'true'}}, function(err, response, body) {
should.exist(err);
err.message.should.equal("incorrect header check");
err.code.should.equal("Z_DATA_ERROR")
done();
})
})
})
describe('and client requests brotli compression', function() {
it('should have the body decompressed', function(done){
// Skip this test if Brotli is not supported
if (typeof zlib.BrotliDecompress !== 'function') {
return done();
}
needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'br'}}, function(err, response, body){
should.ifError(err);
body.should.have.property('foo', 'bar');
response.bytes.should.not.equal(jsonData.length);
done();
})
})
})
})
})