| 40 | | TestHost = "127.0.0.1" |
|---|
| 41 | | TestPort = 8905 |
|---|
| 42 | | |
|---|
| 43 | | |
|---|
| 44 | | #-------------------------------------------------------------------- |
|---|
| 45 | | |
|---|
| 46 | | class SimpleTest < EventMachine::Protocols::HeaderAndContentProtocol |
|---|
| 47 | | attr_reader :first_header, :my_headers, :request |
|---|
| 48 | | |
|---|
| 49 | | def receive_first_header_line hdr |
|---|
| 50 | | @first_header ||= [] |
|---|
| 51 | | @first_header << hdr |
|---|
| 52 | | end |
|---|
| 53 | | def receive_headers hdrs |
|---|
| 54 | | @my_headers ||= [] |
|---|
| 55 | | @my_headers << hdrs |
|---|
| 56 | | end |
|---|
| 57 | | def receive_request hdrs, content |
|---|
| 58 | | @request ||= [] |
|---|
| 59 | | @request << [hdrs, content] |
|---|
| 60 | | end |
|---|
| 61 | | end |
|---|
| 62 | | |
|---|
| 63 | | def test_no_content |
|---|
| 64 | | Thread.abort_on_exception = true |
|---|
| 65 | | the_connection = nil |
|---|
| 66 | | EventMachine.run { |
|---|
| 67 | | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| 68 | | the_connection = conn |
|---|
| 69 | | end |
|---|
| 70 | | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| 71 | | EventMachine.defer proc { |
|---|
| 72 | | t = TCPSocket.new TestHost, TestPort |
|---|
| 73 | | t.write [ |
|---|
| 74 | | "aaa\n", "bbb\r\n", "ccc\n", "\n" |
|---|
| 75 | | ].join |
|---|
| 76 | | t.close |
|---|
| 77 | | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| 78 | | sleep 0.1 |
|---|
| 79 | | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 43 | TestHost = "127.0.0.1" |
|---|
| | 44 | TestPort = 8905 |
|---|
| | 45 | |
|---|
| | 46 | |
|---|
| | 47 | #-------------------------------------------------------------------- |
|---|
| | 48 | |
|---|
| | 49 | class SimpleTest < EventMachine::Protocols::HeaderAndContentProtocol |
|---|
| | 50 | attr_reader :first_header, :my_headers, :request |
|---|
| | 51 | |
|---|
| | 52 | def receive_first_header_line hdr |
|---|
| | 53 | @first_header ||= [] |
|---|
| | 54 | @first_header << hdr |
|---|
| 81 | | }, proc { |
|---|
| 82 | | EventMachine.stop |
|---|
| 83 | | } |
|---|
| 84 | | } |
|---|
| 85 | | assert_equal( ["aaa"], the_connection.first_header ) |
|---|
| 86 | | assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers ) |
|---|
| 87 | | assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request ) |
|---|
| 88 | | end |
|---|
| 89 | | |
|---|
| 90 | | def test_content |
|---|
| 91 | | Thread.abort_on_exception = true |
|---|
| 92 | | the_connection = nil |
|---|
| 93 | | content = "A" * 50 |
|---|
| 94 | | headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"] |
|---|
| 95 | | EventMachine.run { |
|---|
| 96 | | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| 97 | | the_connection = conn |
|---|
| 98 | | end |
|---|
| 99 | | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| 100 | | EventMachine.defer proc { |
|---|
| 101 | | t = TCPSocket.new TestHost, TestPort |
|---|
| 102 | | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| 103 | | t.write "\n" |
|---|
| 104 | | t.write content |
|---|
| 105 | | t.close |
|---|
| 106 | | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| 107 | | sleep 0.1 |
|---|
| 108 | | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 56 | def receive_headers hdrs |
|---|
| | 57 | @my_headers ||= [] |
|---|
| | 58 | @my_headers << hdrs |
|---|
| 110 | | }, proc { |
|---|
| 111 | | EM.stop |
|---|
| 112 | | } |
|---|
| 113 | | } |
|---|
| 114 | | assert_equal( ["aaa"], the_connection.first_header ) |
|---|
| 115 | | assert_equal( [headers], the_connection.my_headers ) |
|---|
| 116 | | assert_equal( [[headers, content]], the_connection.request ) |
|---|
| 117 | | end |
|---|
| 118 | | |
|---|
| 119 | | def test_several_requests |
|---|
| 120 | | Thread.abort_on_exception = true |
|---|
| 121 | | the_connection = nil |
|---|
| 122 | | content = "A" * 50 |
|---|
| 123 | | headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"] |
|---|
| 124 | | EventMachine.run { |
|---|
| 125 | | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| 126 | | the_connection = conn |
|---|
| 127 | | end |
|---|
| 128 | | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| 129 | | EventMachine.defer proc { |
|---|
| 130 | | t = TCPSocket.new TestHost, TestPort |
|---|
| 131 | | 5.times { |
|---|
| 132 | | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| 133 | | t.write "\n" |
|---|
| 134 | | t.write content |
|---|
| 135 | | } |
|---|
| 136 | | t.close |
|---|
| 137 | | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| 138 | | sleep 0.1 |
|---|
| 139 | | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 60 | def receive_request hdrs, content |
|---|
| | 61 | @request ||= [] |
|---|
| | 62 | @request << [hdrs, content] |
|---|
| 141 | | }, proc { |
|---|
| 142 | | EventMachine.stop |
|---|
| 143 | | } |
|---|
| 144 | | } |
|---|
| 145 | | assert_equal( ["aaa"] * 5, the_connection.first_header ) |
|---|
| 146 | | assert_equal( [headers] * 5, the_connection.my_headers ) |
|---|
| 147 | | assert_equal( [[headers, content]] * 5, the_connection.request ) |
|---|
| 148 | | end |
|---|
| | 64 | end |
|---|
| | 65 | |
|---|
| | 66 | |
|---|
| | 67 | def test_no_content |
|---|
| | 68 | Thread.abort_on_exception = true |
|---|
| | 69 | the_connection = nil |
|---|
| | 70 | EventMachine.run { |
|---|
| | 71 | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| | 72 | the_connection = conn |
|---|
| | 73 | end |
|---|
| | 74 | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| | 75 | |
|---|
| | 76 | pr = proc { |
|---|
| | 77 | t = TCPSocket.new TestHost, TestPort |
|---|
| | 78 | t.write [ "aaa\n", "bbb\r\n", "ccc\n", "\n" ].join |
|---|
| | 79 | t.close |
|---|
| | 80 | } |
|---|
| | 81 | |
|---|
| | 82 | if RUBY_PLATFORM =~ /java/i |
|---|
| | 83 | pr.call |
|---|
| | 84 | EM.add_timer(0.5) {EM.stop} |
|---|
| | 85 | else |
|---|
| | 86 | EventMachine.defer proc { |
|---|
| | 87 | pr.call |
|---|
| | 88 | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| | 89 | sleep 0.1 |
|---|
| | 90 | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 91 | end |
|---|
| | 92 | }, proc { |
|---|
| | 93 | EventMachine.stop |
|---|
| | 94 | } |
|---|
| | 95 | end |
|---|
| | 96 | } |
|---|
| | 97 | assert_equal( ["aaa"], the_connection.first_header ) |
|---|
| | 98 | assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers ) |
|---|
| | 99 | assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request ) |
|---|
| | 100 | end |
|---|
| | 101 | |
|---|
| | 102 | |
|---|
| | 103 | |
|---|
| | 104 | |
|---|
| | 105 | def test_content |
|---|
| | 106 | Thread.abort_on_exception = true |
|---|
| | 107 | the_connection = nil |
|---|
| | 108 | content = "A" * 50 |
|---|
| | 109 | headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"] |
|---|
| | 110 | EventMachine.run { |
|---|
| | 111 | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| | 112 | the_connection = conn |
|---|
| | 113 | end |
|---|
| | 114 | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| | 115 | |
|---|
| | 116 | pr = proc { |
|---|
| | 117 | t = TCPSocket.new TestHost, TestPort |
|---|
| | 118 | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| | 119 | t.write "\n" |
|---|
| | 120 | t.write content |
|---|
| | 121 | t.close |
|---|
| | 122 | } |
|---|
| | 123 | |
|---|
| | 124 | if RUBY_PLATFORM =~ /java/i |
|---|
| | 125 | # I/O on threads seems completely unreliable in Java. |
|---|
| | 126 | pr.call |
|---|
| | 127 | EM.add_timer(0.5) {EM.stop} |
|---|
| | 128 | else |
|---|
| | 129 | EventMachine.defer proc { |
|---|
| | 130 | pr.call |
|---|
| | 131 | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| | 132 | sleep 0.1 |
|---|
| | 133 | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 134 | end |
|---|
| | 135 | }, proc { |
|---|
| | 136 | EM.stop |
|---|
| | 137 | } |
|---|
| | 138 | end |
|---|
| | 139 | } |
|---|
| | 140 | assert_equal( ["aaa"], the_connection.first_header ) |
|---|
| | 141 | assert_equal( [headers], the_connection.my_headers ) |
|---|
| | 142 | assert_equal( [[headers, content]], the_connection.request ) |
|---|
| | 143 | end |
|---|
| | 144 | |
|---|
| | 145 | |
|---|
| | 146 | |
|---|
| | 147 | def test_several_requests |
|---|
| | 148 | Thread.abort_on_exception = true |
|---|
| | 149 | the_connection = nil |
|---|
| | 150 | content = "A" * 50 |
|---|
| | 151 | headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"] |
|---|
| | 152 | EventMachine.run { |
|---|
| | 153 | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| | 154 | the_connection = conn |
|---|
| | 155 | end |
|---|
| | 156 | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| | 157 | |
|---|
| | 158 | pr = proc { |
|---|
| | 159 | t = TCPSocket.new TestHost, TestPort |
|---|
| | 160 | 5.times { |
|---|
| | 161 | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| | 162 | t.write "\n" |
|---|
| | 163 | t.write content |
|---|
| | 164 | } |
|---|
| | 165 | t.close |
|---|
| | 166 | } |
|---|
| | 167 | |
|---|
| | 168 | if RUBY_PLATFORM =~ /java/i |
|---|
| | 169 | pr.call |
|---|
| | 170 | EM.add_timer(1) {EM.stop} |
|---|
| | 171 | else |
|---|
| | 172 | EventMachine.defer proc { |
|---|
| | 173 | pr.call |
|---|
| | 174 | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| | 175 | sleep 0.1 |
|---|
| | 176 | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 177 | end |
|---|
| | 178 | }, proc { |
|---|
| | 179 | EventMachine.stop |
|---|
| | 180 | } |
|---|
| | 181 | end |
|---|
| | 182 | } |
|---|
| | 183 | assert_equal( ["aaa"] * 5, the_connection.first_header ) |
|---|
| | 184 | assert_equal( [headers] * 5, the_connection.my_headers ) |
|---|
| | 185 | assert_equal( [[headers, content]] * 5, the_connection.request ) |
|---|
| | 186 | end |
|---|
| 174 | | def test_interpret_headers |
|---|
| 175 | | Thread.abort_on_exception = true |
|---|
| 176 | | the_connection = nil |
|---|
| 177 | | content = "A" * 50 |
|---|
| 178 | | headers = [ |
|---|
| 179 | | "GET / HTTP/1.0", |
|---|
| 180 | | "Accept: aaa", |
|---|
| 181 | | "User-Agent: bbb", |
|---|
| 182 | | "Host: ccc", |
|---|
| 183 | | "x-tempest-header:ddd" |
|---|
| 184 | | ] |
|---|
| 185 | | EventMachine.run { |
|---|
| 186 | | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| 187 | | the_connection = conn |
|---|
| 188 | | end |
|---|
| 189 | | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| 190 | | EventMachine.defer proc { |
|---|
| 191 | | t = TCPSocket.new TestHost, TestPort |
|---|
| 192 | | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| 193 | | t.write "\n" |
|---|
| 194 | | t.write content |
|---|
| 195 | | t.close |
|---|
| 196 | | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| 197 | | sleep 0.1 |
|---|
| 198 | | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| 199 | | end |
|---|
| 200 | | }, proc { |
|---|
| 201 | | EventMachine.stop |
|---|
| 202 | | } |
|---|
| 203 | | } |
|---|
| 204 | | |
|---|
| 205 | | hsh = the_connection.headers_2_hash( the_connection.my_headers.shift ) |
|---|
| 206 | | assert_equal( |
|---|
| 207 | | { |
|---|
| 208 | | :accept => "aaa", |
|---|
| 209 | | :user_agent => "bbb", |
|---|
| 210 | | :host => "ccc", |
|---|
| 211 | | :x_tempest_header => "ddd" |
|---|
| 212 | | }, |
|---|
| 213 | | hsh |
|---|
| 214 | | ) |
|---|
| 215 | | end |
|---|
| 216 | | |
|---|
| 217 | | #-------------------------------------------------------------------- |
|---|
| | 212 | def test_interpret_headers |
|---|
| | 213 | Thread.abort_on_exception = true |
|---|
| | 214 | the_connection = nil |
|---|
| | 215 | content = "A" * 50 |
|---|
| | 216 | headers = [ |
|---|
| | 217 | "GET / HTTP/1.0", |
|---|
| | 218 | "Accept: aaa", |
|---|
| | 219 | "User-Agent: bbb", |
|---|
| | 220 | "Host: ccc", |
|---|
| | 221 | "x-tempest-header:ddd" |
|---|
| | 222 | ] |
|---|
| | 223 | |
|---|
| | 224 | EventMachine.run { |
|---|
| | 225 | EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn| |
|---|
| | 226 | the_connection = conn |
|---|
| | 227 | end |
|---|
| | 228 | EventMachine.add_timer(4) {raise "test timed out"} |
|---|
| | 229 | |
|---|
| | 230 | pr = proc { |
|---|
| | 231 | t = TCPSocket.new TestHost, TestPort |
|---|
| | 232 | headers.each {|h| t.write "#{h}\r\n" } |
|---|
| | 233 | t.write "\n" |
|---|
| | 234 | t.write content |
|---|
| | 235 | t.close |
|---|
| | 236 | } |
|---|
| | 237 | |
|---|
| | 238 | if RUBY_PLATFORM =~ /java/i |
|---|
| | 239 | pr.call |
|---|
| | 240 | EM.add_timer(0.5) {EM.stop} |
|---|
| | 241 | else |
|---|
| | 242 | EventMachine.defer proc { |
|---|
| | 243 | pr.call |
|---|
| | 244 | if RUBY_VERSION =~ /\A1\.9\./ |
|---|
| | 245 | sleep 0.1 |
|---|
| | 246 | STDERR.puts "Introducing extraneous sleep for Ruby 1.9" |
|---|
| | 247 | end |
|---|
| | 248 | }, proc { |
|---|
| | 249 | EventMachine.stop |
|---|
| | 250 | } |
|---|
| | 251 | end |
|---|
| | 252 | } |
|---|
| | 253 | |
|---|
| | 254 | hsh = the_connection.headers_2_hash( the_connection.my_headers.shift ) |
|---|
| | 255 | assert_equal( |
|---|
| | 256 | { |
|---|
| | 257 | :accept => "aaa", |
|---|
| | 258 | :user_agent => "bbb", |
|---|
| | 259 | :host => "ccc", |
|---|
| | 260 | :x_tempest_header => "ddd" |
|---|
| | 261 | }, |
|---|
| | 262 | hsh |
|---|
| | 263 | ) |
|---|
| | 264 | end |
|---|
| | 265 | |
|---|