source: trip-planner-front/node_modules/needle/test/compression_spec.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1var should = require('should'),
2 needle = require('./../'),
3 http = require('http'),
4 zlib = require('zlib'),
5 stream = require('stream'),
6 port = 11123,
7 server;
8
9describe('compression', function(){
10
11 require.bind(null, 'zlib').should.not.throw()
12
13 var jsonData = '{"foo":"bar"}';
14
15 describe('when server supports compression', function(){
16
17 before(function(){
18 server = http.createServer(function(req, res) {
19 var raw = new stream.PassThrough();
20
21 var acceptEncoding = req.headers['accept-encoding'];
22 if (!acceptEncoding) {
23 acceptEncoding = '';
24 }
25
26 if (acceptEncoding.match(/\bdeflate\b/)) {
27 res.setHeader('Content-Encoding', 'deflate');
28 raw.pipe(zlib.createDeflate()).pipe(res);
29 } else if (acceptEncoding.match(/\bgzip\b/)) {
30 res.setHeader('Content-Encoding', 'gzip');
31 raw.pipe(zlib.createGzip()).pipe(res);
32 } else if (acceptEncoding.match(/\bbr\b/)) {
33 res.setHeader('Content-Encoding', 'br');
34 raw.pipe(zlib.createBrotliCompress()).pipe(res);
35 } else {
36 raw.pipe(res);
37 }
38
39 res.setHeader('Content-Type', 'application/json')
40 if (req.headers['with-bad']) {
41 res.end('foo'); // end, no deflate data
42 } else {
43 raw.end(jsonData)
44 }
45
46 })
47
48 server.listen(port);
49 });
50
51 after(function(done){
52 server.close(done);
53 })
54
55 describe('and client requests no compression', function() {
56 it('should have the body decompressed', function(done){
57 needle.get('localhost:' + port, function(err, response, body){
58 should.ifError(err);
59 body.should.have.property('foo', 'bar');
60 response.bytes.should.equal(jsonData.length);
61 done();
62 })
63 })
64 })
65
66 describe('and client requests gzip compression', function() {
67 it('should have the body decompressed', function(done){
68 needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'gzip'}}, function(err, response, body){
69 should.ifError(err);
70 body.should.have.property('foo', 'bar');
71 response.bytes.should.not.equal(jsonData.length);
72 done();
73 })
74 })
75 })
76
77 describe('and client requests deflate compression', function() {
78 it('should have the body decompressed', function(done){
79 needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate'}}, function(err, response, body){
80 should.ifError(err);
81 body.should.have.property('foo', 'bar');
82 response.bytes.should.not.equal(jsonData.length);
83 done();
84 })
85 })
86
87 it('should rethrow errors from decompressors', function(done){
88 needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'deflate', 'With-Bad': 'true'}}, function(err, response, body) {
89 should.exist(err);
90 err.message.should.equal("incorrect header check");
91 err.code.should.equal("Z_DATA_ERROR")
92 done();
93 })
94 })
95 })
96
97 describe('and client requests brotli compression', function() {
98 it('should have the body decompressed', function(done){
99 // Skip this test if Brotli is not supported
100 if (typeof zlib.BrotliDecompress !== 'function') {
101 return done();
102 }
103 needle.get('localhost:' + port, {headers: {'Accept-Encoding': 'br'}}, function(err, response, body){
104 should.ifError(err);
105 body.should.have.property('foo', 'bar');
106 response.bytes.should.not.equal(jsonData.length);
107 done();
108 })
109 })
110 })
111 })
112})
Note: See TracBrowser for help on using the repository browser.