forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_hc.rb
190 lines (170 loc) · 5.25 KB
/
test_hc.rb
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# $Id$
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage:: http://rubyeventmachine.com
# Date:: 8 April 2006
#
# See EventMachine and EventMachine::Connection for documentation and
# usage examples.
#
#----------------------------------------------------------------------------
#
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
# Gmail: blackhedd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 1) the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version; or 2) Ruby's License.
#
# See the file COPYING for complete licensing information.
#
#---------------------------------------------------------------------------
#
#
#
require 'eventmachine'
require 'test/unit'
class TestHeaderAndContentProtocol < Test::Unit::TestCase
TestHost = "127.0.0.1"
TestPort = 8905
class SimpleTest < EventMachine::Protocols::HeaderAndContentProtocol
attr_reader :first_header, :my_headers, :request
def receive_first_header_line hdr
@first_header ||= []
@first_header << hdr
end
def receive_headers hdrs
@my_headers ||= []
@my_headers << hdrs
end
def receive_request hdrs, content
@request ||= []
@request << [hdrs, content]
end
end
class StopOnUnbind < EM::Connection
def unbind
EM.add_timer(0.1) { EM.stop }
end
end
def test_no_content
the_connection = nil
EM.run {
EM.start_server( TestHost, TestPort, SimpleTest ) do |conn|
the_connection = conn
end
setup_timeout
EM.connect TestHost, TestPort, StopOnUnbind do |c|
c.send_data [ "aaa\n", "bbb\r\n", "ccc\n", "\n" ].join
c.close_connection_after_writing
end
}
assert_equal( ["aaa"], the_connection.first_header )
assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers )
assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request )
end
def test_content
the_connection = nil
content = "A" * 50
headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
EventMachine.run {
EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
the_connection = conn
end
setup_timeout
EM.connect TestHost, TestPort, StopOnUnbind do |c|
headers.each { |h| c.send_data "#{h}\r\n" }
c.send_data "\n"
c.send_data content
c.close_connection_after_writing
end
}
assert_equal( ["aaa"], the_connection.first_header )
assert_equal( [headers], the_connection.my_headers )
assert_equal( [[headers, content]], the_connection.request )
end
def test_several_requests
the_connection = nil
content = "A" * 50
headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
EventMachine.run {
EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
the_connection = conn
end
setup_timeout
EventMachine.connect( TestHost, TestPort, StopOnUnbind ) do |c|
5.times do
headers.each { |h| c.send_data "#{h}\r\n" }
c.send_data "\n"
c.send_data content
end
c.close_connection_after_writing
end
}
assert_equal( ["aaa"] * 5, the_connection.first_header )
assert_equal( [headers] * 5, the_connection.my_headers )
assert_equal( [[headers, content]] * 5, the_connection.request )
end
# def x_test_multiple_content_length_headers
# # This is supposed to throw a RuntimeError but it throws a C++ exception instead.
# the_connection = nil
# content = "A" * 50
# headers = ["aaa", "bbb", ["Content-length: #{content.length}"]*2, "ccc"].flatten
# EventMachine.run {
# EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
# the_connection = conn
# end
# EventMachine.add_timer(4) {raise "test timed out"}
# test_proc = proc {
# t = TCPSocket.new TestHost, TestPort
# headers.each {|h| t.write "#{h}\r\n" }
# t.write "\n"
# t.write content
# t.close
# }
# EventMachine.defer test_proc, proc {
# EventMachine.stop
# }
# }
# end
def test_interpret_headers
the_connection = nil
content = "A" * 50
headers = [
"GET / HTTP/1.0",
"Accept: aaa",
"User-Agent: bbb",
"Host: ccc",
"x-tempest-header:ddd"
]
EventMachine.run {
EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
the_connection = conn
end
setup_timeout
EventMachine.connect( TestHost, TestPort, StopOnUnbind ) do |c|
headers.each { |h| c.send_data "#{h}\r\n" }
c.send_data "\n"
c.send_data content
c.close_connection_after_writing
end
}
hsh = the_connection.headers_2_hash( the_connection.my_headers.shift )
expect = {
:accept => "aaa",
:user_agent => "bbb",
:host => "ccc",
:x_tempest_header => "ddd"
}
assert_equal(expect, hsh)
end
def setup_timeout(timeout = 4)
EM.schedule {
start_time = EM.current_time
EM.add_periodic_timer(0.01) {
raise "timeout" if EM.current_time - start_time >= timeout
}
}
end
end