2017-05-11 12:53:16 -07:00
|
|
|
'use strict';
|
2017-10-24 10:05:49 +02:00
|
|
|
/* eslint-disable func-names */
|
2017-05-11 12:53:16 -07:00
|
|
|
|
|
|
|
|
// core requires
|
2017-11-20 12:55:43 -08:00
|
|
|
var http = require('http');
|
2017-05-11 12:53:16 -07:00
|
|
|
|
|
|
|
|
// external requires
|
|
|
|
|
var assert = require('chai').assert;
|
|
|
|
|
var restify = require('../../lib/index.js');
|
|
|
|
|
var restifyClients = require('restify-clients');
|
|
|
|
|
|
|
|
|
|
// local files
|
|
|
|
|
var helper = require('../lib/helper');
|
|
|
|
|
|
|
|
|
|
// local globals
|
|
|
|
|
var SERVER;
|
|
|
|
|
var CLIENT;
|
|
|
|
|
var PORT;
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
describe('body reader', function() {
|
|
|
|
|
describe('gzip content encoding', function() {
|
|
|
|
|
beforeEach(function(done) {
|
2017-05-11 12:53:16 -07:00
|
|
|
SERVER = restify.createServer({
|
|
|
|
|
dtrace: helper.dtrace,
|
|
|
|
|
log: helper.getLog('server')
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
SERVER.listen(0, '127.0.0.1', function() {
|
2017-05-11 12:53:16 -07:00
|
|
|
PORT = SERVER.address().port;
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
afterEach(function(done) {
|
2017-05-11 12:53:16 -07:00
|
|
|
CLIENT.close();
|
|
|
|
|
SERVER.close(done);
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
it('should parse gzip encoded content', function(done) {
|
2017-05-12 17:48:56 -07:00
|
|
|
SERVER.use(restify.plugins.bodyParser());
|
2017-05-11 12:53:16 -07:00
|
|
|
|
|
|
|
|
CLIENT = restifyClients.createJsonClient({
|
|
|
|
|
url: 'http://127.0.0.1:' + PORT,
|
|
|
|
|
retry: false,
|
|
|
|
|
gzip: {}
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
SERVER.post('/compressed', function(req, res, next) {
|
2017-05-11 12:53:16 -07:00
|
|
|
assert.equal(req.body.apple, 'red');
|
|
|
|
|
res.send();
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
CLIENT.post(
|
|
|
|
|
'/compressed',
|
|
|
|
|
{
|
|
|
|
|
apple: 'red'
|
|
|
|
|
},
|
|
|
|
|
function(err, _, res) {
|
|
|
|
|
assert.ifError(err);
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-05-11 12:53:16 -07:00
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
it('should not accept unsupported content encoding', function(done) {
|
2017-05-12 17:48:56 -07:00
|
|
|
SERVER.use(restify.plugins.bodyParser());
|
2017-05-11 12:53:16 -07:00
|
|
|
|
|
|
|
|
CLIENT = restifyClients.createJsonClient({
|
|
|
|
|
url: 'http://127.0.0.1:' + PORT,
|
|
|
|
|
retry: false,
|
|
|
|
|
headers: {
|
|
|
|
|
'content-encoding': 'unsupported'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
SERVER.post('/compressed', function(req, res, next) {
|
2017-05-11 12:53:16 -07:00
|
|
|
assert.equal(req.body.apple, 'red');
|
|
|
|
|
res.send();
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
CLIENT.post(
|
|
|
|
|
'/compressed',
|
|
|
|
|
{
|
|
|
|
|
apple: 'red'
|
|
|
|
|
},
|
|
|
|
|
function(err, _, res) {
|
|
|
|
|
assert.isOk(err, 'should fail');
|
|
|
|
|
assert.equal(res.statusCode, 415);
|
|
|
|
|
assert.equal(res.headers['accept-encoding'], 'gzip');
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-05-11 12:53:16 -07:00
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
it('should parse unencoded content', function(done) {
|
2017-05-12 17:48:56 -07:00
|
|
|
SERVER.use(restify.plugins.bodyParser());
|
2017-05-11 12:53:16 -07:00
|
|
|
|
|
|
|
|
CLIENT = restifyClients.createJsonClient({
|
|
|
|
|
url: 'http://127.0.0.1:' + PORT,
|
|
|
|
|
retry: false
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
SERVER.post('/compressed', function(req, res, next) {
|
2017-05-11 12:53:16 -07:00
|
|
|
assert.equal(req.body.apple, 'red');
|
|
|
|
|
res.send();
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2017-10-31 19:29:49 +01:00
|
|
|
CLIENT.post(
|
|
|
|
|
'/compressed',
|
|
|
|
|
{
|
|
|
|
|
apple: 'red'
|
|
|
|
|
},
|
|
|
|
|
function(err, _, res) {
|
|
|
|
|
assert.ifError(err);
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-05-11 12:53:16 -07:00
|
|
|
});
|
2017-11-20 12:55:43 -08:00
|
|
|
|
|
|
|
|
it('should handle client timeout', function(done) {
|
|
|
|
|
SERVER.use(restify.plugins.bodyParser());
|
|
|
|
|
|
|
|
|
|
SERVER.post('/compressed', function(req, res, next) {
|
2018-02-23 11:20:44 -08:00
|
|
|
res.send('ok');
|
2017-11-20 12:55:43 -08:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// set timeout to 100ms so test runs faster, when client stops
|
|
|
|
|
// sending POST data
|
|
|
|
|
SERVER.on('connection', function(socket) {
|
|
|
|
|
socket.setTimeout(100);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var postData = 'hello world';
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
hostname: '127.0.0.1',
|
|
|
|
|
port: PORT,
|
2018-02-23 11:20:44 -08:00
|
|
|
path: '/compressed?v=1',
|
2017-11-20 12:55:43 -08:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
// report postData + 1 so that request isn't sent
|
|
|
|
|
'Content-Length': Buffer.byteLength(postData) + 1
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var req = http.request(options, function(res) {
|
|
|
|
|
// should never receive a response
|
|
|
|
|
assert.isNotOk(res);
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-23 11:20:44 -08:00
|
|
|
SERVER.on('after', function(req2) {
|
|
|
|
|
if (req2.href() === '/compressed?v=2') {
|
|
|
|
|
assert.equal(SERVER.inflightRequests(), 0);
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-11-20 12:55:43 -08:00
|
|
|
// will get a req error after 100ms timeout
|
|
|
|
|
req.on('error', function(e) {
|
|
|
|
|
// make another request to verify in flight request is only 1
|
|
|
|
|
CLIENT = restifyClients.createJsonClient({
|
|
|
|
|
url: 'http://127.0.0.1:' + PORT,
|
|
|
|
|
retry: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CLIENT.post(
|
2018-02-23 11:20:44 -08:00
|
|
|
'/compressed?v=2',
|
2017-11-20 12:55:43 -08:00
|
|
|
{
|
|
|
|
|
apple: 'red'
|
|
|
|
|
},
|
|
|
|
|
function(err, _, res, obj) {
|
|
|
|
|
assert.ifError(err);
|
|
|
|
|
assert.equal(res.statusCode, 200);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// write data to request body, but don't req.send()
|
|
|
|
|
req.write(postData);
|
|
|
|
|
});
|
2017-05-11 12:53:16 -07:00
|
|
|
});
|
|
|
|
|
});
|