eventmachine-le 1.1.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/.yardopts +7 -0
- data/GNU +281 -0
- data/LICENSE +60 -0
- data/README.md +80 -0
- data/Rakefile +19 -0
- data/eventmachine-le.gemspec +42 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +841 -0
- data/ext/ed.cpp +1995 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2377 -0
- data/ext/em.h +243 -0
- data/ext/eventmachine.h +126 -0
- data/ext/extconf.rb +166 -0
- data/ext/fastfilereader/extconf.rb +94 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +347 -0
- data/ext/project.h +155 -0
- data/ext/rubymain.cpp +1269 -0
- data/ext/ssl.cpp +468 -0
- data/ext/ssl.h +94 -0
- data/lib/em/buftok.rb +110 -0
- data/lib/em/callback.rb +58 -0
- data/lib/em/channel.rb +64 -0
- data/lib/em/completion.rb +304 -0
- data/lib/em/connection.rb +728 -0
- data/lib/em/deferrable.rb +210 -0
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/file_watch.rb +73 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/iterator.rb +313 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/pool.rb +151 -0
- data/lib/em/process_watch.rb +45 -0
- data/lib/em/processes.rb +123 -0
- data/lib/em/protocols.rb +37 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +279 -0
- data/lib/em/protocols/httpclient2.rb +600 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +29 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +331 -0
- data/lib/em/protocols/object_protocol.rb +46 -0
- data/lib/em/protocols/postgres3.rb +246 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +365 -0
- data/lib/em/protocols/smtpserver.rb +663 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +202 -0
- data/lib/em/protocols/tcptest.rb +54 -0
- data/lib/em/queue.rb +71 -0
- data/lib/em/resolver.rb +195 -0
- data/lib/em/spawnable.rb +84 -0
- data/lib/em/streamer.rb +118 -0
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +106 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine-le.rb +10 -0
- data/lib/eventmachine.rb +1548 -0
- data/rakelib/cpp.rake_example +77 -0
- data/rakelib/package.rake +98 -0
- data/rakelib/test.rake +8 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/em_test_helper.rb +143 -0
- data/tests/test_attach.rb +148 -0
- data/tests/test_basic.rb +294 -0
- data/tests/test_channel.rb +62 -0
- data/tests/test_completion.rb +177 -0
- data/tests/test_connection_count.rb +33 -0
- data/tests/test_defer.rb +18 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +134 -0
- data/tests/test_error_handler.rb +38 -0
- data/tests/test_exc.rb +28 -0
- data/tests/test_file_watch.rb +65 -0
- data/tests/test_futures.rb +170 -0
- data/tests/test_get_sock_opt.rb +37 -0
- data/tests/test_handler_check.rb +35 -0
- data/tests/test_hc.rb +155 -0
- data/tests/test_httpclient.rb +190 -0
- data/tests/test_httpclient2.rb +128 -0
- data/tests/test_inactivity_timeout.rb +54 -0
- data/tests/test_ipv4.rb +125 -0
- data/tests/test_ipv6.rb +131 -0
- data/tests/test_iterator.rb +110 -0
- data/tests/test_kb.rb +34 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_ltp.rb +138 -0
- data/tests/test_ltp2.rb +288 -0
- data/tests/test_next_tick.rb +104 -0
- data/tests/test_object_protocol.rb +36 -0
- data/tests/test_pause.rb +78 -0
- data/tests/test_pending_connect_timeout.rb +52 -0
- data/tests/test_pool.rb +196 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +133 -0
- data/tests/test_proxy_connection.rb +168 -0
- data/tests/test_pure.rb +88 -0
- data/tests/test_queue.rb +50 -0
- data/tests/test_resolver.rb +55 -0
- data/tests/test_running.rb +14 -0
- data/tests/test_sasl.rb +47 -0
- data/tests/test_send_file.rb +217 -0
- data/tests/test_servers.rb +33 -0
- data/tests/test_set_sock_opt.rb +41 -0
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +55 -0
- data/tests/test_smtpserver.rb +120 -0
- data/tests/test_spawn.rb +293 -0
- data/tests/test_ssl_args.rb +78 -0
- data/tests/test_ssl_methods.rb +48 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_threaded_resource.rb +55 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +180 -0
- data/tests/test_ud.rb +8 -0
- data/tests/test_udp46.rb +53 -0
- data/tests/test_unbind_reason.rb +48 -0
- metadata +390 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
class TestGetSockOpt < Test::Unit::TestCase
|
5
|
+
|
6
|
+
if EM.respond_to? :get_sock_opt
|
7
|
+
def setup
|
8
|
+
assert(!EM.reactor_running?)
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
assert(!EM.reactor_running?)
|
13
|
+
end
|
14
|
+
|
15
|
+
#-------------------------------------
|
16
|
+
|
17
|
+
def test_get_sock_opt
|
18
|
+
test = self
|
19
|
+
EM.run do
|
20
|
+
EM.connect 'google.com', 80, Module.new {
|
21
|
+
define_method :connection_completed do
|
22
|
+
val = get_sock_opt Socket::SOL_SOCKET, Socket::SO_ERROR
|
23
|
+
test.assert_equal "\0\0\0\0", val
|
24
|
+
EM.stop
|
25
|
+
end
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
warn "EM.get_sock_opt not implemented, skipping tests in #{__FILE__}"
|
31
|
+
|
32
|
+
# Because some rubies will complain if a TestCase class has no tests
|
33
|
+
def test_em_get_sock_opt_unsupported
|
34
|
+
assert true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestHandlerCheck < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class Foo < EM::Connection; end;
|
6
|
+
module TestModule; end;
|
7
|
+
|
8
|
+
def test_with_correct_class
|
9
|
+
assert_nothing_raised do
|
10
|
+
EM.run {
|
11
|
+
EM.connect("127.0.0.1", 80, Foo)
|
12
|
+
EM.stop_event_loop
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_with_incorrect_class
|
18
|
+
assert_raise(ArgumentError) do
|
19
|
+
EM.run {
|
20
|
+
EM.connect("127.0.0.1", 80, String)
|
21
|
+
EM.stop_event_loop
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_with_module
|
27
|
+
assert_nothing_raised do
|
28
|
+
EM.run {
|
29
|
+
EM.connect("127.0.0.1", 80, TestModule)
|
30
|
+
EM.stop_event_loop
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/tests/test_hc.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestHeaderAndContentProtocol < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class SimpleTest < EM::P::HeaderAndContentProtocol
|
6
|
+
attr_reader :first_header, :my_headers, :request
|
7
|
+
|
8
|
+
def receive_first_header_line hdr
|
9
|
+
@first_header ||= []
|
10
|
+
@first_header << hdr
|
11
|
+
end
|
12
|
+
def receive_headers hdrs
|
13
|
+
@my_headers ||= []
|
14
|
+
@my_headers << hdrs
|
15
|
+
end
|
16
|
+
def receive_request hdrs, content
|
17
|
+
@request ||= []
|
18
|
+
@request << [hdrs, content]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class StopOnUnbind < EM::Connection
|
23
|
+
def unbind
|
24
|
+
EM.add_timer(0.01) { EM.stop }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup
|
29
|
+
@port = next_port
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_no_content
|
33
|
+
the_connection = nil
|
34
|
+
EM.run {
|
35
|
+
EM.start_server( "127.0.0.1", @port, SimpleTest ) do |conn|
|
36
|
+
the_connection = conn
|
37
|
+
end
|
38
|
+
setup_timeout
|
39
|
+
|
40
|
+
EM.connect "127.0.0.1", @port, StopOnUnbind do |c|
|
41
|
+
c.send_data [ "aaa\n", "bbb\r\n", "ccc\n", "\n" ].join
|
42
|
+
c.close_connection_after_writing
|
43
|
+
end
|
44
|
+
}
|
45
|
+
assert_equal( ["aaa"], the_connection.first_header )
|
46
|
+
assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers )
|
47
|
+
assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request )
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_content
|
51
|
+
the_connection = nil
|
52
|
+
content = "A" * 50
|
53
|
+
headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
|
54
|
+
EM.run {
|
55
|
+
EM.start_server( "127.0.0.1", @port, SimpleTest ) do |conn|
|
56
|
+
the_connection = conn
|
57
|
+
end
|
58
|
+
setup_timeout
|
59
|
+
|
60
|
+
EM.connect "127.0.0.1", @port, StopOnUnbind do |c|
|
61
|
+
headers.each { |h| c.send_data "#{h}\r\n" }
|
62
|
+
c.send_data "\n"
|
63
|
+
c.send_data content
|
64
|
+
c.close_connection_after_writing
|
65
|
+
end
|
66
|
+
}
|
67
|
+
assert_equal( ["aaa"], the_connection.first_header )
|
68
|
+
assert_equal( [headers], the_connection.my_headers )
|
69
|
+
assert_equal( [[headers, content]], the_connection.request )
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_several_requests
|
73
|
+
the_connection = nil
|
74
|
+
content = "A" * 50
|
75
|
+
headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
|
76
|
+
EM.run {
|
77
|
+
EM.start_server( "127.0.0.1", @port, SimpleTest ) do |conn|
|
78
|
+
the_connection = conn
|
79
|
+
end
|
80
|
+
setup_timeout
|
81
|
+
|
82
|
+
EM.connect( "127.0.0.1", @port, StopOnUnbind ) do |c|
|
83
|
+
5.times do
|
84
|
+
headers.each { |h| c.send_data "#{h}\r\n" }
|
85
|
+
c.send_data "\n"
|
86
|
+
c.send_data content
|
87
|
+
end
|
88
|
+
c.close_connection_after_writing
|
89
|
+
end
|
90
|
+
}
|
91
|
+
assert_equal( ["aaa"] * 5, the_connection.first_header )
|
92
|
+
assert_equal( [headers] * 5, the_connection.my_headers )
|
93
|
+
assert_equal( [[headers, content]] * 5, the_connection.request )
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
# def x_test_multiple_content_length_headers
|
98
|
+
# # This is supposed to throw a RuntimeError but it throws a C++ exception instead.
|
99
|
+
# the_connection = nil
|
100
|
+
# content = "A" * 50
|
101
|
+
# headers = ["aaa", "bbb", ["Content-length: #{content.length}"]*2, "ccc"].flatten
|
102
|
+
# EM.run {
|
103
|
+
# EM.start_server( "127.0.0.1", @port, SimpleTest ) do |conn|
|
104
|
+
# the_connection = conn
|
105
|
+
# end
|
106
|
+
# EM.add_timer(4) {raise "test timed out"}
|
107
|
+
# test_proc = proc {
|
108
|
+
# t = TCPSocket.new "127.0.0.1", @port
|
109
|
+
# headers.each {|h| t.write "#{h}\r\n" }
|
110
|
+
# t.write "\n"
|
111
|
+
# t.write content
|
112
|
+
# t.close
|
113
|
+
# }
|
114
|
+
# EM.defer test_proc, proc {
|
115
|
+
# EM.stop
|
116
|
+
# }
|
117
|
+
# }
|
118
|
+
# end
|
119
|
+
|
120
|
+
def test_interpret_headers
|
121
|
+
the_connection = nil
|
122
|
+
content = "A" * 50
|
123
|
+
headers = [
|
124
|
+
"GET / HTTP/1.0",
|
125
|
+
"Accept: aaa",
|
126
|
+
"User-Agent: bbb",
|
127
|
+
"Host: ccc",
|
128
|
+
"x-tempest-header:ddd"
|
129
|
+
]
|
130
|
+
|
131
|
+
EM.run {
|
132
|
+
EM.start_server( "127.0.0.1", @port, SimpleTest ) do |conn|
|
133
|
+
the_connection = conn
|
134
|
+
end
|
135
|
+
setup_timeout
|
136
|
+
|
137
|
+
EM.connect( "127.0.0.1", @port, StopOnUnbind ) do |c|
|
138
|
+
headers.each { |h| c.send_data "#{h}\r\n" }
|
139
|
+
c.send_data "\n"
|
140
|
+
c.send_data content
|
141
|
+
c.close_connection_after_writing
|
142
|
+
end
|
143
|
+
}
|
144
|
+
|
145
|
+
hsh = the_connection.headers_2_hash( the_connection.my_headers.shift )
|
146
|
+
expect = {
|
147
|
+
:accept => "aaa",
|
148
|
+
:user_agent => "bbb",
|
149
|
+
:host => "ccc",
|
150
|
+
:x_tempest_header => "ddd"
|
151
|
+
}
|
152
|
+
assert_equal(expect, hsh)
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestHttpClient < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Localhost = "127.0.0.1"
|
6
|
+
Localport = 9801
|
7
|
+
|
8
|
+
def setup
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
end
|
13
|
+
|
14
|
+
#-------------------------------------
|
15
|
+
|
16
|
+
def test_http_client
|
17
|
+
ok = false
|
18
|
+
EM.run {
|
19
|
+
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
|
20
|
+
c.callback {
|
21
|
+
ok = true
|
22
|
+
EM.stop
|
23
|
+
}
|
24
|
+
c.errback {EM.stop} # necessary, otherwise a failure blocks the test suite forever.
|
25
|
+
}
|
26
|
+
assert ok
|
27
|
+
end
|
28
|
+
|
29
|
+
#-------------------------------------
|
30
|
+
|
31
|
+
def test_http_client_1
|
32
|
+
ok = false
|
33
|
+
EM.run {
|
34
|
+
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
|
35
|
+
c.callback {ok = true; EM.stop}
|
36
|
+
c.errback {EM.stop}
|
37
|
+
}
|
38
|
+
assert ok
|
39
|
+
end
|
40
|
+
|
41
|
+
#-------------------------------------
|
42
|
+
|
43
|
+
def test_http_client_2
|
44
|
+
ok = false
|
45
|
+
EM.run {
|
46
|
+
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
|
47
|
+
c.callback {|result|
|
48
|
+
ok = true;
|
49
|
+
EM.stop
|
50
|
+
}
|
51
|
+
c.errback {EM.stop}
|
52
|
+
}
|
53
|
+
assert ok
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
#-----------------------------------------
|
58
|
+
|
59
|
+
# Test a server that returns a page with a zero content-length.
|
60
|
+
# This caused an early version of the HTTP client not to generate a response,
|
61
|
+
# causing this test to hang. Observe, there was no problem with responses
|
62
|
+
# lacking a content-length, just when the content-length was zero.
|
63
|
+
#
|
64
|
+
class EmptyContent < EM::Connection
|
65
|
+
def initialize *args
|
66
|
+
super
|
67
|
+
end
|
68
|
+
def receive_data data
|
69
|
+
send_data "HTTP/1.0 404 ...\r\nContent-length: 0\r\n\r\n"
|
70
|
+
close_connection_after_writing
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_http_empty_content
|
75
|
+
ok = false
|
76
|
+
EM.run {
|
77
|
+
EM.start_server "127.0.0.1", 9701, EmptyContent
|
78
|
+
c = silent { EM::P::HttpClient.send :request, :host => "127.0.0.1", :port => 9701 }
|
79
|
+
c.callback {|result|
|
80
|
+
ok = true
|
81
|
+
EM.stop
|
82
|
+
}
|
83
|
+
}
|
84
|
+
assert ok
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
#---------------------------------------
|
89
|
+
|
90
|
+
class PostContent < EM::P::LineAndTextProtocol
|
91
|
+
def initialize *args
|
92
|
+
super
|
93
|
+
@lines = []
|
94
|
+
end
|
95
|
+
def receive_line line
|
96
|
+
if line.length > 0
|
97
|
+
@lines << line
|
98
|
+
else
|
99
|
+
process_headers
|
100
|
+
end
|
101
|
+
end
|
102
|
+
def receive_binary_data data
|
103
|
+
@post_content = data
|
104
|
+
send_response
|
105
|
+
end
|
106
|
+
def process_headers
|
107
|
+
if @lines.first =~ /\APOST ([^\s]+) HTTP\/1.1\Z/
|
108
|
+
@uri = $1.dup
|
109
|
+
else
|
110
|
+
raise "bad request"
|
111
|
+
end
|
112
|
+
|
113
|
+
@lines.each {|line|
|
114
|
+
if line =~ /\AContent-length:\s*(\d+)\Z/i
|
115
|
+
@content_length = $1.dup.to_i
|
116
|
+
elsif line =~ /\AContent-type:\s*(\d+)\Z/i
|
117
|
+
@content_type = $1.dup
|
118
|
+
end
|
119
|
+
}
|
120
|
+
|
121
|
+
raise "invalid content length" unless @content_length
|
122
|
+
set_binary_mode @content_length
|
123
|
+
end
|
124
|
+
def send_response
|
125
|
+
send_data "HTTP/1.1 200 ...\r\nConnection: close\r\nContent-length: 10\r\nContent-type: text/html\r\n\r\n0123456789"
|
126
|
+
close_connection_after_writing
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# TODO, this is WRONG. The handler is asserting an HTTP 1.1 request, but the client
|
131
|
+
# is sending a 1.0 request. Gotta fix the client
|
132
|
+
def test_post
|
133
|
+
response = nil
|
134
|
+
EM.run {
|
135
|
+
EM.start_server Localhost, Localport, PostContent
|
136
|
+
setup_timeout(2)
|
137
|
+
c = silent { EM::P::HttpClient.request(
|
138
|
+
:host=>Localhost,
|
139
|
+
:port=>Localport,
|
140
|
+
:method=>:post,
|
141
|
+
:request=>"/aaa",
|
142
|
+
:content=>"XYZ",
|
143
|
+
:content_type=>"text/plain"
|
144
|
+
)}
|
145
|
+
c.callback {|r|
|
146
|
+
response = r
|
147
|
+
EM.stop
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
assert_equal( 200, response[:status] )
|
152
|
+
assert_equal( "0123456789", response[:content] )
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# TODO, need a more intelligent cookie tester.
|
157
|
+
# In fact, this whole test-harness needs a beefier server implementation.
|
158
|
+
def test_cookie
|
159
|
+
ok = false
|
160
|
+
EM.run {
|
161
|
+
c = silent { EM::Protocols::HttpClient.send :request, :host => "www.google.com", :port => 80, :cookie=>"aaa=bbb" }
|
162
|
+
c.callback {|result|
|
163
|
+
ok = true;
|
164
|
+
EM.stop
|
165
|
+
}
|
166
|
+
c.errback {EM.stop}
|
167
|
+
}
|
168
|
+
assert ok
|
169
|
+
end
|
170
|
+
|
171
|
+
# We can tell the client to send an HTTP/1.0 request (default is 1.1).
|
172
|
+
# This is useful for suppressing chunked responses until those are working.
|
173
|
+
def test_version_1_0
|
174
|
+
ok = false
|
175
|
+
EM.run {
|
176
|
+
c = silent { EM::P::HttpClient.request(
|
177
|
+
:host => "www.google.com",
|
178
|
+
:port => 80,
|
179
|
+
:version => "1.0"
|
180
|
+
)}
|
181
|
+
c.callback {|result|
|
182
|
+
ok = true;
|
183
|
+
EM.stop
|
184
|
+
}
|
185
|
+
c.errback {EM.stop}
|
186
|
+
}
|
187
|
+
assert ok
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestHttpClient2 < Test::Unit::TestCase
|
4
|
+
Localhost = "127.0.0.1"
|
5
|
+
Localport = 9801
|
6
|
+
|
7
|
+
def setup
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class TestServer < EM::Connection
|
15
|
+
end
|
16
|
+
|
17
|
+
# #connect returns an object which has made a connection to an HTTP server
|
18
|
+
# and exposes methods for making HTTP requests on that connection.
|
19
|
+
# #connect can take either a pair of parameters (a host and a port),
|
20
|
+
# or a single parameter which is a Hash.
|
21
|
+
#
|
22
|
+
def test_connect
|
23
|
+
EM.run {
|
24
|
+
EM.start_server Localhost, Localport, TestServer
|
25
|
+
silent do
|
26
|
+
EM::P::HttpClient2.connect Localhost, Localport
|
27
|
+
EM::P::HttpClient2.connect( :host=>Localhost, :port=>Localport )
|
28
|
+
end
|
29
|
+
EM.stop
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def test_bad_port
|
35
|
+
EM.run {
|
36
|
+
EM.start_server Localhost, Localport, TestServer
|
37
|
+
assert_raises( ArgumentError ) {
|
38
|
+
silent { EM::P::HttpClient2.connect Localhost, "xxx" }
|
39
|
+
}
|
40
|
+
EM.stop
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_bad_server
|
45
|
+
err = nil
|
46
|
+
EM.run {
|
47
|
+
http = silent { EM::P::HttpClient2.connect Localhost, 9999 }
|
48
|
+
d = http.get "/"
|
49
|
+
d.errback { err = true; d.internal_error; EM.stop }
|
50
|
+
}
|
51
|
+
assert(err)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_get
|
55
|
+
content = nil
|
56
|
+
EM.run {
|
57
|
+
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
|
58
|
+
d = http.get "/"
|
59
|
+
d.callback {
|
60
|
+
content = d.content
|
61
|
+
EM.stop
|
62
|
+
}
|
63
|
+
}
|
64
|
+
assert(content)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Not a pipelined request because we wait for one response before we request the next.
|
68
|
+
# XXX this test is broken because it sends the second request to the first connection
|
69
|
+
# XXX right before the connection closes
|
70
|
+
def _test_get_multiple
|
71
|
+
content = nil
|
72
|
+
EM.run {
|
73
|
+
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
|
74
|
+
d = http.get "/"
|
75
|
+
d.callback {
|
76
|
+
e = http.get "/"
|
77
|
+
e.callback {
|
78
|
+
content = e.content
|
79
|
+
EM.stop
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
assert(content)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_get_pipeline
|
87
|
+
headers, headers2 = nil, nil
|
88
|
+
EM.run {
|
89
|
+
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
|
90
|
+
d = http.get("/")
|
91
|
+
d.callback {
|
92
|
+
headers = d.headers
|
93
|
+
}
|
94
|
+
e = http.get("/")
|
95
|
+
e.callback {
|
96
|
+
headers2 = e.headers
|
97
|
+
}
|
98
|
+
EM.tick_loop { EM.stop if headers && headers2 }
|
99
|
+
EM.add_timer(1) { EM.stop }
|
100
|
+
}
|
101
|
+
assert(headers)
|
102
|
+
assert(headers2)
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def test_authheader
|
107
|
+
EM.run {
|
108
|
+
EM.start_server Localhost, Localport, TestServer
|
109
|
+
http = silent { EM::P::HttpClient2.connect Localhost, 18842 }
|
110
|
+
d = http.get :url=>"/", :authorization=>"Basic xxx"
|
111
|
+
d.callback {EM.stop}
|
112
|
+
d.errback {EM.stop}
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_https_get
|
117
|
+
d = nil
|
118
|
+
EM.run {
|
119
|
+
http = silent { EM::P::HttpClient2.connect :host => 'www.apple.com', :port => 443, :ssl => true }
|
120
|
+
d = http.get "/"
|
121
|
+
d.callback {
|
122
|
+
EM.stop
|
123
|
+
}
|
124
|
+
}
|
125
|
+
assert_equal(200, d.status)
|
126
|
+
end if EM.ssl?
|
127
|
+
|
128
|
+
end
|