Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove most unit test dependencies on third-party sites #749

Merged
merged 15 commits into from Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -9,7 +9,7 @@ language: ruby
sudo: false
dist: trusty
rvm:
- 2.3.2
- 2.3
- 2.2
- 2.1
- 2.0.0
Expand Down
1 change: 1 addition & 0 deletions lib/em/protocols/httpclient.rb
Expand Up @@ -62,6 +62,7 @@ class HttpClient < Connection

def initialize
warn "HttpClient is deprecated and will be removed. EM-Http-Request should be used instead."
@connected = false
end

# @param args [Hash] The request arguments
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic.rb
Expand Up @@ -146,7 +146,7 @@ def test_byte_range_send
def test_bind_connect
pend('FIXME: this test is broken on Windows') if windows?

local_ip = UDPSocket.open {|s| s.connect('google.com', 80); s.addr.last }
local_ip = UDPSocket.open {|s| s.connect('localhost', 80); s.addr.last }

bind_port = next_port

Expand Down
37 changes: 0 additions & 37 deletions tests/test_get_sock_opt.rb

This file was deleted.

61 changes: 33 additions & 28 deletions tests/test_httpclient.rb
Expand Up @@ -2,13 +2,8 @@

class TestHttpClient < Test::Unit::TestCase

Localhost = "127.0.0.1"
Localport = 9801

def setup
end

def teardown
@port = next_port
end

