2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2016-11-15 21:46:23 +00:00
|
|
|
var loopback = require('../');
|
2014-03-18 07:43:24 +00:00
|
|
|
var net = require('net');
|
2016-11-15 21:46:23 +00:00
|
|
|
|
2014-03-18 07:43:24 +00:00
|
|
|
describe('loopback application', function() {
|
|
|
|
it('pauses request stream during authentication', function(done) {
|
|
|
|
// This test reproduces the issue reported in
|
|
|
|
// https://github.com/strongloop/loopback-storage-service/issues/7
|
|
|
|
var app = loopback();
|
|
|
|
setupAppWithStreamingMethod();
|
|
|
|
|
|
|
|
app.listen(0, function() {
|
|
|
|
sendHttpRequestInOnePacket(
|
|
|
|
this.address().port,
|
|
|
|
'POST /streamers/read HTTP/1.0\n' +
|
|
|
|
'Content-Length: 1\n' +
|
|
|
|
'Content-Type: application/x-custom-octet-stream\n' +
|
|
|
|
'\n' +
|
|
|
|
'X',
|
|
|
|
function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-18 07:43:24 +00:00
|
|
|
expect(res).to.match(/\nX$/);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-18 07:43:24 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function setupAppWithStreamingMethod() {
|
|
|
|
app.dataSource('db', {
|
|
|
|
connector: loopback.Memory,
|
|
|
|
});
|
|
|
|
var db = app.datasources.db;
|
|
|
|
|
|
|
|
loopback.User.attachTo(db);
|
|
|
|
loopback.AccessToken.attachTo(db);
|
|
|
|
loopback.Role.attachTo(db);
|
|
|
|
loopback.ACL.attachTo(db);
|
2016-11-15 21:46:23 +00:00
|
|
|
loopback.User.hasMany(loopback.AccessToken, {as: 'accessTokens'});
|
2014-03-18 07:43:24 +00:00
|
|
|
|
2016-06-03 20:51:48 +00:00
|
|
|
var Streamer = app.registry.createModel('Streamer');
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Streamer, {dataSource: 'db'});
|
2014-03-18 07:43:24 +00:00
|
|
|
Streamer.read = function(req, res, cb) {
|
|
|
|
var body = new Buffer(0);
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
body += chunk;
|
|
|
|
});
|
|
|
|
req.on('end', function() {
|
|
|
|
res.end(body.toString());
|
|
|
|
// we must not call the callback here
|
|
|
|
// because it will attempt to add response headers
|
|
|
|
});
|
|
|
|
req.once('error', function(err) {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
loopback.remoteMethod(Streamer.read, {
|
2016-11-15 21:46:23 +00:00
|
|
|
http: {method: 'post'},
|
2014-03-18 07:43:24 +00:00
|
|
|
accepts: [
|
2016-11-15 21:46:23 +00:00
|
|
|
{arg: 'req', type: 'Object', http: {source: 'req'}},
|
|
|
|
{arg: 'res', type: 'Object', http: {source: 'res'}},
|
2016-04-01 09:14:26 +00:00
|
|
|
],
|
2014-03-18 07:43:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.enableAuth();
|
2016-11-15 21:46:23 +00:00
|
|
|
app.use(loopback.token({model: app.models.accessToken}));
|
2014-03-18 07:43:24 +00:00
|
|
|
app.use(loopback.rest());
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendHttpRequestInOnePacket(port, reqString, cb) {
|
|
|
|
var socket = net.createConnection(port);
|
|
|
|
var response = new Buffer(0);
|
|
|
|
|
|
|
|
socket.on('data', function(chunk) {
|
|
|
|
response += chunk;
|
|
|
|
});
|
|
|
|
socket.on('end', function() {
|
|
|
|
callCb(null, response.toString());
|
|
|
|
});
|
|
|
|
socket.once('error', function(err) {
|
|
|
|
callCb(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.write(reqString.replace(/\n/g, '\r\n'));
|
|
|
|
|
|
|
|
function callCb(err, res) {
|
|
|
|
if (!cb) return;
|
|
|
|
cb(err, res);
|
|
|
|
cb = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|