Permalink
Browse files

all tests pass

  • Loading branch information...
1 parent 6e2ecfd commit f61de5af4a7caca73e437adde8bda8a3502af3dd @pghalliday committed Nov 16, 2012
Showing with 49 additions and 51 deletions.
  1. 0 grunt.sh
  2. +15 −10 src/LocalTunnel.js
  3. +29 −35 test/src/LocalTunnel.js
  4. +5 −6 test/support/childProcess.js
View
0 grunt.sh 100644 → 100755
No changes.
View
@@ -1,3 +1,6 @@
+var util = require('util'),
+ EventEmitter = require('events').EventEmitter;
+
function LocalTunnel(port, binary) {
var self = this,
child,
@@ -15,7 +18,7 @@ function LocalTunnel(port, binary) {
self.start = function(callback) {
if (started) {
- callback(new Error('local tunnel already started'));
+ self.emit('error', new Error('local tunnel already started'));
} else {
var stdoutData = '';
var stdout;
@@ -35,35 +38,37 @@ function LocalTunnel(port, binary) {
}
child.on('exit', function(code, signal) {
+ child = null;
if (!started) {
- callback(new Error('localtunnel failed to start:\n' + stdoutData));
+ self.emit('error', new Error('localtunnel failed to start:\n' + stdoutData));
+ } else {
+ started = false;
+ self.emit('stopped');
}
- child = null;
});
stdout.setEncoding();
stdout.on('data', function(data) {
stdoutData += data.toString();
var match = stdoutData.match(/[a-z0-9]{4}\.localtunnel\.com/g);
- if (match) {
+ if (match && !started) {
started = true;
- callback(null, match[0]);
+ self.once('started', callback);
+ self.emit('started', match[0]);
}
});
}
};
self.stop = function(callback) {
if (!started) {
- callback(new Error('localtunnel not active'));
+ self.emit('error', new Error('localtunnel not active'));
} else {
- child.on('exit', function(code, signal) {
- started = false;
- callback();
- });
+ self.once('stopped', callback);
child.kill();
}
};
}
+util.inherits(LocalTunnel, EventEmitter);
module.exports = LocalTunnel;
View
@@ -10,38 +10,42 @@ var LOCALTUNNEL_NOT_FOUND_RESPONSE = "Not found";
describe('LocalTunnel', function() {
it('should error if the binary does not exist', function(done) {
var localTunnel = new LocalTunnel(PORT, 'blahblah');
- localTunnel.start(function(error, hostname) {
+ localTunnel.on('error', function(error) {
expect(error.message).to.contain('localtunnel failed to start');
done();
});
+ localTunnel.start(function(hostname) {
+ expect().fail('should not get started event');
+ });
});
it('should error if stopped before started', function(done) {
var localTunnel = new LocalTunnel(PORT);
- localTunnel.stop(function(error) {
+ localTunnel.on('error', function(error) {
expect(error.message).to.be('localtunnel not active');
done();
+ });
+ localTunnel.stop(function() {
+ expect().fail('should not get stopped event');
});
});
it('should start a localtunnel', function(done) {
this.timeout(10000);
var localTunnel = new LocalTunnel(PORT);
- localTunnel.start(function(error, hostname) {
- if (error) {
- expect().fail('Error encountered starting the tunnel:\n' + error);
- }
+ localTunnel.on('error', function(error) {
+ expect().fail('Error encountered starting the tunnel:\n' + error);
+ });
+ localTunnel.start(function(hostname) {
expect(hostname).to.contain('localtunnel.com');
-
var server = http.createServer(function(request, response) {
response.end(TEST_RESPONSE);
});
server.listen(PORT, function() {
http.get({
hostname: hostname
}, function(response) {
- expect(response.statusCode).to.equal(200);
-
+ expect(response.statusCode).to.equal(200);
response.setEncoding();
var responseData = '';
response.on('data', function(data) {
@@ -54,12 +58,6 @@ describe('LocalTunnel', function() {
localTunnel.stop(done);
});
});
- }).on('error', function(error) {
- server.close(function() {
- localTunnel.stop(function() {
- done(error);
- });
- });
});
});
});
@@ -68,17 +66,21 @@ describe('LocalTunnel', function() {
it('should error if started when already running', function(done) {
this.timeout(10000);
var localTunnel = new LocalTunnel(PORT);
- localTunnel.start(function(error, hostname) {
- if (error) {
- expect().fail('Error encountered starting the tunnel:\n' + error);
- } else {
- localTunnel.start(function(error, hostname) {
- expect(error.message).to.be('local tunnel already started');
- localTunnel.stop(function(error) {
- done(error);
- });
+ function tempErrorHandler(error) {
+ expect().fail('Error encountered starting the tunnel:\n' + error);
+ }
+ localTunnel.on('error', tempErrorHandler);
+ localTunnel.start(function(hostname) {
+ localTunnel.removeListener('error', tempErrorHandler);
+ localTunnel.on('error', function(error) {
+ expect(error.message).to.be('local tunnel already started');
+ localTunnel.stop(function(error) {
+ done(error);
});
- }
+ });
+ localTunnel.start(function(hostname) {
+ expect().fail('should not get started event');
+ });
});
});
@@ -91,24 +93,16 @@ describe('LocalTunnel', function() {
child.on('exit', done);
} else {
expect(message.hostname).to.contain('localtunnel.com');
- child.on('exit', function() {
+ child.on('exit', function(code, signal) {
var server = http.createServer(function(request, response) {
response.end(TEST_RESPONSE);
});
server.listen(PORT, function() {
http.get({
hostname: message.hostname
}, function(response) {
- expect(response.statusCode).to.equal(200);
-
- response.setEncoding();
- var responseData = '';
- response.on('data', function(data) {
- responseData += data;
- });
+ expect(response.statusCode).to.equal(501);
response.on('end', function() {
- expect(responseData).to.equal(LOCALTUNNEL_NOT_FOUND_RESPONSE);
-
server.close(done);
});
});
@@ -1,11 +1,10 @@
var LocalTunnel = require('../../');
var localTunnel = new LocalTunnel(process.argv[2]);
-localTunnel.start(function(error, hostname) {
- if (error) {
- process.send({error: error.toString()});
- } else {
- process.send({hostname: hostname});
- }
+localTunnel.on('error', function(error) {
+ process.send({error: error.toString()});
+});
+localTunnel.start(function(hostname) {
+ process.send({hostname: hostname});
});

0 comments on commit f61de5a

Please sign in to comment.