#-------------------------------------
Expand All @@ -19,6 +14,7 @@ def test_http_client
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
c.callback {
ok = true
c.close_connection
EM.stop
}
c.errback {EM.stop} # necessary, otherwise a failure blocks the test suite forever.
Expand All @@ -32,7 +28,11 @@ def test_http_client_1
ok = false
EM.run {
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
c.callback {ok = true; EM.stop}
c.callback {
ok = true
c.close_connection
EM.stop
}
c.errback {EM.stop}
}
assert ok
Expand All @@ -44,8 +44,9 @@ def test_http_client_2
ok = false
EM.run {
c = silent { EM::P::HttpClient.send :request, :host => "www.google.com", :port => 80 }
c.callback {|result|
ok = true;
c.callback {
ok = true
c.close_connection
EM.stop
}
c.errback {EM.stop}
Expand Down Expand Up @@ -74,10 +75,11 @@ def receive_data data
def test_http_empty_content
ok = false
EM.run {
EM.start_server "127.0.0.1", 9701, EmptyContent
c = silent { EM::P::HttpClient.send :request, :host => "127.0.0.1", :port => 9701 }
c.callback {|result|
EM.start_server "127.0.0.1", @port, EmptyContent
c = silent { EM::P::HttpClient.send :request, :host => "127.0.0.1", :port => @port }
c.callback {
ok = true
c.close_connection
EM.stop
}
}
Expand Down Expand Up @@ -132,15 +134,15 @@ def send_response
def test_post
response = nil
EM.run {
EM.start_server Localhost, Localport, PostContent
EM.start_server '127.0.0.1', @port, PostContent
setup_timeout(2)
c = silent { EM::P::HttpClient.request(
:host=>Localhost,
:port=>Localport,
:method=>:post,
:request=>"/aaa",
:content=>"XYZ",
:content_type=>"text/plain"
:host => '127.0.0.1',
:port => @port,
:method => :post,
:request => "/aaa",
:content => "XYZ",
:content_type => "text/plain"
)}
c.callback {|r|
response = r
Expand All @@ -159,8 +161,9 @@ def test_cookie
ok = false
EM.run {
c = silent { EM::Protocols::HttpClient.send :request, :host => "www.google.com", :port => 80, :cookie=>"aaa=bbb" }
c.callback {|result|
ok = true;
c.callback {
ok = true
c.close_connection
EM.stop
}
c.errback {EM.stop}
Expand All @@ -178,8 +181,9 @@ def test_version_1_0
:port => 80,
:version => "1.0"
)}
c.callback {|result|
ok = true;
c.callback {
ok = true
c.close_connection
EM.stop
}
c.errback {EM.stop}
Expand Down Expand Up @@ -215,14 +219,15 @@ def receive_data data

def test_http_chunked_encoding_content
ok = false
EventMachine.run {
EventMachine.start_server "127.0.0.1", 9701, ChunkedEncodingContent
c = EventMachine::Protocols::HttpClient.send :request, :host => "127.0.0.1", :port => 9701
c.callback {|result|
EM.run {
EM.start_server "127.0.0.1", @port, ChunkedEncodingContent
c = silent { EM::P::HttpClient.send :request, :host => "127.0.0.1", :port => @port }
c.callback { |result|
if result[:content] == "chunk1" * 1024 + "chunk2" * 15
ok = true
end
EventMachine.stop
c.close_connection
EM.stop
}
}
assert ok
Expand Down
52 changes: 26 additions & 26 deletions tests/test_httpclient2.rb
@@ -1,17 +1,11 @@
require 'em_test_helper'

class TestHttpClient2 < Test::Unit::TestCase
Localhost = "127.0.0.1"
Localport = 9801

def setup
end

def teardown
class TestServer < EM::Connection
end


class TestServer < EM::Connection
def setup
@port = next_port
end

# #connect returns an object which has made a connection to an HTTP server
Expand All @@ -21,21 +15,22 @@ class TestServer < EM::Connection
#
def test_connect
EM.run {
EM.start_server Localhost, Localport, TestServer
setup_timeout(1)
EM.start_server '127.0.0.1', @port, TestServer
silent do
EM::P::HttpClient2.connect Localhost, Localport
EM::P::HttpClient2.connect( :host=>Localhost, :port=>Localport )
EM::P::HttpClient2.connect '127.0.0.1', @port
EM::P::HttpClient2.connect( :host=>'127.0.0.1', :port=>@port )
end
EM.stop
}
end


def test_bad_port
EM.run {
EM.start_server Localhost, Localport, TestServer
setup_timeout(1)
EM.start_server '127.0.0.1', @port, TestServer
assert_raises( ArgumentError ) {
silent { EM::P::HttpClient2.connect Localhost, "xxx" }
silent { EM::P::HttpClient2.connect '127.0.0.1', "xxx" }
}
EM.stop
}
Expand All @@ -44,7 +39,8 @@ def test_bad_port
def test_bad_server
err = nil
EM.run {
http = silent { EM::P::HttpClient2.connect Localhost, 9999 }
setup_timeout(1)
http = silent { EM::P::HttpClient2.connect '127.0.0.1', 9999 }
d = http.get "/"
d.errback { err = true; d.internal_error; EM.stop }
}
Expand All @@ -54,7 +50,8 @@ def test_bad_server
def test_get
content = nil
EM.run {
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
setup_timeout(1)
http = silent { EM::P::HttpClient2.connect :host => "google.com", :port => 80, :version => '1.0' }
d = http.get "/"
d.callback {
content = d.content
Expand All @@ -70,7 +67,8 @@ def test_get
def _test_get_multiple
content = nil
EM.run {
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
setup_timeout(1)
http = silent { EM::P::HttpClient2.connect "google.com", :version => '1.0' }
d = http.get "/"
d.callback {
e = http.get "/"
Expand All @@ -86,6 +84,7 @@ def _test_get_multiple
def test_get_pipeline
headers, headers2 = nil, nil
EM.run {
setup_timeout(1)
http = silent { EM::P::HttpClient2.connect "google.com", 80 }
d = http.get("/")
d.callback {
Expand All @@ -102,27 +101,28 @@ def test_get_pipeline
assert(headers2)
end


def test_authheader
EM.run {
EM.start_server Localhost, Localport, TestServer
http = silent { EM::P::HttpClient2.connect Localhost, 18842 }
setup_timeout(1)
EM.start_server '127.0.0.1', @port, TestServer
http = silent { EM::P::HttpClient2.connect '127.0.0.1', 18842 }
d = http.get :url=>"/", :authorization=>"Basic xxx"
d.callback {EM.stop}
d.errback {EM.stop}
}
end

def test_https_get
omit_unless(EM.ssl?)
d = nil
EM.run {
http = silent { EM::P::HttpClient2.connect :host => 'www.apple.com', :port => 443, :ssl => true }
setup_timeout(1)
http = silent { EM::P::HttpClient2.connect :host => 'www.google.com', :port => 443, :ssl => true, :version => '1.0' }
d = http.get "/"
d.callback {
EM.stop
}
d.callback {EM.stop}
d.errback {EM.stop}
}
assert_equal(200, d.status)
end if EM.ssl?
end

end
42 changes: 24 additions & 18 deletions tests/test_idle_connection.rb
@@ -1,25 +1,31 @@
require 'em_test_helper'

class TestIdleConnection < Test::Unit::TestCase
if EM.respond_to?(:get_idle_time)
def test_idle_time
EM.run{
conn = EM.connect 'www.google.com', 80
EM.add_timer(3){
$idle_time = conn.get_idle_time
conn.send_data "GET / HTTP/1.0\r\n\r\n"
EM.next_tick{
EM.next_tick{
$idle_time_after_send = conn.get_idle_time
conn.close_connection
EM.stop
}
}
}
}
def setup
@port = next_port
end

def test_idle_time
omit_if(!EM.respond_to?(:get_idle_time))

assert_in_delta 3, $idle_time, 0.2
assert_in_delta 0, $idle_time_after_send, 0.1
a, b = nil, nil
EM.run do
EM.start_server '127.0.0.1', @port, Module.new
conn = EM.connect '127.0.0.1', @port
EM.add_timer(0.3) do
a = conn.get_idle_time
conn.send_data 'a'
EM.next_tick do
EM.next_tick do
b = conn.get_idle_time
conn.close_connection
EM.stop
end
end
end
end

assert_in_delta 0.3, a, 0.1
assert_in_delta 0, b, 0.1
end
end