test.js
4.52 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var tape = require('tape');
var resumer = require('resumer');
var Parser = require('./');
tape('Parse single line - normal', function(t) {
var str = '231 User logged out; service terminated.\n';
var stream = resumer().queue(str).end();
var parser = new Parser();
stream.pipe(parser);
parser.on('readable', function() {
var line;
line = parser.read();
t.equal(line.code, 231);
t.equal(line.isMark, false);
t.equal(line.isError, false);
t.equal(line.text, '231 User logged out; service terminated.');
t.end();
});
});
tape('Parse multiline response', function(t) {
var res = '150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' +
'150 This line ends the mark\n' +
'226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226\n';
var stream = resumer().queue(res).end();
var parser = new Parser();
stream.pipe(parser);
var responses = [];
parser.on('readable', function() {
var line;
while (line = parser.read()) {
responses.push(line);
}
});
parser.on('end', function() {
t.equal(2, responses.length);
t.equal(150, responses[0].code);
t.equal(226, responses[1].code);
t.equal(true, responses[0].isMark);
t.equal(false, responses[1].isMark);
t.equal('150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' +
'150 This line ends the mark', responses[0].text);
t.equal('226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226',
responses[1].text);
t.end();
});
});
tape('Parse multiline response 2', function(t) {
var res = '150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' +
'150 This line ends the mark\n';
var res2 = '226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226\n';
var stream = resumer().queue(res);
var stream2 = resumer().queue(res2).end();
var parser = new Parser();
stream.pipe(parser);
stream2.pipe(parser);
var responses = [];
parser.on('readable', function() {
var line;
while (line = parser.read()) {
responses.push(line);
}
});
parser.on('end', function() {
t.equal(2, responses.length);
t.equal(150, responses[0].code);
t.equal(226, responses[1].code);
t.equal(true, responses[0].isMark);
t.equal(false, responses[1].isMark);
t.equal('150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' +
'150 This line ends the mark', responses[0].text);
t.equal('226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226',
responses[1].text);
t.end();
});
});
tape('Parse multiline response 3', function(t) {
var res = '150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' + '1'
var res2 = '50 This line ends the mark\n226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226\n';
var stream = resumer().queue(res);
var stream2 = resumer().queue(res2).end();
var parser = new Parser();
stream.pipe(parser);
stream2.pipe(parser);
var responses = [];
parser.on('readable', function() {
var line;
while (line = parser.read()) {
responses.push(line);
}
});
parser.on('end', function() {
t.equal(2, responses.length);
t.equal(150, responses[0].code);
t.equal(226, responses[1].code);
t.equal(true, responses[0].isMark);
t.equal(false, responses[1].isMark);
t.equal(responses[0].text, '150-This is the first line of a mark\n' +
'123-This line does not end the mark;note the hyphen\n' +
'150 This line ends the mark');
t.equal(responses[1].text, '226-This is the first line of the second response\n' +
' 226 This line does not end the response;note the leading space\n' +
'226 This is the last line of the response, using code 226');
t.end();
});
});