Skip to content

Commit

Permalink
Remove compress dependency (#971)
Browse files Browse the repository at this point in the history
* Remove compress dependency in favor of zlib.

* Add server compression test.

* Document server compression test.
  • Loading branch information
tdecaluwe authored and herom committed Oct 1, 2017
1 parent 84628de commit 6a7a49a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 15 deletions.
23 changes: 9 additions & 14 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ function getDateString(d) {
}

var url = require('url'),
compress = null,
zlib = null,
events = require('events'),
util = require('util'),
findPrefix = require('./utils').findPrefix;

try {
compress = require("compress");
zlib = require("zlib");
} catch (error) {
}

Expand Down Expand Up @@ -187,24 +187,19 @@ Server.prototype._requestListener = function (req, res) {
return self._processRequestXml(req, res, req.body.toString());
}

var chunks = [], gunzip;
if (compress && req.headers["content-encoding"] === "gzip") {
gunzip = new compress.Gunzip();
gunzip.init();
var chunks = [], gunzip, source = req;
if (req.headers["content-encoding"] === "gzip") {
gunzip = zlib.createGunzip();
req.pipe(gunzip);
source = gunzip;
}
req.on('data', function (chunk) {
if (gunzip)
chunk = gunzip.inflate(chunk, "binary");
source.on('data', function (chunk) {
chunks.push(chunk);
});
req.on('end', function () {
source.on('end', function () {
var xml = chunks.join('');
var result;
var error;
if (gunzip) {
gunzip.end();
gunzip = null;
}
self._processRequestXml(req, res, xml);
});
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"author": "Vinay Pulim <v@pulim.com>",
"dependencies": {
"bluebird": "^3.5.0",
"compress": "^0.99.0",
"concat-stream": "^1.5.1",
"debug": "^2.6.9",
"ejs": "~2.5.5",
Expand Down
89 changes: 89 additions & 0 deletions test/server-compress-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

var fs = require('fs');
var soap = require('..');
var assert = require('assert');

var http = require('http');
var zlib = require('zlib');

var path = 'test/request-response-samples/DefaultNamespace__no_xmlns_prefix_used_for_default_namespace/';

var wsdl = path + 'soap.wsdl'

var xml = fs.readFileSync(path + '/soap.wsdl', 'utf8');
var json = fs.readFileSync(path + '/request.json', 'utf8');
var request = fs.readFileSync(path + '/request.xml', 'utf8');
var response = fs.readFileSync(path + '/response.xml', 'utf8');

var service = {
MyService: {
MyServicePort: {
DefaultNamespace: function (args) {
return JSON.parse(json);
}
}
}
};

describe('SOAP Server', function () {
// This test sends two requests and checks the responses for equality. The
// first request is sent through a soap client. The second request sends the
// same request in gzipped format.
it('should properly handle compression', function (done) {
var server = http.createServer(function (req, res) {});
var clientResponse, gzipResponse;

// If both arguments are defined, check if they are equal and exit the test.
var check = function (a, b) {
if (a && b) {
assert(a === b);
done();
}
}

server.listen(8000);
soap.listen(server, '/wsdl', service, xml);

soap.createClient(wsdl, {
endpoint: 'http://localhost:8000/wsdl'
}, function (error, client) {
assert(!error);
client.DefaultNamespace(json, function (error, response) {
assert(!error);
clientResponse = client.lastResponse;
check(clientResponse, gzipResponse);
});
});

var gzip = zlib.createGzip();

// Construct a request with the appropriate headers.
gzip.pipe(http.request({
host: 'localhost',
path: '/wsdl',
port: 8000,
method: 'POST',
headers: {
'content-type': 'text/xml; charset=utf-8',
'content-encoding': 'gzip',
'soapaction': '"DefaultNamespace"'
}
}, function (res) {
var body = '';
res.on('data', function (data) {
// Parse the response body.
body += data;
});
res.on('end', function () {
gzipResponse = body;
check(clientResponse, gzipResponse);
// Don't forget to close the server.
server.close();
});
}));

// Send the request body through the gzip stream to the server.
gzip.end(request);
});
});

0 comments on commit 6a7a49a

Please sign in to comment.