3 # Author:: Francis Cianfrocca (gmail: blackhedd)
\r
4 # Homepage:: http://rubyeventmachine.com
\r
5 # Date:: 8 April 2006
\r
7 # See EventMachine and EventMachine::Connection for documentation and
\r
10 #----------------------------------------------------------------------------
\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
\r
15 # This program is free software; you can redistribute it and/or modify
\r
16 # it under the terms of either: 1) the GNU General Public License
\r
17 # as published by the Free Software Foundation; either version 2 of the
\r
18 # License, or (at your option) any later version; or 2) Ruby's License.
\r
20 # See the file COPYING for complete licensing information.
\r
22 #---------------------------------------------------------------------------
\r
29 require 'eventmachine'
\r
32 # TODO!!! Need tests for overlength headers and text bodies.
\r
34 class TestLineText2 < Test::Unit::TestCase
\r
36 # Run each of these tests two ways: passing in the whole test-dataset in one chunk,
\r
37 # and passing it in one character at a time.
\r
40 include EM::Protocols::LineText2
\r
42 def receive_line line
\r
43 (@lines ||= []) << line
\r
47 testdata = "Line 1\nLine 2\r\nLine 3\n"
\r
50 a.receive_data testdata
\r
51 assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
\r
54 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
55 assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
\r
58 class ChangeDelimiter
\r
59 include EM::Protocols::LineText2
\r
61 def initialize *args
\r
64 set_delimiter @delim
\r
66 def receive_line line
\r
67 (@lines ||= []) << line
\r
68 set_delimiter( @delim.succ! )
\r
72 def test_change_delimiter
\r
73 testdata = %Q(LineaALinebBLinecCLinedD)
\r
75 a = ChangeDelimiter.new
\r
76 a.receive_data testdata
\r
77 assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
\r
79 a = ChangeDelimiter.new
\r
80 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
81 assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
\r
86 # Test two lines followed by an empty line, ten bytes of binary data, then
\r
90 include EM::Protocols::LineText2
\r
91 attr_reader :lines, :body
\r
92 def initialize *args
\r
104 def receive_binary_data data
\r
110 testdata = %Q(Line 1
\r
118 a.receive_data testdata
\r
119 assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
\r
120 assert_equal( "0000000000", a.body )
\r
123 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
124 assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
\r
125 assert_equal( "0000000000", a.body )
\r
129 # Test unsized binary data. The expectation is that each chunk of it
\r
130 # will be passed to us as it it received.
\r
131 class UnsizedBinary
\r
132 include EM::Protocols::LineText2
\r
133 attr_reader :n_calls, :body
\r
134 def initialize *args
\r
138 def receive_binary_data data
\r
141 (@body ||= "") << data
\r
145 def test_unsized_binary
\r
146 testdata = "X\0" * 1000
\r
148 a = UnsizedBinary.new
\r
149 a.receive_data testdata
\r
150 assert_equal( 1, a.n_calls )
\r
151 assert_equal( testdata, a.body )
\r
153 a = UnsizedBinary.new
\r
154 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
155 assert_equal( 2000, a.n_calls )
\r
156 assert_equal( testdata, a.body )
\r
160 # Test binary data with a "throw back" into line-mode.
\r
162 include EM::Protocols::LineText2
\r
163 attr_reader :headers
\r
164 def initialize *args
\r
170 def receive_binary_data data
\r
171 wanted = 25 - @n_bytes
\r
172 will_take = if data.length > wanted
\r
173 data.length - wanted
\r
177 @n_bytes += will_take
\r
180 set_line_mode( data[will_take..-1] )
\r
183 def receive_line ln
\r
187 def test_throw_back
\r
188 testdata = "Line\n" * 10
\r
191 a.receive_data testdata
\r
192 assert_equal( ["Line"] * 5, a.headers )
\r
195 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
196 assert_equal( ["Line"] * 5, a.headers )
\r
199 # Test multi-character line delimiters.
\r
200 # Also note that the test data has a "tail" with no delimiter, that will be
\r
201 # discarded, but cf. the BinaryTail test.
\r
202 # TODO!!! This test doesn't work in the byte-by-byte case.
\r
204 include EM::Protocols::LineText2
\r
206 def initialize *args
\r
209 set_delimiter "012"
\r
211 def receive_line ln
\r
216 testdata = "Line012Line012Line012Line"
\r
219 a.receive_data testdata
\r
220 assert_equal( ["Line"]*3, a.lines )
\r
223 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
224 # DOESN'T WORK in this case. Multi-character delimiters are broken.
\r
225 #assert_equal( ["Line"]*3, a.lines )
\r
228 # Test a binary "tail," when a sized binary transfer doesn't complete because
\r
229 # of an unbind. We get a partial result.
\r
231 include EM::Protocols::LineText2
\r
233 def initialize *args
\r
238 def receive_binary_data data
\r
239 # we expect to get all the data in one chunk, even in the byte-by-byte case,
\r
240 # because sized transfers by definition give us exactly one call to
\r
241 # #receive_binary_data.
\r
245 def test_binary_tail
\r
246 testdata = "0" * 500
\r
249 a.receive_data testdata
\r
251 assert_equal( "0" * 500, a.data )
\r
254 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
256 assert_equal( "0" * 500, a.data )
\r
260 # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
\r
261 # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
\r
263 # At the present time, this isn't strictly necessary with sized binary chunks because by
\r
264 # definition we accumulate them and make exactly one call to receive_binary_data, but
\r
265 # we may want to support a mode in the future that would break up large chunks into multiple
\r
268 include EM::Protocols::LineText2
\r
269 attr_reader :data, :end
\r
270 def initialize *args
\r
275 def receive_binary_data data
\r
276 # we expect to get all the data in one chunk, even in the byte-by-byte case,
\r
277 # because sized transfers by definition give us exactly one call to
\r
278 # #receive_binary_data.
\r
281 def receive_end_of_binary_data
\r
285 def test_receive_end_of_binary_data
\r
286 testdata = "_" * 1000
\r
288 testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
\r
289 assert_equal( "_" * 1000, a.data )
\r
294 # This tests a bug fix in which calling set_text_mode failed when called
\r
295 # inside receive_binary_data.
\r
298 include EM::Protocols::LineText2
\r
300 def initialize *args
\r
305 def receive_binary_data dt
\r
306 @sizes << dt.length
\r
307 set_text_mode( (dt.length == 1) ? 2 : 1 )
\r
310 def test_binary_pairs
\r
311 test_data = "123" * 5
\r
313 a.receive_data test_data
\r
314 assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
\r