forked from smfreegard/Haraka
/
basic.js
111 lines (90 loc) · 3.21 KB
/
basic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var MessageStream = require('./messagestream');
test.expect(21);
var server = {notes: {}};
exports.get_pool(server);
var pool_name = '25:localhost:300';
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 message_stream = new MessageStream(
{ main : { spool_after : 1024 } }, "123456789"
);
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(message_stream);
message_stream.on('end', function () {
smtp_client.socket.write('.\r\n');
});
message_stream.add_line('Header: test\r\n');
message_stream.add_line('\r\n');
message_stream.add_line('hi\r\n');
message_stream.add_line_end();
});
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());
});