Changeset 283
- Timestamp:
- 11/16/06 09:36:29 (2 years ago)
- Files:
-
- version_0/lib/protocols/line_and_text.rb (modified) (3 diffs)
- version_0/Rakefile (modified) (1 diff)
- version_0/tests/test_ltp.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
version_0/lib/protocols/line_and_text.rb
r281 r283 41 41 42 42 class LineAndTextProtocol < Connection 43 MaxLineLength = 16*1024 44 MaxBinaryLength = 32*1024*1024 45 43 46 def initialize *args 44 47 super … … 49 52 @lbp_data << data 50 53 while i = @lbp_data.index("\n") 51 # raise something if the line is "too long" 54 # line-length test is provisional. Need to be tunable and do something 55 # more intelligent than throwing something. 56 if i > MaxLineLength 57 receive_error("overlength line") if respond_to?(:receive_error) 58 close_connection 59 break # exit the while loop 60 end 61 STDERR.puts "Doing it!" if i > MaxLineLength 62 raise "overlength line" if i > MaxLineLength 52 63 line = @lbp_data.slice!(0..i).chomp 53 64 receive_line line if respond_to?(:receive_line) … … 103 114 @lbp_binary_limit = size.to_i # (nil will be stored as zero) 104 115 if @lbp_binary_limit > 0 105 raise "Overlength" if @lbp_binary_limit > 32*1024*1024# arbitrary sanity check116 raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check 106 117 @lbp_binary_buffer = "\0" * @lbp_binary_limit 107 118 @lbp_binary_bytes_received = 0 version_0/Rakefile
r277 r283 161 161 end 162 162 163 desc "PROVISIONAL: run tests for line/text protocol handler" 164 task :test_ltp do |t| 165 run_tests t, :extension, "test_ltp.rb" 166 end 167 163 168 164 169 desc "Build everything" version_0/tests/test_ltp.rb
r282 r283 40 40 class TestLineAndTextProtocol < Test::Unit::TestCase 41 41 42 def setup43 end42 TestHost = "127.0.0.1" 43 TestPort = 8905 44 44 45 def teardown46 end47 45 48 #-------------------------------------46 #-------------------------------------------------------------------- 49 47 50 def test_http_client 51 ok = false 52 EventMachine.run { 53 c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80 54 c.callback { 55 ok = true 56 EventMachine.stop 57 } 58 c.errback {EventMachine.stop} # necessary, otherwise a failure blocks the test suite forever. 59 } 60 assert ok 61 end 48 class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol 49 def receive_line line 50 @line_buffer << line 51 end 52 end 62 53 63 #------------------------------------- 54 def test_simple_lines 55 lines_received = [] 56 Thread.abort_on_exception = true 57 EventMachine.run { 58 EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn| 59 conn.instance_eval "@line_buffer = lines_received" 60 end 61 EventMachine.add_timer(4) {raise "test timed out"} 62 EventMachine.defer proc { 63 t = TCPSocket.new TestHost, TestPort 64 t.write [ 65 "aaa\n", "bbb\r\n", "ccc\n" 66 ].join 67 t.close 68 }, proc { 69 EventMachine.stop 70 } 71 } 72 assert_equal( %w(aaa bbb ccc), lines_received ) 73 end 64 74 65 def test_http_client_1 66 ok = false 67 EventMachine.run { 68 c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80 69 c.callback {ok = true; EventMachine.stop} 70 c.errback {EventMachine.stop} 71 } 72 assert ok 73 end 75 #-------------------------------------------------------------------- 74 76 75 #------------------------------------- 77 class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol 78 def receive_error text 79 @error_message << text 80 end 81 end 76 82 77 def test_http_client_2 78 ok = false 79 EventMachine.run { 80 c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80 81 c.callback {|result| 82 p result 83 ok = true; 84 EventMachine.stop 85 } 86 c.errback {EventMachine.stop} 87 } 88 assert ok 89 end 83 def test_overlength_lines 84 lines_received = [] 85 Thread.abort_on_exception = true 86 EventMachine.run { 87 EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn| 88 conn.instance_eval "@error_message = lines_received" 89 end 90 EventMachine.add_timer(4) {raise "test timed out"} 91 EventMachine.defer proc { 92 t = TCPSocket.new TestHost, TestPort 93 t.write "a" * (16*1024 + 1) 94 t.write "\n" 95 t.close 96 }, proc { 97 EventMachine.stop 98 } 99 } 100 assert_equal( ["overlength line"], lines_received ) 101 end 90 102 103 104 #-------------------------------------------------------------------- 105 106 class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol 107 def post_init 108 end 109 def receive_line line 110 if line =~ /content-length:\s*(\d+)/i 111 @content_lenth = $1.to_i 112 elsif line.length == 0 113 set_binary_mode @content_length 114 end 115 end 116 def receive_binary_data text 117 send_data "received #{text.length} bytes" 118 close_connection_after_writing 119 end 120 end 121 122 def test_lines_and_text 123 output = nil 124 lines_received = [] 125 text_received = [] 126 Thread.abort_on_exception = true 127 EventMachine.run { 128 EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn| 129 conn.instance_eval "@lines = lines_received; @text = text_received" 130 end 131 EventMachine.add_timer(4) {raise "test timed out"} 132 EventMachine.defer proc { 133 t = TCPSocket.new TestHost, TestPort 134 t.puts "Content-length: 400" 135 t.puts 136 t.write "A" * 400 137 output = t.read 138 t.close 139 }, proc { 140 EventMachine.stop 141 } 142 } 143 assert_equal( "received 400 bytes", output ) 144 end 145 146 #-------------------------------------------------------------------- 91 147 92 148 end