require 'test/unit' $:.unshift File.join(File.dirname(__FILE__),'..','..','lib') require 'evdispatch' class TestRequests < Test::Unit::TestCase def setup @loop = Evdispatch::Loop.new @loop.start end def teardown @loop.stop end def loop @loop end def test_response id = loop.request_http("http://127.0.0.1:4044/bytes/10") response = loop.response( id ) assert_equal("http://127.0.0.1:4044/bytes/10", response[:name]) assert_equal("CCCCCCCCCC", response[:body]) assert_match(/Content-Type: text\/json/, response[:header]) assert_match(/HTTP\/1.1 200 OK/, response[:header]) assert_match(/Content-Length: 10/, response[:header]) assert(response.keys.include?(:response_time)) assert(response.keys.include?(:id)) end def test_options_request id = loop.request("http://127.0.0.1:4044/redir/1", :followlocation => 1, :referer => 'pizza') response = loop.response( id ) assert_not_nil( response ) assert_match(/ 302 Moved Temporarily/,response[:header]) assert_match(/ 200 OK/,response[:header]) end def test_redir_with_stream stream = loop.request("http://127.0.0.1:4044/redir/2", :followlocation => 1, :stream => true ) res = stream.read until res == 0 res = stream.read end assert_equal( 0, res ) assert_equal( "C"*10, stream.body ) headers = stream.headers assert_match( "Content-Length: 10", headers ) assert_match( "Content-Type: text/json", headers ) assert_match( "HTTP/1.1 200 OK", headers ) end def test_post id = loop.request("http://127.0.0.1:4044/test_post_length", :post => "hello there world") res = loop.blocking_response_for( id ) assert_not_nil res end def test_streaming count = 10000 stream = loop.request("http://127.0.0.1:4044/bytes/#{count}", :stream => true ) res = stream.read until res == 0 res = stream.read end assert_equal( 0, res ) assert_equal( "C"*count, stream.body ) headers = stream.headers assert_match( "Content-Length: #{count}", headers ) assert_match( "Content-Type: text/json", headers ) assert_match( "HTTP/1.1 200 OK", headers ) end end # not bothering to cleanup