Skip to content

Instantly share code, notes, and snippets.

@mariash
Last active August 29, 2015 14:04
Show Gist options
  • Save mariash/5d9f69d638384cfb2839 to your computer and use it in GitHub Desktop.
Save mariash/5d9f69d638384cfb2839 to your computer and use it in GitHub Desktop.
HTTPClient tests
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
7.91 tests/s, 7.91 assertions/s
mariash@mariash-mac:~/workspace/test_httpclient $ ruby main.rb -v
Loaded suite main
Started
TestHTTPClient:
test_get_chunked_file: [2014-07-29 22:11:37] INFO WEBrick 1.3.1
[2014-07-29 22:11:37] INFO ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
[2014-07-29 22:11:37] INFO WEBrick::HTTPServer#start: pid=78989 port=8080
localhost - - [29/Jul/2014:22:11:37 PDT] "GET /large HTTP/1.1" 200 12582912
- -> /large
[2014-07-29 22:11:37] INFO going to shutdown ...
.: (0.120961)
test_get_chunked_string: [2014-07-29 22:11:37] INFO WEBrick 1.3.1
[2014-07-29 22:11:37] INFO ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
[2014-07-29 22:11:37] INFO WEBrick::HTTPServer#start: pid=78989 port=8080
localhost - - [29/Jul/2014:22:11:37 PDT] "GET /large HTTP/1.1" 200 12582912
- -> /large
[2014-07-29 22:11:37] INFO going to shutdown ...
.: (0.074404)
Finished in 0.195843 seconds.
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
10.21 tests/s, 10.21 assertions/s
require 'httpclient'
require 'webrick'
require 'test/unit'
require 'stringio'
class TestHTTPClient < Test::Unit::TestCase
def setup
super
@server = WEBrick::HTTPServer.new(
:BindAddress => "localhost",
:Port => 8080
)
@server.mount_proc '/large' do |req, res|
res['content-type'] = 'text/html'
res.body = "привет" * 1024 * 1024 # 200 Mb
end
server_thread = Thread.new {
Thread.current.abort_on_exception = true
@server.start
}
# Wait to start
while @server.status != :Running
Thread.pass
unless server_thread.alive?
server_thread.join
raise
end
end
end
def teardown
super
@server.shutdown
end
def test_get_chunked_string
client = HTTPClient.new
data = StringIO.new
client.get('http://localhost:8080/large') do |chunk|
data << chunk
end
assert_equal(12 * 1024 * 1024, data.length)
end
def test_get_chunked_file
client = HTTPClient.new
File.open('/tmp/test', 'wb') do |file|
client.get('http://localhost:8080/large') do |chunk|
file.write(chunk)
end
end
File.open('/tmp/test', 'rb') do |file|
assert_equal(12 * 1024 * 1024, file.read.length)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment