Permalink
Browse files

Filled out more of the smtp_client unit test.

  • Loading branch information...
1 parent 85ea212 commit 8ae0f51bd666e8849cb93010c0b90381921b8a45 Eric Day committed Apr 10, 2012
Showing with 119 additions and 33 deletions.
  1. +1 −0 smtp_client.js
  2. +11 −10 tests/fixtures/line_socket.js
  3. +7 −17 tests/fixtures/stub_plugin.js
  4. +2 −2 tests/fixtures/vm_harness.js
  5. +98 −4 tests/smtp_client/basic.js
View
@@ -90,6 +90,7 @@ function SMTPClient(port, host, timeout, enable_tls) {
self.emit(self.command);
break;
case 'quit':
+ self.emit('quit');
self.pool.destroy(self);
break;
default:
@@ -1,20 +1,21 @@
"use strict";
+var events = require('events');
+var util = require('util');
var stub = require('./stub');
-function Socket() {
- if (!(this instanceof Socket)) return new Socket();
- var self = this;
- self.destroySoon = stub();
- self.setTimeout = stub();
- self.removeAllListeners = stub();
- self.on = stub();
+function Socket(port, host) {
+ events.EventEmitter.call(this);
+ this.port = port;
+ this.host = host;
+ this.setTimeout = stub();
+ this.destroy = stub();
}
+util.inherits(Socket, events.EventEmitter);
+
exports.Socket = Socket;
-// New interface - uses TLS
exports.connect = function (port, host, cb) {
- var sock = new Socket();
- return sock;
+ return new Socket(port, host);
}
@@ -1,10 +1,11 @@
"use strict";
-var stub = require('../fixtures/stub'),
- path = require('path'),
- constants = require('../../constants'),
- vm = require('vm'),
- fs = require('fs');
+var stub = require('./stub'),
+ vm_harness = require('./vm_harness'),
+ path = require('path'),
+ constants = require('../../constants'),
+ vm = require('vm'),
+ fs = require('fs');
function Plugin(name) {
if (false === (this instanceof Plugin)) {
@@ -30,19 +31,8 @@ Plugin.prototype.load_plugin = function(name) {
}
var code = '"use strict";' + rf;
- var sandbox_require = function (id) {
- if (id[0] == '.') {
- try {
- fs.statSync(__dirname + '/' + id + '.js');
- } catch (e) {
- id = '../../' + id;
- }
- }
- return require(id);
- }
-
var sandbox = {
- require: sandbox_require,
+ require: vm_harness.sandbox_require,
__filename: full_path,
__dirname: path.dirname(full_path),
exports: this,
@@ -5,7 +5,7 @@ function dot_files(element) {
return element.match(/^\./) == null;
}
-function sandbox_require(id) {
+exports.sandbox_require = function (id) {
if (id[0] == '.') {
try {
var override = __dirname + '/' + id + '.js';
@@ -23,7 +23,7 @@ function make_test(module_path, test_path) {
var code = fs.readFileSync(module_path);
code += fs.readFileSync(test_path);
var sandbox = {
- require: sandbox_require,
+ require: exports.sandbox_require,
console: console,
exports: {},
test: test
View
@@ -1,7 +1,101 @@
-test.expect(1);
+test.expect(24);
var server = {notes: {}};
+
exports.get_pool(server);
-exports.get_client(server, function(smtp_client) {
- test.equals(1, Object.keys(server.notes.pool).length);
- test.done();
+var pool_name = '25:localhost:300:false';
+test.equals(1, Object.keys(server.notes.pool).length);
+test.equals(pool_name, Object.keys(server.notes.pool)[0]);
+test.equals(0, server.notes.pool[pool_name].getPoolSize());
+test.equals(0, server.notes.pool[pool_name].availableObjectsCount());
+
+exports.get_client(server, function(err, smtp_client) {
+ test.equals(null, err);
+ test.equals(1, server.notes.pool[pool_name].getPoolSize());
+ test.equals(0, server.notes.pool[pool_name].availableObjectsCount());
+
+ var data = [];
+ var reading_body = false;
+ data.push('220 hi');
+
+ smtp_client.on('greeting', function (command) {
+ test.equals(smtp_client.response[0], 'hi');
+ test.equals('EHLO', command);
+ smtp_client.send_command(command, 'example.com');
+ });
+
+ data.push('EHLO example.com');
+ data.push('250 hello');
+
+ smtp_client.on('helo', function () {
+ test.equals(smtp_client.response[0], 'hello');
+ smtp_client.send_command('MAIL', 'FROM: me@example.com');
+ });
+
+ data.push('MAIL FROM: me@example.com');
+ data.push('250 sender ok');
+
+ smtp_client.on('mail', function () {
+ test.equals(smtp_client.response[0], 'sender ok');
+ smtp_client.send_command('RCPT', 'TO: you@example.com');
+ });
+
+ data.push('RCPT TO: you@example.com');
+ data.push('250 recipient ok');
+
+ smtp_client.on('rcpt', function () {
+ test.equals(smtp_client.response[0], 'recipient ok');
+ smtp_client.send_command('DATA');
+ });
+
+ data.push('DATA');
+ data.push('354 go ahead');
+
+ smtp_client.on('data', function () {
+ test.equals(smtp_client.response[0], 'go ahead');
+ smtp_client.start_data(['Header: test\r\n', '\r\n', 'hi\r\n']);
+ });
+
+ data.push('Header: test');
+ data.push('');
+ data.push('hi');
+ data.push('.');
+ data.push('250 message queued');
+
+ smtp_client.on('dot', function () {
+ test.equals(smtp_client.response[0], 'message queued');
+ smtp_client.send_command('QUIT');
+ });
+
+ data.push('QUIT');
+ data.push('221 goodbye');
+
+ smtp_client.on('quit', function () {
+ test.equals(smtp_client.response[0], 'goodbye');
+ test.done();
+ });
+
+ smtp_client.socket.write = function (line) {
+ if (data.length == 0) {
+ test.ok(false);
+ return;
+ }
+ test.equals(data.shift() + '\r\n', line);
+ if (reading_body && line == '.\r\n') {
+ reading_body = false;
+ }
+ if (!reading_body) {
+ if (line == 'DATA\r\n') {
+ reading_body = true;
+ }
+ while (true) {
+ var line = data.shift();
+ this.emit('line', line + '\r\n');
+ if (line[3] == ' ') break;
+ }
+ }
+
+ return true;
+ };
+
+ smtp_client.socket.emit('line', data.shift());
});

0 comments on commit 8ae0f51

Please sign in to comment.