How to use client method of Http Package

Best Selenium code snippet using Http.client

test_httpclient.rb

Source:test_httpclient.rb Github

copy

Full Screen

...4 include Helper5 def setup6 super7 setup_server8 setup_client9 end10 def teardown11 super12 end13 def test_initialize14 setup_proxyserver15 escape_noproxy do16 @proxyio.string = ""17 @client = HTTPClient.new(proxyurl)18 assert_equal(URI.parse(proxyurl), @client.proxy)19 assert_equal(200, @client.head(serverurl).status)20 assert(/accept/ =~ @proxyio.string)21 end22 end23 def test_agent_name24 @client = HTTPClient.new(nil, "agent_name_foo")25 str = ""26 @client.debug_dev = str27 @client.get(serverurl)28 lines = str.split(/(?:\r?\n)+/)29 assert_equal("= Request", lines[0])30 assert_match(/^User-Agent: agent_name_foo \(#{HTTPClient::VERSION}/, lines[4])31 end32 def test_from33 @client = HTTPClient.new(nil, nil, "from_bar")34 str = ""35 @client.debug_dev = str36 @client.get(serverurl)37 lines = str.split(/(?:\r?\n)+/)38 assert_equal("= Request", lines[0])39 assert_match(/^From: from_bar/, lines[4])40 end41 def test_debug_dev42 str = ""43 @client.debug_dev = str44 assert_equal(str.object_id, @client.debug_dev.object_id)45 assert(str.empty?)46 @client.get(serverurl)47 assert(!str.empty?)48 end49 def test_debug_dev_stream50 str = ""51 @client.debug_dev = str52 conn = @client.get_async(serverurl)53 Thread.pass while !conn.finished?54 assert(!str.empty?)55 end56 def test_protocol_version_http0957 @client.protocol_version = 'HTTP/0.9'58 @client.debug_dev = str = ''59 @client.test_loopback_http_response << "hello\nworld\n"60 res = @client.get(serverurl + 'hello')61 assert_equal('0.9', res.http_version)62 assert_equal(nil, res.status)63 assert_equal(nil, res.reason)64 assert_equal("hello\nworld\n", res.content)65 lines = str.split(/(?:\r?\n)+/)66 assert_equal("= Request", lines[0])67 assert_equal("! CONNECTION ESTABLISHED", lines[2])68 assert_equal("GET /hello HTTP/0.9", lines[3])69 assert_equal("Connection: close", lines[6])70 assert_equal("= Response", lines[7])71 assert_match(/^hello$/, lines[8])72 assert_match(/^world$/, lines[9])73 end74 def test_protocol_version_http1075 assert_equal(nil, @client.protocol_version)76 @client.protocol_version = 'HTTP/1.0'77 assert_equal('HTTP/1.0', @client.protocol_version)78 str = ""79 @client.debug_dev = str80 @client.get(serverurl + 'hello')81 lines = str.split(/(?:\r?\n)+/)82 assert_equal("= Request", lines[0])83 assert_equal("! CONNECTION ESTABLISHED", lines[2])84 assert_equal("GET /hello HTTP/1.0", lines[3])85 assert_equal("Connection: close", lines[6])86 assert_equal("= Response", lines[7])87 end88 def test_header_accept_by_default89 str = ""90 @client.debug_dev = str91 @client.get(serverurl)92 lines = str.split(/(?:\r?\n)+/)93 assert_equal("Accept: */*", lines[4])94 end95 def test_header_accept96 str = ""97 @client.debug_dev = str98 @client.get(serverurl, :header => {:Accept => 'text/html'})99 lines = str.split(/(?:\r?\n)+/)100 assert_equal("Accept: text/html", lines[4])101 end102 def test_host_given103 str = ""104 @client.debug_dev = str105 @client.get(serverurl)106 lines = str.split(/(?:\r?\n)+/)107 assert_equal("= Request", lines[0])108 assert_equal("! CONNECTION ESTABLISHED", lines[2])109 assert_equal("GET / HTTP/1.1", lines[3])110 assert_equal("Host: localhost:#{serverport}", lines[6])111 #112 @client.reset_all113 str = ""114 @client.debug_dev = str115 @client.get(serverurl, nil, {'Host' => 'foo'})116 lines = str.split(/(?:\r?\n)+/)117 assert_equal("= Request", lines[0])118 assert_equal("! CONNECTION ESTABLISHED", lines[2])119 assert_equal("GET / HTTP/1.1", lines[3])120 assert_equal("Host: foo", lines[4]) # use given param121 end122 def test_protocol_version_http11123 assert_equal(nil, @client.protocol_version)124 str = ""125 @client.debug_dev = str126 @client.get(serverurl)127 lines = str.split(/(?:\r?\n)+/)128 assert_equal("= Request", lines[0])129 assert_equal("! CONNECTION ESTABLISHED", lines[2])130 assert_equal("GET / HTTP/1.1", lines[3])131 assert_equal("Host: localhost:#{serverport}", lines[6])132 @client.protocol_version = 'HTTP/1.1'133 assert_equal('HTTP/1.1', @client.protocol_version)134 str = ""135 @client.debug_dev = str136 @client.get(serverurl)137 lines = str.split(/(?:\r?\n)+/)138 assert_equal("= Request", lines[0])139 assert_equal("! CONNECTION ESTABLISHED", lines[2])140 assert_equal("GET / HTTP/1.1", lines[3])141 @client.protocol_version = 'HTTP/1.0'142 str = ""143 @client.debug_dev = str144 @client.get(serverurl)145 lines = str.split(/(?:\r?\n)+/)146 assert_equal("= Request", lines[0])147 assert_equal("! CONNECTION ESTABLISHED", lines[2])148 assert_equal("GET / HTTP/1.0", lines[3])149 end150 def test_proxy151 setup_proxyserver152 escape_noproxy do153 assert_raises(URI::InvalidURIError) do154 @client.proxy = "http://"155 end156 @client.proxy = ""157 assert_nil(@client.proxy)158 @client.proxy = "http://admin:admin@foo:1234"159 assert_equal(URI.parse("http://admin:admin@foo:1234"), @client.proxy)160 uri = URI.parse("http://bar:2345")161 @client.proxy = uri162 assert_equal(uri, @client.proxy)163 #164 @proxyio.string = ""165 @client.proxy = nil166 assert_equal(200, @client.head(serverurl).status)167 assert(/accept/ !~ @proxyio.string)168 #169 @proxyio.string = ""170 @client.proxy = proxyurl171 @client.debug_dev = str = ""172 assert_equal(200, @client.head(serverurl).status)173 assert(/accept/ =~ @proxyio.string)174 assert(/Host: localhost:#{serverport}/ =~ str)175 end176 end177 def test_host_header178 @client.proxy = proxyurl179 @client.debug_dev = str = ""180 @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"181 assert_equal(200, @client.head('http://www.example.com/foo').status)182 # ensure no ':80' is added. some servers dislike that.183 assert(/\r\nHost: www\.example\.com\r\n/ =~ str)184 #185 @client.debug_dev = str = ""186 @client.test_loopback_http_response << "HTTP/1.0 200 OK\r\n\r\n"187 assert_equal(200, @client.head('http://www.example.com:12345/foo').status)188 # ensure ':12345' exists.189 assert(/\r\nHost: www\.example\.com:12345\r\n/ =~ str)190 end191 def test_proxy_env192 setup_proxyserver193 escape_env do194 ENV['http_proxy'] = "http://admin:admin@foo:1234"195 ENV['NO_PROXY'] = "foobar"196 client = HTTPClient.new197 assert_equal(URI.parse("http://admin:admin@foo:1234"), client.proxy)198 assert_equal('foobar', client.no_proxy)199 end200 end201 def test_proxy_env_cgi202 setup_proxyserver203 escape_env do204 ENV['REQUEST_METHOD'] = 'GET' # CGI environment emulation205 ENV['http_proxy'] = "http://admin:admin@foo:1234"206 ENV['no_proxy'] = "foobar"207 client = HTTPClient.new208 assert_equal(nil, client.proxy)209 ENV['CGI_HTTP_PROXY'] = "http://admin:admin@foo:1234"210 client = HTTPClient.new211 assert_equal(URI.parse("http://admin:admin@foo:1234"), client.proxy)212 end213 end214 def test_empty_proxy_env215 setup_proxyserver216 escape_env do217 ENV['http_proxy'] = ""218 client = HTTPClient.new219 assert_equal(nil, client.proxy)220 end221 end222 def test_noproxy_for_localhost223 @proxyio.string = ""224 @client.proxy = proxyurl225 assert_equal(200, @client.head(serverurl).status)226 assert(/accept/ !~ @proxyio.string)227 end228 def test_no_proxy229 setup_proxyserver230 escape_noproxy do231 # proxy is not set.232 assert_equal(nil, @client.no_proxy)233 @client.no_proxy = 'localhost'234 assert_equal('localhost', @client.no_proxy)235 @proxyio.string = ""236 @client.proxy = nil237 assert_equal(200, @client.head(serverurl).status)238 assert(/accept/ !~ @proxyio.string)239 #240 @proxyio.string = ""241 @client.proxy = proxyurl242 assert_equal(200, @client.head(serverurl).status)243 assert(/accept/ !~ @proxyio.string)244 #245 @client.no_proxy = 'foobar'246 @proxyio.string = ""247 @client.proxy = proxyurl248 assert_equal(200, @client.head(serverurl).status)249 assert(/accept/ =~ @proxyio.string)250 #251 @client.no_proxy = 'foobar,localhost:baz'252 @proxyio.string = ""253 @client.proxy = proxyurl254 assert_equal(200, @client.head(serverurl).status)255 assert(/accept/ !~ @proxyio.string)256 #257 @client.no_proxy = 'foobar,localhost:443'258 @proxyio.string = ""259 @client.proxy = proxyurl260 assert_equal(200, @client.head(serverurl).status)261 assert(/accept/ =~ @proxyio.string)262 #263 @client.no_proxy = "foobar,localhost:443:localhost:#{serverport},baz"264 @proxyio.string = ""265 @client.proxy = proxyurl266 assert_equal(200, @client.head(serverurl).status)267 assert(/accept/ !~ @proxyio.string)268 end269 end270 def test_cookie_update_while_authentication271 escape_noproxy do272 @client.test_loopback_http_response << <<EOS273HTTP/1.0 401\r274Date: Fri, 19 Dec 2008 11:57:29 GMT\r275Content-Type: text/plain\r276Content-Length: 0\r277WWW-Authenticate: Basic realm="hello"\r278Set-Cookie: foo=bar; path=/; domain=.example.org; expires=#{Time.at(1924873200).httpdate}\r279\r280EOS281 @client.test_loopback_http_response << <<EOS282HTTP/1.1 200 OK\r283Content-Length: 5\r284Connection: close\r285\r286hello287EOS288 @client.debug_dev = str = ''289 @client.set_auth("http://www.example.org/baz/", 'admin', 'admin')290 assert_equal('hello', @client.get('http://www.example.org/baz/foo').content)291 assert_match(/^Cookie: foo=bar/, str)292 assert_match(/^Authorization: Basic YWRtaW46YWRtaW4=/, str)293 end294 end295 def test_proxy_ssl296 escape_noproxy do297 @client.proxy = 'http://admin:admin@localhost:8080/'298 # disconnected at initial 'CONNECT' so there're 2 loopback responses299 @client.test_loopback_http_response << <<EOS300HTTP/1.0 407 Proxy Authentication Required\r301Date: Fri, 19 Dec 2008 11:57:29 GMT\r302Content-Type: text/plain\r303Content-Length: 0\r304Proxy-Authenticate: Basic realm="hello"\r305Proxy-Connection: close\r306\r307EOS308 @client.test_loopback_http_response << <<EOS309HTTP/1.0 200 Connection established\r310\r311HTTP/1.1 200 OK\r312Content-Length: 5\r313Connection: close\r314\r315hello316EOS317 assert_equal('hello', @client.get('https://localhost:17171/baz').content)318 end319 end320 def test_loopback_response321 @client.test_loopback_response << 'message body 1'322 @client.test_loopback_response << 'message body 2'323 assert_equal('message body 1', @client.get_content('http://somewhere'))324 assert_equal('message body 2', @client.get_content('http://somewhere'))325 #326 @client.debug_dev = str = ''327 @client.test_loopback_response << 'message body 3'328 assert_equal('message body 3', @client.get_content('http://somewhere'))329 assert_match(/message body 3/, str)330 end331 def test_loopback_response_stream332 @client.test_loopback_response << 'message body 1'333 @client.test_loopback_response << 'message body 2'334 conn = @client.get_async('http://somewhere')335 Thread.pass while !conn.finished?336 assert_equal('message body 1', conn.pop.content.read)337 conn = @client.get_async('http://somewhere')338 Thread.pass while !conn.finished?339 assert_equal('message body 2', conn.pop.content.read)340 end341 def test_loopback_http_response342 @client.test_loopback_http_response << "HTTP/1.0 200 OK\ncontent-length: 100\n\nmessage body 1"343 @client.test_loopback_http_response << "HTTP/1.0 200 OK\ncontent-length: 100\n\nmessage body 2"344 assert_equal('message body 1', @client.get_content('http://somewhere'))345 assert_equal('message body 2', @client.get_content('http://somewhere'))346 end347 def test_multiline_header348 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nX-Foo: XXX\n YYY\nX-Bar: \n XXX\n\tYYY\ncontent-length: 100\n\nmessage body 1"349 res = @client.get('http://somewhere')350 assert_equal('message body 1', res.content)351 assert_equal(['XXX YYY'], res.header['x-foo'])352 assert_equal(['XXX YYY'], res.header['x-bar'])353 end354 def test_broken_header355 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nXXXXX\ncontent-length: 100\n\nmessage body 1"356 res = @client.get('http://somewhere')357 assert_equal('message body 1', res.content)358 end359 def test_redirect_non_https360 url = serverurl + 'redirect1'361 https_url = URI.parse(url)362 https_url.scheme = 'https'363 #364 redirect_to_http = "HTTP/1.0 302 OK\nLocation: #{url}\n\n"365 redirect_to_https = "HTTP/1.0 302 OK\nLocation: #{https_url}\n\n"366 #367 # https -> http is denied368 @client.test_loopback_http_response << redirect_to_http369 assert_raises(HTTPClient::BadResponseError) do370 @client.get_content(https_url)371 end372 #373 # http -> http is OK374 @client.reset_all375 @client.test_loopback_http_response << redirect_to_http376 assert_equal('hello', @client.get_content(url))377 #378 # http -> https is OK379 @client.reset_all380 @client.test_loopback_http_response << redirect_to_https381 assert_raises(OpenSSL::SSL::SSLError) do382 # trying to normal endpoint with SSL -> SSL negotiation failure383 @client.get_content(url)384 end385 #386 # https -> https is OK387 @client.reset_all388 @client.test_loopback_http_response << redirect_to_https389 assert_raises(OpenSSL::SSL::SSLError) do390 # trying to normal endpoint with SSL -> SSL negotiation failure391 @client.get_content(https_url)392 end393 #394 # https -> http with strict_redirect_uri_callback395 @client.redirect_uri_callback = @client.method(:strict_redirect_uri_callback)396 @client.test_loopback_http_response << redirect_to_http397 assert_raises(HTTPClient::BadResponseError) do398 @client.get_content(https_url)399 end400 end401 def test_redirect_relative402 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: hello\n\n"403 silent do404 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))405 end406 #407 @client.reset_all408 @client.redirect_uri_callback = @client.method(:strict_redirect_uri_callback)409 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))410 @client.reset_all411 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: hello\n\n"412 begin413 @client.get_content(serverurl + 'redirect1')414 assert(false)415 rescue HTTPClient::BadResponseError => e416 assert_equal(302, e.res.status)417 end418 end419 def test_redirect_https_relative420 url = serverurl + 'redirect1'421 https_url = URI.parse(url)422 https_url.scheme = 'https'423 @client.test_loopback_http_response << "HTTP/1.0 302 OK\nLocation: /foo\n\n"424 @client.test_loopback_http_response << "HTTP/1.0 200 OK\n\nhello"425 silent do426 assert_equal('hello', @client.get_content(https_url))427 end428 end429 def test_no_content430 assert_nothing_raised do431 timeout(2) do432 @client.get(serverurl + 'status', :status => 101)433 @client.get(serverurl + 'status', :status => 204)434 @client.get(serverurl + 'status', :status => 304)435 end436 end437 end438 def test_get_content439 assert_equal('hello', @client.get_content(serverurl + 'hello'))440 assert_equal('hello', @client.get_content(serverurl + 'redirect1'))441 assert_equal('hello', @client.get_content(serverurl + 'redirect2'))442 url = serverurl.sub(/localhost/, '127.0.0.1')443 assert_equal('hello', @client.get_content(url + 'hello'))444 assert_equal('hello', @client.get_content(url + 'redirect1'))445 assert_equal('hello', @client.get_content(url + 'redirect2'))446 @client.reset(serverurl)447 @client.reset(url)448 @client.reset(serverurl)449 @client.reset(url)450 assert_raises(HTTPClient::BadResponseError) do451 @client.get_content(serverurl + 'notfound')452 end453 assert_raises(HTTPClient::BadResponseError) do454 @client.get_content(serverurl + 'redirect_self')455 end456 called = false457 @client.redirect_uri_callback = lambda { |uri, res|458 newuri = res.header['location'][0]459 called = true460 newuri461 }462 assert_equal('hello', @client.get_content(serverurl + 'relative_redirect'))463 assert(called)464 end465 GZIP_CONTENT = "\x1f\x8b\x08\x00\x1a\x96\xe0\x4c\x00\x03\xcb\x48\xcd\xc9\xc9\x07\x00\x86\xa6\x10\x36\x05\x00\x00\x00"466 DEFLATE_CONTENT = "\x78\x9c\xcb\x48\xcd\xc9\xc9\x07\x00\x06\x2c\x02\x15"467 GZIP_CONTENT.force_encoding('BINARY') if GZIP_CONTENT.respond_to?(:force_encoding)468 DEFLATE_CONTENT.force_encoding('BINARY') if DEFLATE_CONTENT.respond_to?(:force_encoding)469 def test_get_gzipped_content470 @client.transparent_gzip_decompression = false471 content = @client.get_content(serverurl + 'compressed?enc=gzip')472 assert_not_equal('hello', content)473 assert_equal(GZIP_CONTENT, content)474 @client.transparent_gzip_decompression = true475 assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=gzip'))476 assert_equal('hello', @client.get_content(serverurl + 'compressed?enc=deflate'))477 @client.transparent_gzip_decompression = false478 end479 def test_get_content_with_block480 @client.get_content(serverurl + 'hello') do |str|481 assert_equal('hello', str)482 end483 @client.get_content(serverurl + 'redirect1') do |str|484 assert_equal('hello', str)485 end486 @client.get_content(serverurl + 'redirect2') do |str|487 assert_equal('hello', str)488 end489 end490 def test_post_content491 assert_equal('hello', @client.post_content(serverurl + 'hello'))492 assert_equal('hello', @client.post_content(serverurl + 'redirect1'))493 assert_equal('hello', @client.post_content(serverurl + 'redirect2'))494 assert_raises(HTTPClient::BadResponseError) do495 @client.post_content(serverurl + 'notfound')496 end497 assert_raises(HTTPClient::BadResponseError) do498 @client.post_content(serverurl + 'redirect_self')499 end500 called = false501 @client.redirect_uri_callback = lambda { |uri, res|502 newuri = res.header['location'][0]503 called = true504 newuri505 }506 assert_equal('hello', @client.post_content(serverurl + 'relative_redirect'))507 assert(called)508 end509 def test_post_content_io510 post_body = StringIO.new("1234567890")511 assert_equal('post,1234567890', @client.post_content(serverurl + 'servlet', post_body))512 post_body = StringIO.new("1234567890")513 assert_equal('post,1234567890', @client.post_content(serverurl + 'servlet_redirect', post_body))514 #515 post_body = StringIO.new("1234567890")516 post_body.read(5)517 assert_equal('post,67890', @client.post_content(serverurl + 'servlet_redirect', post_body))518 end519 def test_head520 assert_equal("head", @client.head(serverurl + 'servlet').header["x-head"][0])521 param = {'1'=>'2', '3'=>'4'}522 res = @client.head(serverurl + 'servlet', param)523 assert_equal(param, params(res.header["x-query"][0]))524 end525 def test_head_async526 param = {'1'=>'2', '3'=>'4'}527 conn = @client.head_async(serverurl + 'servlet', param)528 Thread.pass while !conn.finished?529 res = conn.pop530 assert_equal(param, params(res.header["x-query"][0]))531 end532 def test_get533 assert_equal("get", @client.get(serverurl + 'servlet').content)534 param = {'1'=>'2', '3'=>'4'}535 res = @client.get(serverurl + 'servlet', param)536 assert_equal(param, params(res.header["x-query"][0]))537 assert_nil(res.contenttype)538 #539 url = serverurl.to_s + 'servlet?5=6&7=8'540 res = @client.get(url, param)541 assert_equal(param.merge("5"=>"6", "7"=>"8"), params(res.header["x-query"][0]))542 assert_nil(res.contenttype)543 end544 def test_get_follow_redirect545 assert_equal('hello', @client.get(serverurl + 'hello', :follow_redirect => true).body)546 assert_equal('hello', @client.get(serverurl + 'redirect1', :follow_redirect => true).body)547 assert_equal('hello', @client.get(serverurl + 'redirect2', :follow_redirect => true).body)548 end549 def test_get_async550 param = {'1'=>'2', '3'=>'4'}551 conn = @client.get_async(serverurl + 'servlet', param)552 Thread.pass while !conn.finished?553 res = conn.pop554 assert_equal(param, params(res.header["x-query"][0]))555 end556 def test_get_async_for_largebody557 conn = @client.get_async(serverurl + 'largebody')558 res = conn.pop559 assert_equal(1000*1000, res.content.read.length)560 end561 def test_get_with_block562 called = false563 res = @client.get(serverurl + 'servlet') { |str|564 assert_equal('get', str)565 called = true566 }567 assert(called)568 # res does not have a content569 assert_nil(res.content)570 end571 def test_get_with_block_chunk_string_recycle572 @client.read_block_size = 2573 body = []574 res = @client.get(serverurl + 'servlet') { |str|575 body << str576 }577 assert_equal(2, body.size)578 assert_equal("get", body.join) # Was "tt" by String object recycle...579 end580 def test_post581 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4])582 param = {'1'=>'2', '3'=>'4'}583 res = @client.post(serverurl + 'servlet', param)584 assert_equal(param, params(res.header["x-query"][0]))585 end586 def test_post_follow_redirect587 assert_equal('hello', @client.post(serverurl + 'hello', :follow_redirect => true).body)588 assert_equal('hello', @client.post(serverurl + 'redirect1', :follow_redirect => true).body)589 assert_equal('hello', @client.post(serverurl + 'redirect2', :follow_redirect => true).body)590 end591 def test_post_with_content_type592 param = [['1', '2'], ['3', '4']]593 ext = {'content-type' => 'application/x-www-form-urlencoded', 'hello' => 'world'}594 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)595 res = @client.post(serverurl + 'servlet', param, ext)596 assert_equal(Hash[param], params(res.header["x-query"][0]))597 #598 ext = [['content-type', 'multipart/form-data'], ['hello', 'world']]599 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)600 res = @client.post(serverurl + 'servlet', param, ext)601 assert_match(/Content-Disposition: form-data; name="1"/, res.content)602 assert_match(/Content-Disposition: form-data; name="3"/, res.content)603 #604 ext = {'content-type' => 'multipart/form-data; boundary=hello'}605 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)606 res = @client.post(serverurl + 'servlet', param, ext)607 assert_match(/Content-Disposition: form-data; name="1"/, res.content)608 assert_match(/Content-Disposition: form-data; name="3"/, res.content)609 assert_equal("post,--hello\r\nContent-Disposition: form-data; name=\"1\"\r\n\r\n2\r\n--hello\r\nContent-Disposition: form-data; name=\"3\"\r\n\r\n4\r\n--hello--\r\n\r\n", res.content)610 end611 def test_post_with_file612 STDOUT.sync = true613 File.open(__FILE__) do |file|614 res = @client.post(serverurl + 'servlet', {1=>2, 3=>file})615 assert_match(/^Content-Disposition: form-data; name="1"\r\n/nm, res.content)616 assert_match(/^Content-Disposition: form-data; name="3";/, res.content)617 assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)618 end619 end620 def test_post_with_file_without_size621 STDOUT.sync = true622 File.open(__FILE__) do |file|623 def file.size624 # Simulates some strange Windows behaviour625 raise SystemCallError.new "Unknown Error (20047)"626 end627 assert_nothing_raised do628 @client.post(serverurl + 'servlet', {1=>2, 3=>file})629 end630 end631 end632 def test_post_with_io # streaming, but not chunked633 myio = StringIO.new("X" * (HTTP::Message::Body::DEFAULT_CHUNK_SIZE + 1))634 def myio.read(*args)635 @called ||= 0636 @called += 1637 super638 end639 def myio.called640 @called641 end642 @client.debug_dev = str = StringIO.new643 res = @client.post(serverurl + 'servlet', {1=>2, 3=>myio})644 assert_match(/\r\nContent-Disposition: form-data; name="1"\r\n/m, res.content)645 assert_match(/\r\n2\r\n/m, res.content)646 assert_match(/\r\nContent-Disposition: form-data; name="3"; filename=""\r\n/m, res.content)647 assert_match(/\r\nContent-Length:/m, str.string)648 assert_equal(3, myio.called)649 end650 def test_post_with_io_nosize # streaming + chunked post651 myio = StringIO.new("4")652 def myio.size653 nil654 end655 @client.debug_dev = str = StringIO.new656 res = @client.post(serverurl + 'servlet', {1=>2, 3=>myio})657 assert_match(/\r\nContent-Disposition: form-data; name="1"\r\n/m, res.content)658 assert_match(/\r\n2\r\n/m, res.content)659 assert_match(/\r\nContent-Disposition: form-data; name="3"; filename=""\r\n/m, res.content)660 assert_match(/\r\n4\r\n/m, res.content)661 assert_match(/\r\nTransfer-Encoding: chunked\r\n/m, str.string)662 end663 def test_post_async664 param = {'1'=>'2', '3'=>'4'}665 conn = @client.post_async(serverurl + 'servlet', param)666 Thread.pass while !conn.finished?667 res = conn.pop668 assert_equal(param, params(res.header["x-query"][0]))669 end670 def test_post_with_block671 called = false672 res = @client.post(serverurl + 'servlet') { |str|673 assert_equal('post,', str)674 called = true675 }676 assert(called)677 assert_nil(res.content)678 #679 called = false680 param = [['1', '2'], ['3', '4']]681 res = @client.post(serverurl + 'servlet', param) { |str|682 assert_equal('post,1=2&3=4', str)683 called = true684 }685 assert(called)686 assert_equal('1=2&3=4', res.header["x-query"][0])687 assert_nil(res.content)688 end689 def test_post_with_custom_multipart690 ext = {'content-type' => 'multipart/form-data'}691 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)692 body = [{ 'Content-Disposition' => 'form-data; name="1"', :content => "2"},693 { 'Content-Disposition' => 'form-data; name="3"', :content => "4"}]694 res = @client.post(serverurl + 'servlet', body, ext)695 assert_match(/Content-Disposition: form-data; name="1"/, res.content)696 assert_match(/Content-Disposition: form-data; name="3"/, res.content)697 #698 ext = {'content-type' => 'multipart/form-data; boundary=hello'}699 assert_equal("post", @client.post(serverurl + 'servlet').content[0, 4], ext)700 res = @client.post(serverurl + 'servlet', body, ext)701 assert_match(/Content-Disposition: form-data; name="1"/, res.content)702 assert_match(/Content-Disposition: form-data; name="3"/, res.content)703 assert_equal("post,--hello\r\nContent-Disposition: form-data; name=\"1\"\r\n\r\n2\r\n--hello\r\nContent-Disposition: form-data; name=\"3\"\r\n\r\n4\r\n--hello--\r\n\r\n", res.content)704 end705 def test_post_with_custom_multipart_and_file706 STDOUT.sync = true707 File.open(__FILE__) do |file|708 ext = { 'Content-Type' => 'multipart/alternative' }709 body = [{ 'Content-Type' => 'text/plain', :content => "this is only a test" },710 { 'Content-Type' => 'application/x-ruby', :content => file }]711 res = @client.post(serverurl + 'servlet', body, ext)712 assert_match(/^Content-Type: text\/plain\r\n/m, res.content)713 assert_match(/^this is only a test\r\n/m, res.content)714 assert_match(/^Content-Type: application\/x-ruby\r\n/m, res.content)715 assert_match(/FIND_TAG_IN_THIS_FILE/, res.content)716 end717 end718 def test_put719 assert_equal("put", @client.put(serverurl + 'servlet').content)720 param = {'1'=>'2', '3'=>'4'}721 res = @client.put(serverurl + 'servlet', param)722 assert_equal(param, params(res.header["x-query"][0]))723 end724 def test_put_bytesize725 res = @client.put(serverurl + 'servlet', 'txt' => 'あいうえお')726 assert_equal('txt=%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A', res.header["x-query"][0])727 assert_equal('15', res.header["x-size"][0])728 end729 def test_put_async730 param = {'1'=>'2', '3'=>'4'}731 conn = @client.put_async(serverurl + 'servlet', param)732 Thread.pass while !conn.finished?733 res = conn.pop734 assert_equal(param, params(res.header["x-query"][0]))735 end736 def test_delete737 assert_equal("delete", @client.delete(serverurl + 'servlet').content)738 end739 def test_delete_async740 conn = @client.delete_async(serverurl + 'servlet')741 Thread.pass while !conn.finished?742 res = conn.pop743 assert_equal('delete', res.content.read)744 end745 def test_options746 assert_equal("options", @client.options(serverurl + 'servlet').content)747 end748 def test_options_async749 conn = @client.options_async(serverurl + 'servlet')750 Thread.pass while !conn.finished?751 res = conn.pop752 assert_equal('options', res.content.read)753 end754 def test_propfind755 assert_equal("propfind", @client.propfind(serverurl + 'servlet').content)756 end757 def test_propfind_async758 conn = @client.propfind_async(serverurl + 'servlet')759 Thread.pass while !conn.finished?760 res = conn.pop761 assert_equal('propfind', res.content.read)762 end763 def test_proppatch764 assert_equal("proppatch", @client.proppatch(serverurl + 'servlet').content)765 param = {'1'=>'2', '3'=>'4'}766 res = @client.proppatch(serverurl + 'servlet', param)767 assert_equal('proppatch', res.content)768 assert_equal(param, params(res.header["x-query"][0]))769 end770 def test_proppatch_async771 param = {'1'=>'2', '3'=>'4'}772 conn = @client.proppatch_async(serverurl + 'servlet', param)773 Thread.pass while !conn.finished?774 res = conn.pop775 assert_equal('proppatch', res.content.read)776 assert_equal(param, params(res.header["x-query"][0]))777 end778 def test_trace779 assert_equal("trace", @client.trace(serverurl + 'servlet').content)780 param = {'1'=>'2', '3'=>'4'}781 res = @client.trace(serverurl + 'servlet', param)782 assert_equal(param, params(res.header["x-query"][0]))783 end784 def test_trace_async785 param = {'1'=>'2', '3'=>'4'}786 conn = @client.trace_async(serverurl + 'servlet', param)787 Thread.pass while !conn.finished?788 res = conn.pop789 assert_equal(param, params(res.header["x-query"][0]))790 end791 def test_chunked792 assert_equal('chunked', @client.get_content(serverurl + 'chunked', { 'msg' => 'chunked' }))793 end794 def test_chunked_empty795 assert_equal('', @client.get_content(serverurl + 'chunked', { 'msg' => '' }))796 end797 def test_get_query798 assert_equal({'1'=>'2'}, check_query_get({1=>2}))799 assert_equal({'a'=>'A', 'B'=>'b'}, check_query_get({"a"=>"A", "B"=>"b"}))800 assert_equal({'&'=>'&'}, check_query_get({"&"=>"&"}))801 assert_equal({'= '=>' =+'}, check_query_get({"= "=>" =+"}))802 assert_equal(803 ['=', '&'].sort,804 check_query_get([["=", "="], ["=", "&"]])['='].to_ary.sort805 )806 assert_equal({'123'=>'45'}, check_query_get('123=45'))807 assert_equal({'12 3'=>'45', ' '=>' '}, check_query_get('12+3=45&+=+'))808 assert_equal({}, check_query_get(''))809 assert_equal({'1'=>'2'}, check_query_get({1=>StringIO.new('2')}))810 assert_equal({'1'=>'2', '3'=>'4'}, check_query_get(StringIO.new('3=4&1=2')))811 end812 def test_post_body813 assert_equal({'1'=>'2'}, check_query_post({1=>2}))814 assert_equal({'a'=>'A', 'B'=>'b'}, check_query_post({"a"=>"A", "B"=>"b"}))815 assert_equal({'&'=>'&'}, check_query_post({"&"=>"&"}))816 assert_equal({'= '=>' =+'}, check_query_post({"= "=>" =+"}))817 assert_equal(818 ['=', '&'].sort,819 check_query_post([["=", "="], ["=", "&"]])['='].to_ary.sort820 )821 assert_equal({'123'=>'45'}, check_query_post('123=45'))822 assert_equal({'12 3'=>'45', ' '=>' '}, check_query_post('12+3=45&+=+'))823 assert_equal({}, check_query_post(''))824 #825 post_body = StringIO.new("foo=bar&foo=baz")826 assert_equal(827 ["bar", "baz"],828 check_query_post(post_body)["foo"].to_ary.sort829 )830 end831 def test_extra_headers832 str = ""833 @client.debug_dev = str834 @client.head(serverurl, nil, {"ABC" => "DEF"})835 lines = str.split(/(?:\r?\n)+/)836 assert_equal("= Request", lines[0])837 assert_match("ABC: DEF", lines[4])838 #839 str = ""840 @client.debug_dev = str841 @client.get(serverurl, nil, [["ABC", "DEF"], ["ABC", "DEF"]])842 lines = str.split(/(?:\r?\n)+/)843 assert_equal("= Request", lines[0])844 assert_match("ABC: DEF", lines[4])845 assert_match("ABC: DEF", lines[5])846 end847 def test_http_custom_date_header848 @client.debug_dev = (str = "")849 res = @client.get(serverurl + 'hello', :header => {'Date' => 'foo'})850 lines = str.split(/(?:\r?\n)+/)851 assert_equal('Date: foo', lines[4])852 end853 def test_timeout854 assert_equal(60, @client.connect_timeout)855 assert_equal(120, @client.send_timeout)856 assert_equal(60, @client.receive_timeout)857 #858 @client.connect_timeout = 1859 @client.send_timeout = 2860 @client.receive_timeout = 3861 assert_equal(1, @client.connect_timeout)862 assert_equal(2, @client.send_timeout)863 assert_equal(3, @client.receive_timeout)864 end865 def test_connect_timeout866 # ToDo867 end868 def test_send_timeout869 # ToDo870 end871 def test_receive_timeout872 # this test takes 2 sec873 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))874 @client.receive_timeout = 1875 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=0'))876 assert_raise(HTTPClient::ReceiveTimeoutError) do877 @client.get_content(serverurl + 'sleep?sec=2')878 end879 @client.receive_timeout = 3880 assert_equal('hello', @client.get_content(serverurl + 'sleep?sec=2'))881 end882 def test_receive_timeout_post883 # this test takes 2 sec884 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)885 @client.receive_timeout = 1886 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 0).content)887 assert_raise(HTTPClient::ReceiveTimeoutError) do888 @client.post(serverurl + 'sleep', :sec => 2)889 end890 @client.receive_timeout = 3891 assert_equal('hello', @client.post(serverurl + 'sleep', :sec => 2).content)892 end893 def test_async_error894 assert_raise( SocketError ) do895 conn = @client.get_async("http://non-existing-host/")896 conn.pop897 end898 end899 def test_reset900 url = serverurl + 'servlet'901 assert_nothing_raised do902 5.times do903 @client.get(url)904 @client.reset(url)905 end906 end907 end908 def test_reset_all909 assert_nothing_raised do910 5.times do911 @client.get(serverurl + 'servlet')912 @client.reset_all913 end914 end915 end916 def test_cookies917 cookiefile = File.join(File.dirname(File.expand_path(__FILE__)), 'test_cookies_file')918 File.open(cookiefile, "wb") do |f|919 f << "http://rubyforge.org/account/login.php session_ser LjEwMy45Ni40Ni0q%2A-fa0537de8cc31 2000000000 .rubyforge.org / 13\n"920 end921 @client.set_cookie_store(cookiefile)922 cookie = @client.cookie_manager.cookies.first923 url = cookie.url924 assert(cookie.domain_match(url.host, cookie.domain))925 #926 @client.reset_all927 @client.test_loopback_http_response << "HTTP/1.0 200 OK\nSet-Cookie: foo=bar; expires=#{Time.at(1924873200).gmtime.httpdate}\n\nOK"928 @client.get_content('http://rubyforge.org/account/login.php')929 @client.save_cookie_store930 str = File.read(cookiefile)931 assert_match(%r(http://rubyforge.org/account/login.php foo bar 1924873200 rubyforge.org /account 1), str)932 File.unlink(cookiefile)933 end934 def test_eof_error_length935 io = StringIO.new('')936 def io.gets(*arg)937 @buf ||= ["HTTP/1.0 200 OK\n", "content-length: 123\n", "\n"]938 @buf.shift939 end940 def io.readpartial(size, buf)941 @second ||= false942 if !@second943 @second = '1st'944 buf << "abc"945 buf946 elsif @second == '1st'947 @second = '2nd'948 raise EOFError.new949 else950 raise Exception.new951 end952 end953 def io.eof?954 true955 end956 @client.test_loopback_http_response << io957 assert_nothing_raised do958 @client.get('http://foo/bar')959 end960 end961 def test_eof_error_rest962 io = StringIO.new('')963 def io.gets(*arg)964 @buf ||= ["HTTP/1.0 200 OK\n", "\n"]965 @buf.shift966 end967 def io.readpartial(size, buf)968 @second ||= false969 if !@second970 @second = '1st'971 buf << "abc"972 buf973 elsif @second == '1st'974 @second = '2nd'975 raise EOFError.new976 else977 raise Exception.new978 end979 end980 def io.eof?981 true982 end983 @client.test_loopback_http_response << io984 assert_nothing_raised do985 @client.get('http://foo/bar')986 end987 end988 def test_urify989 extend HTTPClient::Util990 assert_nil(urify(nil))991 uri = 'http://foo'992 assert_equal(URI.parse(uri), urify(uri))993 assert_equal(URI.parse(uri), urify(URI.parse(uri)))994 end995 def test_connection996 c = HTTPClient::Connection.new997 assert(c.finished?)998 assert_nil(c.join)999 end1000 def test_site1001 site = HTTPClient::Site.new1002 assert_equal('tcp', site.scheme)1003 assert_equal('0.0.0.0', site.host)1004 assert_equal(0, site.port)1005 assert_equal('tcp://0.0.0.0:0', site.addr)1006 assert_equal('tcp://0.0.0.0:0', site.to_s)1007 assert_nothing_raised do1008 site.inspect1009 end1010 #1011 site = HTTPClient::Site.new(URI.parse('http://localhost:12345/foo'))1012 assert_equal('http', site.scheme)1013 assert_equal('localhost', site.host)1014 assert_equal(12345, site.port)1015 assert_equal('http://localhost:12345', site.addr)1016 assert_equal('http://localhost:12345', site.to_s)1017 assert_nothing_raised do1018 site.inspect1019 end1020 #1021 site1 = HTTPClient::Site.new(URI.parse('http://localhost:12341/'))1022 site2 = HTTPClient::Site.new(URI.parse('http://localhost:12342/'))1023 site3 = HTTPClient::Site.new(URI.parse('http://localhost:12342/'))1024 assert(!(site1 == site2))1025 h = { site1 => 'site1', site2 => 'site2' }1026 h[site3] = 'site3'1027 assert_equal('site1', h[site1])1028 assert_equal('site3', h[site2])1029 end1030 def test_http_header1031 res = @client.get(serverurl + 'hello')1032 assert_equal('text/html', res.contenttype)1033 assert_equal(5, res.header.get(nil).size)1034 #1035 res.header.delete('connection')1036 assert_equal(4, res.header.get(nil).size)1037 #1038 res.header['foo'] = 'bar'1039 assert_equal(['bar'], res.header['foo'])1040 #1041 assert_equal([['foo', 'bar']], res.header.get('foo'))1042 res.header['foo'] = ['bar', 'bar2']1043 assert_equal([['foo', 'bar'], ['foo', 'bar2']], res.header.get('foo'))1044 end1045 def test_mime_type1046 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))1047 assert_equal('text/html', HTTP::Message.mime_type('foo.html'))1048 assert_equal('text/html', HTTP::Message.mime_type('foo.htm'))1049 assert_equal('application/msword', HTTP::Message.mime_type('foo.doc'))1050 assert_equal('image/png', HTTP::Message.mime_type('foo.png'))1051 assert_equal('image/gif', HTTP::Message.mime_type('foo.gif'))1052 assert_equal('image/jpeg', HTTP::Message.mime_type('foo.jpg'))1053 assert_equal('image/jpeg', HTTP::Message.mime_type('foo.jpeg'))1054 assert_equal('application/octet-stream', HTTP::Message.mime_type('foo.unknown'))1055 #1056 handler = lambda { |path| 'hello/world' }1057 assert_nil(HTTP::Message.mime_type_handler)1058 assert_nil(HTTP::Message.get_mime_type_func)1059 HTTP::Message.mime_type_handler = handler1060 assert_not_nil(HTTP::Message.mime_type_handler)1061 assert_not_nil(HTTP::Message.get_mime_type_func)1062 assert_equal('hello/world', HTTP::Message.mime_type('foo.txt'))1063 HTTP::Message.mime_type_handler = nil1064 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))1065 HTTP::Message.set_mime_type_func(nil)1066 assert_equal('text/plain', HTTP::Message.mime_type('foo.txt'))1067 #1068 handler = lambda { |path| nil }1069 HTTP::Message.mime_type_handler = handler1070 assert_equal('application/octet-stream', HTTP::Message.mime_type('foo.txt'))1071 end1072 def test_connect_request1073 req = HTTP::Message.new_connect_request(URI.parse('https://foo/bar'))1074 assert_equal("CONNECT foo:443 HTTP/1.0\r\n\r\n", req.dump)1075 req = HTTP::Message.new_connect_request(URI.parse('https://example.com/'))1076 assert_equal("CONNECT example.com:443 HTTP/1.0\r\n\r\n", req.dump)1077 end1078 def test_response1079 res = HTTP::Message.new_response('response')1080 res.contenttype = 'text/plain'1081 res.header.body_date = Time.at(946652400)1082 assert_equal(1083 [1084 "",1085 "Content-Length: 8",1086 "Content-Type: text/plain",1087 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",1088 "Status: 200 OK",1089 "response"1090 ],1091 res.dump.split(/\r\n/).sort1092 )1093 assert_equal(['8'], res.header['Content-Length'])1094 assert_equal('8', res.headers['Content-Length'])1095 res.header.set('foo', 'bar')1096 assert_equal(1097 [1098 "",1099 "Content-Length: 8",1100 "Content-Type: text/plain",1101 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",1102 "Status: 200 OK",1103 "foo: bar",1104 "response"1105 ],1106 res.dump.split(/\r\n/).sort1107 )1108 # nil body1109 res = HTTP::Message.new_response(nil)1110 assert_equal(1111 [1112 "Content-Length: 0",1113 "Content-Type: text/html; charset=us-ascii",1114 "Status: 200 OK"1115 ],1116 res.dump.split(/\r\n/).sort1117 )1118 # for mod_ruby env1119 Object.const_set('Apache', nil)1120 begin1121 res = HTTP::Message.new_response('response')1122 assert(res.dump.split(/\r\n/).any? { |line| /^Date/ =~ line })1123 #1124 res = HTTP::Message.new_response('response')1125 res.contenttype = 'text/plain'1126 res.header.body_date = Time.at(946652400)1127 res.header['Date'] = Time.at(946652400).httpdate1128 assert_equal(1129 [1130 "",1131 "Content-Length: 8",1132 "Content-Type: text/plain",1133 "Date: Fri, 31 Dec 1999 15:00:00 GMT",1134 "HTTP/1.1 200 OK",1135 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",1136 "response"1137 ],1138 res.dump.split(/\r\n/).sort1139 )1140 ensure1141 Object.instance_eval { remove_const('Apache') }1142 end1143 end1144 def test_response_cookies1145 res = HTTP::Message.new_response('response')1146 res.contenttype = 'text/plain'1147 res.header.body_date = Time.at(946652400)1148 assert_nil(res.cookies)1149 #1150 res.header['Set-Cookie'] = [1151 'CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',1152 'PART_NUMBER=ROCKET_LAUNCHER_0001; path=/'1153 ]1154 assert_equal(1155 [1156 "",1157 "Content-Length: 8",1158 "Content-Type: text/plain",1159 "Last-Modified: Fri, 31 Dec 1999 15:00:00 GMT",1160 "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT",1161 "Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/",1162 "Status: 200 OK",1163 "response"1164 ],1165 res.dump.split(/\r\n/).sort1166 )1167 assert_equal(2, res.cookies.size)1168 assert_equal('CUSTOMER', res.cookies[0].name)1169 assert_equal('PART_NUMBER', res.cookies[1].name)1170 end1171 if !defined?(JRUBY_VERSION) and RUBY_VERSION < '1.9'1172 def test_timeout_scheduler1173 assert_equal('hello', @client.get_content(serverurl + 'hello'))1174 status = HTTPClient.timeout_scheduler.instance_eval { @thread.kill; @thread.join; @thread.status }1175 assert(!status) # dead1176 assert_equal('hello', @client.get_content(serverurl + 'hello'))1177 end1178 end1179 def test_session_manager1180 mgr = HTTPClient::SessionManager.new(@client)1181 assert_nil(mgr.instance_eval { @proxy })1182 assert_nil(mgr.debug_dev)1183 @client.debug_dev = Object.new1184 @client.proxy = 'http://myproxy:12345'1185 mgr = HTTPClient::SessionManager.new(@client)1186 assert_equal('http://myproxy:12345', mgr.instance_eval { @proxy }.to_s)1187 assert_equal(@client.debug_dev, mgr.debug_dev)1188 end1189 def create_keepalive_disconnected_thread(idx, sock)1190 Thread.new {1191 # return "12345" for the first connection1192 sock.gets1193 sock.gets1194 sock.write("HTTP/1.1 200 OK\r\n")1195 sock.write("Content-Length: 5\r\n")1196 sock.write("\r\n")1197 sock.write("12345")1198 # for the next connection, close while reading the request for emulating1199 # KeepAliveDisconnected1200 sock.gets1201 sock.close1202 }1203 end1204 def test_keepalive_disconnected1205 client = HTTPClient.new1206 server = TCPServer.open('127.0.0.1', 0)1207 server.listen(30) # set enough backlogs1208 endpoint = "http://127.0.0.1:#{server.addr[1]}/"1209 Thread.new {1210 Thread.abort_on_exception = true1211 # emulate 10 keep-alive connections1212 10.times do |idx|1213 sock = server.accept1214 create_keepalive_disconnected_thread(idx, sock)1215 end1216 # return "23456" for the request which gets KeepAliveDisconnected1217 5.times do1218 sock = server.accept1219 sock.gets1220 sock.gets1221 sock.write("HTTP/1.1 200 OK\r\n")1222 sock.write("\r\n")1223 sock.write("23456")1224 sock.close1225 end1226 # return "34567" for the rest requests1227 while true1228 sock = server.accept1229 sock.gets1230 sock.gets1231 sock.write("HTTP/1.1 200 OK\r\n")1232 sock.write("Connection: close\r\n")1233 sock.write("Content-Length: 5\r\n")1234 sock.write("\r\n")1235 sock.write("34567")1236 sock.close1237 end1238 }1239 # allocate 10 keep-alive connections1240 (0...10).to_a.map {1241 Thread.new {1242 assert_equal("12345", client.get(endpoint).content)1243 }1244 }.each { |th| th.join }1245 # send 5 requests, which should get KeepAliveDesconnected.1246 # doing these requests, rest keep-alive connections are invalidated.1247 (0...5).to_a.map {1248 Thread.new {1249 assert_equal("23456", client.get(endpoint).content)1250 }1251 }.each { |th| th.join }1252 # rest requests won't get KeepAliveDisconnected; how can I check this?1253 (0...10).to_a.map {1254 Thread.new {1255 assert_equal("34567", client.get(endpoint).content)1256 }1257 }.each { |th| th.join }1258 end1259 def create_keepalive_thread(count, sock)1260 Thread.new {1261 Thread.abort_on_exception = true1262 count.times do1263 req = sock.gets1264 while line = sock.gets1265 break if line.chomp.empty?1266 end1267 case req1268 when /chunked/1269 sock.write("HTTP/1.1 200 OK\r\n")1270 sock.write("Transfer-Encoding: chunked\r\n")1271 sock.write("\r\n")1272 sock.write("1a\r\n")1273 sock.write("abcdefghijklmnopqrstuvwxyz\r\n")1274 sock.write("10\r\n")1275 sock.write("1234567890abcdef\r\n")1276 sock.write("0\r\n")1277 sock.write("\r\n")1278 else1279 sock.write("HTTP/1.1 200 OK\r\n")1280 sock.write("Content-Length: 5\r\n")1281 sock.write("\r\n")1282 sock.write("12345")1283 end1284 end1285 sock.close1286 }1287 end1288 def test_keepalive1289 server = TCPServer.open('localhost', 0)1290 server_thread = Thread.new {1291 Thread.abort_on_exception = true1292 sock = server.accept1293 create_keepalive_thread(10, sock)1294 }1295 url = "http://localhost:#{server.addr[1]}/"1296 begin1297 # content-length1298 5.times do1299 assert_equal('12345', @client.get(url).body)1300 end1301 # chunked1302 5.times do1303 assert_equal('abcdefghijklmnopqrstuvwxyz1234567890abcdef', @client.get(url + 'chunked').body)1304 end1305 ensure1306 server.close1307 server_thread.join1308 end1309 end1310 def test_socket_local1311 @client.socket_local.host = '127.0.0.1'1312 assert_equal('hello', @client.get_content(serverurl + 'hello'))1313 @client.reset_all1314 @client.socket_local.port = serverport1315 begin1316 @client.get_content(serverurl + 'hello')1317 rescue Errno::EADDRINUSE, SocketError1318 assert(true)1319 end1320 end1321 def test_body_param_order1322 ary = ('b'..'d').map { |k| ['key2', k] } << ['key1', 'a'] << ['key3', 'z']1323 assert_equal("key2=b&key2=c&key2=d&key1=a&key3=z", HTTP::Message.escape_query(ary))1324 end1325 if RUBY_VERSION > "1.9"1326 def test_charset1327 body = @client.get(serverurl + 'charset').body1328 assert_equal(Encoding::EUC_JP, body.encoding)1329 assert_equal('あいうえお'.encode(Encoding::EUC_JP), body)1330 end1331 end1332private1333 def check_query_get(query)1334 WEBrick::HTTPUtils.parse_query(1335 @client.get(serverurl + 'servlet', query).header["x-query"][0]1336 )1337 end1338 def check_query_post(query)1339 WEBrick::HTTPUtils.parse_query(1340 @client.post(serverurl + 'servlet', query).header["x-query"][0]1341 )1342 end1343 def setup_server1344 @server = WEBrick::HTTPServer.new(1345 :BindAddress => "localhost",1346 :Logger => @logger,1347 :Port => 0,1348 :AccessLog => [],1349 :DocumentRoot => File.dirname(File.expand_path(__FILE__))1350 )1351 @serverport = @server.config[:Port]1352 [:hello, :sleep, :servlet_redirect, :redirect1, :redirect2, :redirect3, :redirect_self, :relative_redirect, :chunked, :largebody, :status, :compressed, :charset].each do |sym|1353 @server.mount(1354 "/#{sym}",1355 WEBrick::HTTPServlet::ProcHandler.new(method("do_#{sym}").to_proc)1356 )1357 end1358 @server.mount('/servlet', TestServlet.new(@server))1359 @server_thread = start_server_thread(@server)1360 end1361 def escape_noproxy1362 backup = HTTPClient::NO_PROXY_HOSTS.dup1363 HTTPClient::NO_PROXY_HOSTS.clear1364 yield1365 ensure1366 HTTPClient::NO_PROXY_HOSTS.replace(backup)1367 end1368 def do_hello(req, res)1369 res['content-type'] = 'text/html'1370 res.body = "hello"1371 end1372 def do_sleep(req, res)1373 sec = req.query['sec'].to_i1374 sleep sec1375 res['content-type'] = 'text/html'1376 res.body = "hello"1377 end1378 def do_servlet_redirect(req, res)1379 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "servlet") 1380 end1381 def do_redirect1(req, res)1382 res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, serverurl + "hello") 1383 end1384 def do_redirect2(req, res)1385 res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect, serverurl + "redirect3")1386 end1387 def do_redirect3(req, res)1388 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "hello") 1389 end1390 def do_redirect_self(req, res)1391 res.set_redirect(WEBrick::HTTPStatus::Found, serverurl + "redirect_self") 1392 end1393 def do_relative_redirect(req, res)1394 res.set_redirect(WEBrick::HTTPStatus::Found, "hello") 1395 end1396 def do_chunked(req, res)1397 res.chunked = true1398 piper, pipew = IO.pipe1399 res.body = piper1400 pipew << req.query['msg']1401 pipew.close1402 end1403 def do_largebody(req, res)1404 res['content-type'] = 'text/html'1405 res.body = "a" * 1000 * 10001406 end1407 def do_compressed(req, res)1408 res['content-type'] = 'application/octet-stream'1409 if req.query['enc'] == 'gzip'1410 res['content-encoding'] = 'gzip'1411 res.body = GZIP_CONTENT1412 elsif req.query['enc'] == 'deflate'1413 res['content-encoding'] = 'deflate'1414 res.body = DEFLATE_CONTENT1415 end1416 end1417 def do_charset(req, res)1418 if RUBY_VERSION > "1.9"1419 res.body = 'あいうえお'.encode("euc-jp")1420 res['Content-Type'] = 'text/plain; charset=euc-jp'1421 else1422 res.body = 'this endpoint is for 1.9 or later'1423 end1424 end1425 def do_status(req, res)1426 res.status = req.query['status'].to_i1427 end1428 class TestServlet < WEBrick::HTTPServlet::AbstractServlet1429 def get_instance(*arg)1430 self1431 end1432 def do_HEAD(req, res)1433 res["x-head"] = 'head' # use this for test purpose only.1434 res["x-query"] = query_response(req)1435 end1436 def do_GET(req, res)1437 res.body = 'get'1438 res["x-query"] = query_response(req)1439 end1440 def do_POST(req, res)1441 res["content-type"] = "text/plain" # iso-8859-1, not US-ASCII1442 res.body = 'post,' + req.body.to_s1443 res["x-query"] = body_response(req)1444 end1445 def do_PUT(req, res)1446 res["x-query"] = body_response(req)1447 param = WEBrick::HTTPUtils.parse_query(req.body) || {}1448 res["x-size"] = (param['txt'] || '').size1449 res.body = param['txt'] || 'put'1450 end1451 def do_DELETE(req, res)1452 res.body = 'delete'1453 end1454 def do_OPTIONS(req, res)1455 # check RFC for legal response.1456 res.body = 'options'1457 end1458 def do_PROPFIND(req, res)1459 res.body = 'propfind'1460 end1461 def do_PROPPATCH(req, res)1462 res.body = 'proppatch'1463 res["x-query"] = body_response(req)1464 end1465 def do_TRACE(req, res)1466 # client SHOULD reflect the message received back to the client as the1467 # entity-body of a 200 (OK) response. [RFC2616]1468 res.body = 'trace'1469 res["x-query"] = query_response(req)1470 end1471 private1472 def query_response(req)1473 query_escape(WEBrick::HTTPUtils.parse_query(req.query_string))1474 end1475 def body_response(req)1476 query_escape(WEBrick::HTTPUtils.parse_query(req.body))1477 end1478 def query_escape(query)1479 escaped = []1480 query.sort_by { |k, v| k }.collect do |k, v|...

Full Screen

Full Screen

aws_client_examples.rb

Source:aws_client_examples.rb Github

copy

Full Screen

...11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13require 'timeout'14module AWS::Core15 shared_examples_for "an aws client" do |sample_method|16 context '#initialize' do17 it 'accepts a config' do18 config = double('config').as_null_object19 new_client = client.class.new(:config => config)20 new_client.config.should == config21 end22 end23 context '#with_options' do24 it 'should return a new client' do25 new_client = client.with_options(:max_retries => 2)26 new_client.should_not == client27 end28 it 'should modify the config on the new client' do29 client.with_options(:max_retries => 2).config.max_retries.should == 230 end31 it 'should not modify the config on the old client' do32 client.with_options(:max_retries => 2)33 client.config.max_retries.should == 334 end35 end36 context '#with_http_handler' do37 it 'should yield request and response objects when a request is made' do38 recorded_request = nil39 recorded_response = nil40 client.with_http_handler { |req, resp|41 recorded_request = req42 recorded_response = resp43 }.send(sample_method)44 recorded_request.should be_a_kind_of(Http::Request)45 recorded_response.should be_a(Http::Response)46 end47 it 'should be able to call super to access previous definition' do48 called_super = false49 called_derived = false50 client.with_http_handler { |req, resp|51 called_super = true52 }.with_http_handler { |req, resp|53 called_derived = true54 super(req, resp)55 }.send(sample_method)56 called_super.should be_true57 called_derived.should be_true58 end59 it 'should set the http_response member to the same instance that was passed to the block' do60 http_response = nil61 response = client.with_http_handler { |req, resp|62 http_response = resp63 }.send(sample_method)64 response.http_response.should be(http_response)65 end66 it 'should set the http_request member to the same instance that was passed to the block' do67 http_request = nil68 response = client.with_http_handler { |req, resp|69 http_request = req70 }.send(sample_method)71 response.http_request.should be(http_request)72 end73 it 'should pass along exceptions raised from the block' do74 error = StandardError.new("FOO")75 begin76 client.with_http_handler { |req, resp|77 raise error78 }.send(sample_method)79 rescue => e80 e.should be(error)81 else82 fail83 end84 end85 end86 end87 # method - the service method, put_bucket, delete_bucket, etc88 # opts - a hash of valid options for the method89 # http_verb - expected http method, PUT, GET, POST or DELETE90 shared_examples_for "an aws http request" do |http_verb|91 #it_should_behave_like "supports async option"92 before(:each) do93 Kernel.stub(:sleep)94 end95 let(:server_error_client) {96 client.with_http_handler do |request, response|97 response.status = 50098 end99 }100 it 'with the proper http verb' do101 request_method = nil102 client.with_http_handler{|request, response|103 request_method = request.http_method104 }.send(method, opts)105 request_method.should == http_verb106 end107 it 'should set use_ssl to the current config use_ssl? value' do108 use_ssl = double('use_ssl_state')109 use_ssl_state = nil110 new_client = client.with_http_handler{|request, response|111 use_ssl_state = request.use_ssl?112 }113 new_client.config.stub(:use_ssl?).and_return(use_ssl)114 new_client.send(method, opts)115 use_ssl_state.should == use_ssl116 end117 it 'populates the response with the request options' do118 resp = client.send(method, opts)119 resp.request_options.should == opts120 end121 it 'raises client errors for errors that can not be retried' do122 lambda {123 client.with_http_handler{|request, response|124 response.status = 405 # method not allowed125 }.send(method, opts)126 }.should raise_error(AWS::Errors::ClientError)127 end128 it 'raises client errors for 4xx response codes with a nil response body' do129 lambda {130 client.with_http_handler{|request, response|131 response.status = 405 # method not allowed132 response.body = nil133 }.send(method, opts)134 }.should raise_error(AWS::Errors::ClientError)135 end136 it 'does not retry client errors' do137 handler = double("handler")138 handler.should_receive(:handle).once do |request, response|139 response.status = 405140 end141 client.with_http_handler(handler).send(method, opts) rescue nil142 end143 it 'retry requests that respond with 500' do144 requests_made = 0145 statuses = [500, 200]146 client.with_http_handler{|req, resp|147 resp.status = statuses.shift148 requests_made += 1149 }.send(method, opts)150 requests_made.should == 2151 end152 it 'retry requests that respond with 503' do153 requests_made = 0154 statuses = [503, 200]155 client.with_http_handler{|req, resp|156 resp.status = statuses.shift157 requests_made += 1158 }.send(method, opts)159 requests_made.should == 2160 end161 it 'retry throttled requests that respond with 400' do162 requests_made = 0163 statuses = [400, 200]164 bodies = ["<FOO><Code>Throttling</Code></FOO>", "<bar/>"]165 client.with_http_handler{|req, resp|166 resp.status = statuses.shift167 resp.body = bodies.shift168 requests_made += 1169 }.send(method, opts)170 requests_made.should == 2171 end172 it 'retry timeout errors' do173 timed_out = false174 requests_made = 0175 client.with_http_handler{|req, resp|176 requests_made += 1177 if timed_out178 resp.status = 200179 resp.body = "<ok/>"180 else181 timed_out = true182 resp.timeout = Timeout::Error.new183 end184 }.send(method, opts)185 requests_made.should == 2186 end187 it 'should retry at most 3 times' do188 requests_made = 0189 begin190 client.with_http_handler{|req, resp|191 resp.status = 500192 requests_made += 1193 }.send(method, opts)194 rescue AWS::Errors::ServerError195 end196 requests_made.should == 4197 end198 it 'should build a new request for each retry' do199 requests = (1..4).map { |i| client.send(:new_request) }200 expect_requests = requests.dup201 client.stub(:new_request) { requests.shift }202 http_handler.should_receive(:handle).exactly(4).times do |req, resp|203 resp.status = 500204 req.should be(expect_requests.shift)205 end206 begin207 client.send(method, opts)208 rescue AWS::Errors::ServerError209 end210 end211 it 'should retry more times if configured' do212 requests_made = 0213 new_client = client.with_http_handler{|req, resp|214 resp.status = 500215 requests_made += 1216 }217 new_client.config.stub(:max_retries).and_return(5)218 begin219 new_client.send(method, opts)220 rescue AWS::Errors::ServerError221 end222 requests_made.should == 6223 end224 it 'should re-raise the server error after retries fail' do225 lambda {226 client.with_http_handler{|req, resp|227 resp.status = 500228 }.send(method, opts)229 }.should raise_error(AWS::Errors::ServerError)230 end231 it 'should raise a network error after retries fail due to timeout' do232 err = Timeout::Error.new233 raised = false234 begin235 client.with_http_handler{|req, resp|236 resp.timeout = err237 }.send(method, opts)238 rescue Exception => e239 e.should be(err)240 raised = true241 end242 raised.should be(true)243 end244 it 'should raise a network error after retries fail' do245 err = StandardError.new('oops')246 raised = false247 begin248 client.with_http_handler{|req, resp|249 resp.timeout = err250 }.send(method, opts)251 rescue Exception => e252 e.should be(err)253 raised = true254 end255 raised.should be(true)256 end257 it 'should sleep between retries' do258 Kernel.should_receive(:sleep).exactly(3).times259 begin260 client.with_http_handler{|req, resp|261 resp.status = 500262 }.send(method, opts);263 rescue264 end265 end266 it 'it backs off exponentially' do267 Kernel.should_receive(:sleep).with(0.3).with(0.6).with(1.2)268 begin269 client.with_http_handler{|req, resp|270 resp.status = 500271 }.send(method, opts)272 rescue273 end274 end275 it 'it uses a randomized scaling factor for throttled requests' do276 Kernel.stub(:rand) { 0.5 }277 Kernel.should_receive(:sleep).with(0.55).with(1.1).with(2.2)278 begin279 client.with_http_handler{|req, resp|280 resp.status = 500281 resp.body = "<FOO><Code>Throttling</Code></FOO>"282 }.send(method, opts)283 rescue284 end285 end286 context 'user_agent' do287 let(:stub_client) { client.with_options(options) }288 let(:options) { { :stub_requests => true } }289 let(:user_agent) do290 response = stub_client.send(method, opts)291 response.http_request.headers['user-agent']292 end293 it 'should set the user_agent header' do294 user_agent.should match(/^aws-sdk-ruby/)295 end296 it 'should prefix the user agent when configured' do297 options[:user_agent_prefix] = "PREFIX"298 user_agent.should match(/^PREFIX/)299 end300 context 'memoization status' do301 it 'should not include "memoizing" if memoization is disabled' do302 AWS.stub(:memoizing?).and_return(false)303 user_agent.should_not include("memoizing")304 end305 it 'should include "memoizing" if memoization is enabled' do306 AWS.stub(:memoizing?).and_return(true)307 user_agent.should include("memoizing")308 end309 end310 end311 context 'endpoint' do312 it 'should default the endpoint given in the client config' do313 stub_client = client.with_options(:s3_endpoint => 'xyz.com',314 :simple_db_endpoint => 'xyz.com',315 :ec2_endpoint => 'xyz.com',316 :stub_requests => true)317 response = stub_client.send(method, opts)318 response.http_request.host.should match('xyz.com')319 end320 end321 context 'user agent' do322 let(:http_handler) { double("handler") }323 let(:client_with_handler) do324 client.with_http_handler(http_handler)325 end326 it 'should send a user-agent header' do327 http_handler.should_receive(:handle).with do |req, resp|328 req.headers["user-agent"].329 should =~ %r{aws-sdk-ruby/#{AWS::VERSION} [a-z]+/[0-9.]+ \w+}330 end331 client_with_handler.send(method, opts)332 end333 end334 context 'stubbing' do335 let(:stub_client) { client.with_options(:stub_requests => true) }336 it 'should not call handle' do337 called = false338 stub_client.with_http_handler do |req, resp|339 called = true340 end.send(method, opts)341 called.should == false342 end343 end344 context 'logging' do345 let(:service) { described_class.to_s.gsub(/^AWS::/, '') }346 let(:logger) { double('logger') }347 let(:logging_client) {348 client.class.new(:config => client.config.with(:logger => logger))349 }350 it 'should log the client request' do351 logger.should_receive(:info)352 logging_client.send(method, opts)353 end354 it 'should log server errors' do355 logger.should_receive(:info)356 begin357 logging_client.with_http_handler do |req,resp|358 resp.status = 502359 resp.body = 'Service busy'360 end.send(method, opts)361 rescue AWS::Errors::ServerError362 end363 end364 end365 context 'signing' do366 let(:fake_request) { client.send(:new_request) }367 before(:each) do368 fake_request369 client.stub(:new_request).and_return(fake_request)370 end371 it 'should sign the request' do372 client.should_receive(:sign_request).with(fake_request)373 client.send(method, opts)374 end375 end376 context 'caching enabled' do377 let(:response) { Response.new(Http::Request.new,378 Http::Response.new) }379 let(:cache) { double("cache",380 :cached => nil,381 :add => nil) }382 before(:each) do383 client.stub(:new_response).and_return(response)384 AWS.stub(:response_cache).and_return(cache)385 end386 it 'should add the low-level response object to the cache' do387 cache.should_receive(:add).with(response)388 client.send(method, opts)389 end390 it 'should not cache on failure' do391 client.stub(:populate_error) do |resp|392 resp.error = StandardError.new("FOO")393 end394 cache.should_not_receive(:add)395 lambda { client.send(method, opts) }.should raise_error("FOO")396 end397 context 'asynchronous' do398 before(:each) do399 client.config.http_handler.stub(:handle_async)400 end401 it 'should add the response on success' do402 handle = nil403 client.config.http_handler.stub(:handle_async) do |req, resp, h|404 resp.status = 200405 handle = h406 end407 client.send(method, opts.merge(:async => true))408 cache.should_receive(:add).with(response)409 handle.signal_success410 end411 it 'should not cache on failure' do412 cache.should_not_receive(:add)413 client.send(method, opts.merge(:async => true)).signal_failure414 end415 end416 end417 end418 shared_examples_for 'supports async option' do419 let(:async_opts) { opts.merge(:async => true) }420 context 'asynchronous' do421 let(:obj) { double("an object") }422 let(:handle) { client.send(method, async_opts) }423 before(:each) do424 client.config.http_handler.stub(:handle_async) do |req, resp, handle|425 @handler_request = req426 @handler_response = resp427 resp.status = 200428 @handler_handle = handle429 end430 handle # forces the above to run431 end432 it 'should call handle_async on the handler' do433 client.config.http_handler.should_receive(:handle_async)434 client.config.http_handler.should_not_receive(:handle)435 client.send(method, async_opts)436 end437 it 'should return a kind of async handle' do438 client.config.http_handler.stub(:handle_async)439 client.send(method, async_opts).should be_kind_of(AsyncHandle)440 end441 it 'should call the success callback on handler success' do442 handle.on_success { obj.success }443 obj.should_receive(:success)444 @handler_handle.signal_success445 end446 it 'should call the failure callback on handler failure' do447 handle.on_failure { obj.failure }448 obj.should_receive(:failure)449 @handler_handle.signal_failure450 end451 it 'should make an error available on handler failure' do452 @handler_handle.signal_failure453 handle.error.should be_a(StandardError)454 end455 it 'should call the failure callback on service failure' do456 obj.should_receive(:failure).once457 handle.on_failure { obj.failure }458 @handler_response.status = 500459 @handler_handle.signal_failure460 end461 it 'should retry service errors' do462 new_client = client.with_http_handler do |req, resp|463 resp.status = 500464 end465 http_handler = new_client.config.http_handler466 Kernel.should_receive(:sleep).with(0.3).with(0.6).with(1.2)467 response = new_client.send(method, async_opts)468 complete = false469 response.on_complete do |status|470 complete = true471 status.should == :failure472 response.error.should be_a(AWS::Errors::ServerError)473 end474 sleep 0.001 until complete475 end476 it 'should retry timeouts' do477 new_client = client.with_http_handler do |req, resp|478 resp.timeout = TimeoutError.new479 end480 http_handler = new_client.config.http_handler481 Kernel.should_receive(:sleep).with(0.3).with(0.6).with(1.2)482 response = new_client.send(method, async_opts)483 complete = false484 response.on_complete do |status|485 complete = true486 status.should == :failure487 response.error.should be_a(TimeoutError)488 end489 sleep 0.001 until complete490 end491 it 'should build a new request for each retry' do492 seen_requests = []493 new_client = client.with_http_handler do |req, resp|494 resp.timeout = TimeoutError.new495 seen_requests << req496 end497 Kernel.stub(:sleep)498 response = new_client.send(method, async_opts)499 complete = false500 response.on_complete do |status|501 complete = true502 seen_requests.uniq.size.should == 4503 end504 sleep 0.001 until complete505 end506 end507 end508 shared_examples_for 'parses response' do509 let(:response_body) { "foo response" }510 it 'should parse the response body' do511 http_handler.stub(:handle) do |req, resp|512 resp.status = 200513 resp.body = response_body514 end515 client.send(method, opts)516 end517 context 'asynchronous' do518 it 'should parse the response body on completion' do519 http_resp = nil520 handler_handle = nil521 http_handler.stub(:handle_async) do |req, resp, handle|522 http_resp = resp523 http_resp.stub(:body) { fail "read body before it was ready" }524 handler_handle = handle525 end526 resp = client.send(method, opts.merge(:async => true))527 http_resp.stub(:body).and_return(response_body)528 http_resp.status = 200529 handler_handle.signal_success530 end531 end532 end533end...

Full Screen

Full Screen

client_spec.rb

Source:client_spec.rb Github

copy

Full Screen

...35 )36 end37 describe "following redirects" do38 it "returns response of new location" do39 client = StubbedClient.new(:follow => true).stub(40 "http://example.com/" => redirect_response("http://example.com/blog"),41 "http://example.com/blog" => simple_response("OK")42 )43 expect(client.get("http://example.com/").to_s).to eq "OK"44 end45 it "prepends previous request uri scheme and host if needed" do46 client = StubbedClient.new(:follow => true).stub(47 "http://example.com/" => redirect_response("/index"),48 "http://example.com/index" => redirect_response("/index.html"),49 "http://example.com/index.html" => simple_response("OK")50 )51 expect(client.get("http://example.com/").to_s).to eq "OK"52 end53 it "fails upon endless redirects" do54 client = StubbedClient.new(:follow => true).stub(55 "http://example.com/" => redirect_response("/")56 )57 expect { client.get("http://example.com/") }.58 to raise_error(HTTP::Redirector::EndlessRedirectError)59 end60 it "fails if max amount of hops reached" do61 client = StubbedClient.new(:follow => {:max_hops => 5}).stub(62 "http://example.com/" => redirect_response("/1"),63 "http://example.com/1" => redirect_response("/2"),64 "http://example.com/2" => redirect_response("/3"),65 "http://example.com/3" => redirect_response("/4"),66 "http://example.com/4" => redirect_response("/5"),67 "http://example.com/5" => redirect_response("/6"),68 "http://example.com/6" => simple_response("OK")69 )70 expect { client.get("http://example.com/") }.71 to raise_error(HTTP::Redirector::TooManyRedirectsError)72 end73 context "with non-ASCII URLs" do74 it "theoretically works like a charm" do75 client = StubbedClient.new(:follow => true).stub(76 "http://example.com/" => redirect_response("/könig"),77 "http://example.com/könig" => simple_response("OK")78 )79 expect { client.get "http://example.com/könig" }.not_to raise_error80 end81 it "works like a charm in real world" do82 url = "http://git.io/jNeY"83 client = HTTP.follow84 expect(client.get(url).to_s).to include "support for non-ascii URIs"85 end86 end87 end88 describe "parsing params" do89 let(:client) { HTTP::Client.new }90 before { allow(client).to receive :perform }91 it "accepts params within the provided URL" do92 expect(HTTP::Request).to receive(:new) do |opts|93 expect(CGI.parse(opts[:uri].query)).to eq("foo" => %w(bar))94 end95 client.get("http://example.com/?foo=bar")96 end97 it "combines GET params from the URI with the passed in params" do98 expect(HTTP::Request).to receive(:new) do |opts|99 expect(CGI.parse(opts[:uri].query)).to eq("foo" => %w(bar), "baz" => %w(quux))100 end101 client.get("http://example.com/?foo=bar", :params => {:baz => "quux"})102 end103 it "merges duplicate values" do104 expect(HTTP::Request).to receive(:new) do |opts|105 expect(opts[:uri].query).to match(/^(a=1&a=2|a=2&a=1)$/)106 end107 client.get("http://example.com/?a=1", :params => {:a => 2})108 end109 it "does not modifies query part if no params were given" do110 expect(HTTP::Request).to receive(:new) do |opts|111 expect(opts[:uri].query).to eq "deadbeef"112 end113 client.get("http://example.com/?deadbeef")114 end115 it "does not corrupts index-less arrays" do116 expect(HTTP::Request).to receive(:new) do |opts|117 expect(CGI.parse(opts[:uri].query)).to eq "a[]" => %w(b c), "d" => %w(e)118 end119 client.get("http://example.com/?a[]=b&a[]=c", :params => {:d => "e"})120 end121 it "properly encodes colons" do122 expect(HTTP::Request).to receive(:new) do |opts|123 expect(opts[:uri].query).to eq "t=1970-01-01T00%3A00%3A00Z"124 end125 client.get("http://example.com/", :params => {:t => "1970-01-01T00:00:00Z"})126 end127 end128 describe "passing json" do129 it "encodes given object" do130 client = HTTP::Client.new131 allow(client).to receive(:perform)132 expect(HTTP::Request).to receive(:new) do |opts|133 expect(opts[:body]).to eq '{"foo":"bar"}'134 end135 client.get("http://example.com/", :json => {:foo => :bar})136 end137 end138 describe "#request" do139 context "with non-ASCII URLs" do140 it "theoretically works like a charm" do141 client = described_class.new142 expect { client.get "#{dummy.endpoint}/könig" }.not_to raise_error143 end144 it "works like a charm in real world" do145 url = "https://github.com/httprb/http.rb/pull/197/ö無"146 client = HTTP.follow147 expect(client.get(url).to_s).to include "support for non-ascii URIs"148 end149 end150 context "with explicitly given `Host` header" do151 let(:headers) { {"Host" => "another.example.com"} }152 let(:client) { described_class.new :headers => headers }153 it "keeps `Host` header as is" do154 expect(client).to receive(:perform) do |req, _|155 expect(req["Host"]).to eq "another.example.com"156 end157 client.request(:get, "http://example.com/")158 end159 end160 end161 include_context "HTTP handling" do162 let(:extra_options) { {} }163 let(:options) { {} }164 let(:server) { dummy }165 let(:client) { described_class.new(options.merge(extra_options)) }166 end167 describe "working with SSL" do168 run_server(:dummy_ssl) { DummyServer.new(:ssl => true) }169 let(:extra_options) { {} }170 let(:client) do171 described_class.new options.merge(:ssl_context => SSLHelper.client_context).merge(extra_options)172 end173 include_context "HTTP handling" do174 let(:server) { dummy_ssl }175 end176 it "just works" do177 response = client.get(dummy_ssl.endpoint)178 expect(response.body.to_s).to eq("<!doctype html>")179 end180 it "fails with OpenSSL::SSL::SSLError if host mismatch" do181 expect { client.get(dummy_ssl.endpoint.gsub("127.0.0.1", "localhost")) }.182 to raise_error(OpenSSL::SSL::SSLError, /does not match/)183 end184 context "with SSL options instead of a context" do185 let(:client) do186 described_class.new options.merge :ssl => SSLHelper.client_params187 end188 it "just works" do189 response = client.get(dummy_ssl.endpoint)190 expect(response.body.to_s).to eq("<!doctype html>")191 end192 end193 end194 describe "#perform" do195 let(:client) { described_class.new }196 it "calls finish_response once body was fully flushed" do197 expect_any_instance_of(HTTP::Connection).to receive(:finish_response).and_call_original198 client.get(dummy.endpoint).to_s199 end200 context "with HEAD request" do201 it "does not iterates through body" do202 expect_any_instance_of(HTTP::Connection).to_not receive(:readpartial)203 client.head(dummy.endpoint)204 end205 it "finishes response after headers were received" do206 expect_any_instance_of(HTTP::Connection).to receive(:finish_response).and_call_original207 client.head(dummy.endpoint)208 end209 end210 context "when server fully flushes response in one chunk" do211 before do212 socket_spy = double213 chunks = [214 <<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")215 | HTTP/1.1 200 OK216 | Content-Type: text/html217 | Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-11-22)218 | Date: Mon, 24 Mar 2014 00:32:22 GMT219 | Content-Length: 15220 | Connection: Keep-Alive221 |222 | <!doctype html>223 RESPONSE224 ]225 allow(socket_spy).to receive(:close) { nil }226 allow(socket_spy).to receive(:closed?) { true }227 allow(socket_spy).to receive(:readpartial) { chunks[0] }228 allow(socket_spy).to receive(:write) { chunks[0].length }229 allow(TCPSocket).to receive(:open) { socket_spy }230 end231 it "properly reads body" do232 body = client.get(dummy.endpoint).to_s233 expect(body).to eq "<!doctype html>"234 end235 end236 end237end...

Full Screen

Full Screen

test_client.rb

Source:test_client.rb Github

copy

Full Screen

1require 'minitest/autorun'2require 'xmlrpc/client'3require 'net/http'4begin5 require 'openssl'6rescue LoadError7end8module XMLRPC9 class ClientTest < MiniTest::Unit::TestCase10 module Fake11 class HTTP12 attr_accessor :read_timeout, :open_timeout, :use_ssl13 def initialize responses = {}14 @started = false15 @responses = responses16 end17 def started?18 @started19 end20 def start21 @started = true22 if block_given?23 begin24 return yield(self)25 ensure26 @started = false27 end28 end29 self30 end31 def request_post path, request, headers32 @responses[path].shift33 end34 end35 class Client < XMLRPC::Client36 attr_reader :args, :http37 def initialize(*args)38 @args = args39 super40 end41 private42 def net_http host, port, proxy_host, proxy_port43 HTTP.new44 end45 end46 class Response47 def self.new body, fields = [], status = '200'48 klass = Class.new(Net::HTTPResponse::CODE_TO_OBJ[status]) {49 def initialize(*args)50 super51 @read = true52 end53 }54 resp = klass.new '1.1', status, 'OK'55 resp.body = body56 fields.each do |k,v|57 resp.add_field k, v58 end59 resp60 end61 end62 end63 def test_new2_host_path_port64 client = Fake::Client.new2 'http://example.org/foo'65 host, path, port, *rest = client.args66 assert_equal 'example.org', host67 assert_equal '/foo', path68 assert_equal 80, port69 rest.each { |x| refute x }70 end71 def test_new2_custom_port72 client = Fake::Client.new2 'http://example.org:1234/foo'73 host, path, port, *rest = client.args74 assert_equal 'example.org', host75 assert_equal '/foo', path76 assert_equal 1234, port77 rest.each { |x| refute x }78 end79 def test_new2_ssl80 client = Fake::Client.new2 'https://example.org/foo'81 host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args82 assert_equal 'example.org', host83 assert_equal '/foo', path84 assert_equal 443, port85 assert use_ssl86 refute proxy_host87 refute proxy_port88 refute user89 refute password90 refute timeout91 end if defined?(OpenSSL)92 def test_new2_ssl_custom_port93 client = Fake::Client.new2 'https://example.org:1234/foo'94 host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args95 assert_equal 'example.org', host96 assert_equal '/foo', path97 assert_equal 1234, port98 assert use_ssl99 refute proxy_host100 refute proxy_port101 refute user102 refute password103 refute timeout104 end if defined?(OpenSSL)105 def test_new2_user_password106 client = Fake::Client.new2 'http://aaron:tenderlove@example.org/foo'107 host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args108 [ host, path, port ].each { |x| assert x }109 assert_equal 'aaron', user110 assert_equal 'tenderlove', password111 [ proxy_host, proxy_port, use_ssl, timeout ].each { |x| refute x }112 end113 def test_new2_proxy_host114 client = Fake::Client.new2 'http://example.org/foo', 'example.com'115 host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args116 [ host, path, port ].each { |x| assert x }117 assert_equal 'example.com', proxy_host118 [ user, password, proxy_port, use_ssl, timeout ].each { |x| refute x }119 end120 def test_new2_proxy_port121 client = Fake::Client.new2 'http://example.org/foo', 'example.com:1234'122 host, path, port, proxy_host, proxy_port, user, password, use_ssl, timeout = client.args123 [ host, path, port ].each { |x| assert x }124 assert_equal 'example.com', proxy_host125 assert_equal 1234, proxy_port126 [ user, password, use_ssl, timeout ].each { |x| refute x }127 end128 def test_new2_no_path129 client = Fake::Client.new2 'http://example.org'130 host, path, port, *rest = client.args131 assert_equal 'example.org', host132 assert_nil path133 assert port134 rest.each { |x| refute x }135 end136 def test_new2_slash_path137 client = Fake::Client.new2 'http://example.org/'138 host, path, port, *rest = client.args139 assert_equal 'example.org', host140 assert_equal '/', path141 assert port142 rest.each { |x| refute x }143 end144 def test_new2_bad_protocol145 assert_raises(ArgumentError) do146 XMLRPC::Client.new2 'ftp://example.org'147 end148 end149 def test_new2_bad_uri150 assert_raises(ArgumentError) do151 XMLRPC::Client.new2 ':::::'152 end153 end154 def test_new2_path_with_query155 client = Fake::Client.new2 'http://example.org/foo?bar=baz'156 host, path, port, *rest = client.args157 assert_equal 'example.org', host158 assert_equal '/foo?bar=baz', path159 assert port160 rest.each { |x| refute x }161 end162 def test_request163 fh = read 'blog.xml'164 responses = {165 '/foo' => [ Fake::Response.new(fh, [['Content-Type', 'text/xml']]) ]166 }167 client = fake_client(responses).new2 'http://example.org/foo'168 resp = client.call('wp.getUsersBlogs', 'tlo', 'omg')169 expected = [{170 "isAdmin" => true,171 "url" => "http://tenderlovemaking.com/",172 "blogid" => "1",173 "blogName" => "Tender Lovemaking",174 "xmlrpc" => "http://tenderlovemaking.com/xmlrpc.php"175 }]176 assert_equal expected, resp177 end178 def test_async_request179 fh = read 'blog.xml'180 responses = {181 '/foo' => [ Fake::Response.new(fh, [['Content-Type', 'text/xml']]) ]182 }183 client = fake_client(responses).new2 'http://example.org/foo'184 resp = client.call_async('wp.getUsersBlogs', 'tlo', 'omg')185 expected = [{186 "isAdmin" => true,187 "url" => "http://tenderlovemaking.com/",188 "blogid" => "1",189 "blogName" => "Tender Lovemaking",190 "xmlrpc" => "http://tenderlovemaking.com/xmlrpc.php"191 }]192 assert_equal expected, resp193 end194 # make a request without content-type header195 def test_bad_content_type196 fh = read 'blog.xml'197 responses = {198 '/foo' => [ Fake::Response.new(fh) ]199 }200 client = fake_client(responses).new2 'http://example.org/foo'201 resp = client.call('wp.getUsersBlogs', 'tlo', 'omg')202 expected = [{203 "isAdmin" => true,204 "url" => "http://tenderlovemaking.com/",205 "blogid" => "1",206 "blogName" => "Tender Lovemaking",207 "xmlrpc" => "http://tenderlovemaking.com/xmlrpc.php"208 }]209 assert_equal expected, resp210 end211 def test_i8_tag212 fh = read('blog.xml').gsub(/string/, 'i8')213 responses = {214 '/foo' => [ Fake::Response.new(fh) ]215 }216 client = fake_client(responses).new2 'http://example.org/foo'217 resp = client.call('wp.getUsersBlogs', 'tlo', 'omg')218 assert_equal 1, resp.first['blogid']219 end220 def test_cookie_simple221 client = Fake::Client.new2('http://example.org/cookie')222 assert_nil(client.cookie)223 client.send(:parse_set_cookies, ["param1=value1", "param2=value2"])224 assert_equal("param1=value1; param2=value2", client.cookie)225 end226 def test_cookie_override227 client = Fake::Client.new2('http://example.org/cookie')228 client.send(:parse_set_cookies,229 [230 "param1=value1",231 "param2=value2",232 "param1=value3",233 ])234 assert_equal("param2=value2; param1=value3", client.cookie)235 end236 private237 def read filename238 File.read File.expand_path(File.join(__FILE__, '..', 'data', filename))239 end240 def fake_client responses241 Class.new(Fake::Client) {242 define_method(:net_http) { |*_| Fake::HTTP.new(responses) }243 }244 end245 end246end...

Full Screen

Full Screen

test_ssl.rb

Source:test_ssl.rb Github

copy

Full Screen

...15 Config::CONFIG["ruby_install_name"] + Config::CONFIG["EXEEXT"]16 )17 def setup18 @url = "https://localhost:#{PORT}/hello"19 @serverpid = @client = nil20 @verify_callback_called = false21 setup_server22 setup_client23 end24 def teardown25 teardown_client26 teardown_server27 end28 def test_options29 cfg = @client.streamhandler.client.ssl_config30 assert_nil(cfg.client_cert)31 assert_nil(cfg.client_key)32 assert_nil(cfg.client_ca)33 assert_equal(OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT, cfg.verify_mode)34 assert_nil(cfg.verify_callback)35 assert_nil(cfg.timeout)36 assert_equal(OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv2, cfg.options)37 assert_equal("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH", cfg.ciphers)38 assert_instance_of(OpenSSL::X509::Store, cfg.cert_store)39 # dummy call to ensure sslsvr initialization finished.40 assert_raise(OpenSSL::SSL::SSLError) do41 @client.hello_world("ssl client")42 end43 end44 def test_verification45 cfg = @client.options46 cfg["protocol.http.ssl_config.verify_callback"] = method(:verify_callback).to_proc47 @verify_callback_called = false48 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}49 assert_equal("certificate verify failed", ssle.message)50 assert(@verify_callback_called)51 #52 cfg["protocol.http.ssl_config.client_cert"] = File.join(DIR, "client.cert")53 cfg["protocol.http.ssl_config.client_key"] = File.join(DIR, "client.key")54 @verify_callback_called = false55 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}56 assert_equal("certificate verify failed", ssle.message)57 assert(@verify_callback_called)58 #59 cfg["protocol.http.ssl_config.ca_file"] = File.join(DIR, "ca.cert")60 @verify_callback_called = false61 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}62 assert_equal("certificate verify failed", ssle.message)63 assert(@verify_callback_called)64 #65 cfg["protocol.http.ssl_config.ca_file"] = File.join(DIR, "subca.cert")66 @verify_callback_called = false67 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))68 assert(@verify_callback_called)69 #70 cfg["protocol.http.ssl_config.verify_depth"] = "1"71 @verify_callback_called = false72 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}73 assert_equal("certificate verify failed", ssle.message)74 assert(@verify_callback_called)75 #76 cfg["protocol.http.ssl_config.verify_depth"] = ""77 cfg["protocol.http.ssl_config.cert_store"] = OpenSSL::X509::Store.new78 cfg["protocol.http.ssl_config.verify_mode"] = OpenSSL::SSL::VERIFY_PEER.to_s79 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}80 assert_equal("certificate verify failed", ssle.message)81 #82 cfg["protocol.http.ssl_config.verify_mode"] = ""83 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))84 end85 def test_property86 testpropertyname = File.join(DIR, 'soapclient.properties')87 File.open(testpropertyname, "w") do |f|88 f <<<<__EOP__89protocol.http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_PEER90# depth: 1 causes an error (intentional)91protocol.http.ssl_config.verify_depth = 192protocol.http.ssl_config.client_cert = #{File.join(DIR, 'client.cert')}93protocol.http.ssl_config.client_key = #{File.join(DIR, 'client.key')}94protocol.http.ssl_config.ca_file = #{File.join(DIR, 'ca.cert')}95protocol.http.ssl_config.ca_file = #{File.join(DIR, 'subca.cert')}96protocol.http.ssl_config.ciphers = ALL97__EOP__98 end99 begin100 @client.loadproperty(testpropertyname)101 @client.options["protocol.http.ssl_config.verify_callback"] = method(:verify_callback).to_proc102 @verify_callback_called = false103 # NG with String104 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}105 assert_equal("certificate verify failed", ssle.message)106 assert(@verify_callback_called)107 # NG with Integer108 @client.options["protocol.http.ssl_config.verify_depth"] = 0109 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}110 assert_equal("certificate verify failed", ssle.message)111 assert(@verify_callback_called)112 # OK with empty113 @client.options["protocol.http.ssl_config.verify_depth"] = ""114 @verify_callback_called = false115 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))116 assert(@verify_callback_called)117 # OK with nil118 @client.options["protocol.http.ssl_config.verify_depth"] = nil119 @verify_callback_called = false120 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))121 assert(@verify_callback_called)122 # OK with String123 @client.options["protocol.http.ssl_config.verify_depth"] = "3"124 @verify_callback_called = false125 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))126 assert(@verify_callback_called)127 # OK with Integer128 @client.options["protocol.http.ssl_config.verify_depth"] = 3129 @verify_callback_called = false130 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))131 assert(@verify_callback_called)132 ensure133 File.unlink(testpropertyname)134 end135 end136 def test_ciphers137 cfg = @client.options138 cfg["protocol.http.ssl_config.client_cert"] = File.join(DIR, 'client.cert')139 cfg["protocol.http.ssl_config.client_key"] = File.join(DIR, 'client.key')140 cfg["protocol.http.ssl_config.ca_file"] = File.join(DIR, "ca.cert")141 cfg["protocol.http.ssl_config.ca_file"] = File.join(DIR, "subca.cert")142 #cfg.timeout = 123143 cfg["protocol.http.ssl_config.ciphers"] = "!ALL"144 #145 ssle = assert_raise(OpenSSL::SSL::SSLError) {@client.hello_world("ssl client")}146 # depends on OpenSSL version. (?:0.9.8|0.9.7)147 assert_match(/\A(?:SSL_CTX_set_cipher_list:: no cipher match|no ciphers available)\z/, ssle.message)148 #149 cfg["protocol.http.ssl_config.ciphers"] = "ALL"150 assert_equal("Hello World, from ssl client", @client.hello_world("ssl client"))151 end152private153 def q(str)154 %Q["#{str}"]155 end156 def setup_server157 svrcmd = "#{q(RUBY)} "158 #svrcmd << "-d " if $DEBUG159 svrcmd << File.join(DIR, "sslsvr.rb")160 svrout = IO.popen(svrcmd)161 @serverpid = Integer(svrout.gets.chomp)162 end163 def setup_client164 @client = SOAP::RPC::Driver.new(@url, 'urn:ssltst')165 @client.add_method("hello_world", "from")166 end167 def teardown_server168 if @serverpid169 Process.kill('KILL', @serverpid)170 Process.waitpid(@serverpid)171 end172 end173 def teardown_client174 @client.reset_stream if @client175 end176 def verify_callback(ok, cert)177 @verify_callback_called = true178 p ["client", ok, cert] if $DEBUG179 ok180 end181end182end; end183end...

Full Screen

Full Screen

test_request.rb

Source:test_request.rb Github

copy

Full Screen

...20 end21 @lint = Rack::Lint.new(@app)22 end23 def test_options24 client = MockRequest.new("OPTIONS * HTTP/1.1\r\n" \25 "Host: foo\r\n\r\n")26 env = @request.read(client)27 assert_equal '', env['REQUEST_PATH']28 assert_equal '', env['PATH_INFO']29 assert_equal '*', env['REQUEST_URI']30 res = @lint.call(env)31 end32 def test_absolute_uri_with_query33 client = MockRequest.new("GET http://e:3/x?y=z HTTP/1.1\r\n" \34 "Host: foo\r\n\r\n")35 env = @request.read(client)36 assert_equal '/x', env['REQUEST_PATH']37 assert_equal '/x', env['PATH_INFO']38 assert_equal 'y=z', env['QUERY_STRING']39 res = @lint.call(env)40 end41 def test_absolute_uri_with_fragment42 client = MockRequest.new("GET http://e:3/x#frag HTTP/1.1\r\n" \43 "Host: foo\r\n\r\n")44 env = @request.read(client)45 assert_equal '/x', env['REQUEST_PATH']46 assert_equal '/x', env['PATH_INFO']47 assert_equal '', env['QUERY_STRING']48 assert_equal 'frag', env['FRAGMENT']49 res = @lint.call(env)50 end51 def test_absolute_uri_with_query_and_fragment52 client = MockRequest.new("GET http://e:3/x?a=b#frag HTTP/1.1\r\n" \53 "Host: foo\r\n\r\n")54 env = @request.read(client)55 assert_equal '/x', env['REQUEST_PATH']56 assert_equal '/x', env['PATH_INFO']57 assert_equal 'a=b', env['QUERY_STRING']58 assert_equal 'frag', env['FRAGMENT']59 res = @lint.call(env)60 end61 def test_absolute_uri_unsupported_schemes62 %w(ssh+http://e/ ftp://e/x http+ssh://e/x).each do |abs_uri|63 client = MockRequest.new("GET #{abs_uri} HTTP/1.1\r\n" \64 "Host: foo\r\n\r\n")65 assert_raises(HttpParserError) { @request.read(client) }66 end67 end68 def test_x_forwarded_proto_https69 client = MockRequest.new("GET / HTTP/1.1\r\n" \70 "X-Forwarded-Proto: https\r\n" \71 "Host: foo\r\n\r\n")72 env = @request.read(client)73 assert_equal "https", env['rack.url_scheme']74 res = @lint.call(env)75 end76 def test_x_forwarded_proto_http77 client = MockRequest.new("GET / HTTP/1.1\r\n" \78 "X-Forwarded-Proto: http\r\n" \79 "Host: foo\r\n\r\n")80 env = @request.read(client)81 assert_equal "http", env['rack.url_scheme']82 res = @lint.call(env)83 end84 def test_x_forwarded_proto_invalid85 client = MockRequest.new("GET / HTTP/1.1\r\n" \86 "X-Forwarded-Proto: ftp\r\n" \87 "Host: foo\r\n\r\n")88 env = @request.read(client)89 assert_equal "http", env['rack.url_scheme']90 res = @lint.call(env)91 end92 def test_rack_lint_get93 client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")94 env = @request.read(client)95 assert_equal "http", env['rack.url_scheme']96 assert_equal '127.0.0.1', env['REMOTE_ADDR']97 res = @lint.call(env)98 end99 def test_no_content_stringio100 client = MockRequest.new("GET / HTTP/1.1\r\nHost: foo\r\n\r\n")101 env = @request.read(client)102 assert_equal StringIO, env['rack.input'].class103 end104 def test_zero_content_stringio105 client = MockRequest.new("PUT / HTTP/1.1\r\n" \106 "Content-Length: 0\r\n" \107 "Host: foo\r\n\r\n")108 env = @request.read(client)109 assert_equal StringIO, env['rack.input'].class110 end111 def test_real_content_not_stringio112 client = MockRequest.new("PUT / HTTP/1.1\r\n" \113 "Content-Length: 1\r\n" \114 "Host: foo\r\n\r\n")115 env = @request.read(client)116 assert_equal Unicorn::TeeInput, env['rack.input'].class117 end118 def test_rack_lint_put119 client = MockRequest.new(120 "PUT / HTTP/1.1\r\n" \121 "Host: foo\r\n" \122 "Content-Length: 5\r\n" \123 "\r\n" \124 "abcde")125 env = @request.read(client)126 assert ! env.include?(:http_body)127 res = @lint.call(env)128 end129 def test_rack_lint_big_put130 count = 100131 bs = 0x10000132 buf = (' ' * bs).freeze133 length = bs * count134 client = Tempfile.new('big_put')135 def client.kgio_addr; '127.0.0.1'; end136 def client.kgio_read(*args)137 readpartial(*args)138 rescue EOFError139 end140 def client.kgio_read!(*args)141 readpartial(*args)142 end143 client.syswrite(144 "PUT / HTTP/1.1\r\n" \145 "Host: foo\r\n" \146 "Content-Length: #{length}\r\n" \147 "\r\n")148 count.times { assert_equal bs, client.syswrite(buf) }149 assert_equal 0, client.sysseek(0)150 env = @request.read(client)151 assert ! env.include?(:http_body)152 assert_equal length, env['rack.input'].size153 count.times {154 tmp = env['rack.input'].read(bs)155 tmp << env['rack.input'].read(bs - tmp.size) if tmp.size != bs156 assert_equal buf, tmp157 }158 assert_nil env['rack.input'].read(bs)159 env['rack.input'].rewind160 res = @lint.call(env)161 end162end...

Full Screen

Full Screen

curb.rb

Source:curb.rb Github

copy

Full Screen

...20 module WebDriver21 module Remote22 module Http23 #24 # An alternative to the default Net::HTTP client.25 #26 # This can be used for the Firefox and Remote drivers if you have Curb27 # installed.28 #29 # @example Using Curb30 # require 'selenium/webdriver/remote/http/curb'31 # include Selenium32 #33 # driver = WebDriver.for :firefox, :http_client => WebDriver::Remote::Http::Curb.new34 #35 class Curb < Common36 def quit_errors37 [Curl::Err::RecvError] + super38 end39 private40 def request(verb, url, headers, payload)41 client.url = url.to_s42 # workaround for http://github.com/taf2/curb/issues/issue/4043 # curb will handle this for us anyway44 headers.delete 'Content-Length'45 client.headers = headers46 # http://github.com/taf2/curb/issues/issue/3347 client.head = false48 client.delete = false49 case verb50 when :get51 client.http_get52 when :post53 client.post_body = payload || ''54 client.http_post55 when :put56 client.put_data = payload || ''57 client.http_put58 when :delete59 client.http_delete60 when :head61 client.http_head62 else63 raise Error::WebDriverError, "unknown HTTP verb: #{verb.inspect}"64 end65 create_response client.response_code, client.body_str, client.content_type66 end67 def client68 @client ||= begin69 c = Curl::Easy.new70 c.max_redirects = MAX_REDIRECTS71 c.follow_location = true72 c.timeout = @timeout if @timeout73 c.verbose = WebDriver.logger.info?74 c75 end76 end77 end # Curb78 end # Http79 end # Remote80 end # WebDriver81end # Selenium...

Full Screen

Full Screen

test_include_client.rb

Source:test_include_client.rb Github

copy

Full Screen

1# -*- encoding: utf-8 -*-2require File.expand_path('helper', File.dirname(__FILE__))3require 'httpclient/include_client'4class TestIncludeClient < Test::Unit::TestCase5 class Widget6 extend HTTPClient::IncludeClient7 include_http_client("http://example.com") do |client|8 client.cookie_manager = nil9 client.agent_name = "iMonkey 4k"10 end11 end12 class OtherWidget13 extend HTTPClient::IncludeClient14 include_http_client15 include_http_client(:method_name => :other_http_client)16 end17 18 class UnrelatedBlankClass ; end19 def test_client_class_level_singleton20 assert_equal Widget.http_client.object_id, Widget.http_client.object_id21 assert_equal Widget.http_client.object_id, Widget.new.http_client.object_id22 assert_not_equal Widget.http_client.object_id, OtherWidget.http_client.object_id23 end24 def test_configured25 assert_equal Widget.http_client.agent_name, "iMonkey 4k"26 assert_nil Widget.http_client.cookie_manager27 assert_equal Widget.http_client.proxy.to_s, "http://example.com"28 end29 30 def test_two_includes31 assert_not_equal OtherWidget.http_client.object_id, OtherWidget.other_http_client.object_id32 33 assert_equal OtherWidget.other_http_client.object_id, OtherWidget.new.other_http_client.object_id 34 end35 36 # meta-programming gone wrong sometimes accidentally37 # adds the class method to _everyone_, a mistake we've made before. 38 def test_not_infected_class_hieararchy39 assert ! Class.respond_to?(:http_client)40 assert ! UnrelatedBlankClass.respond_to?(:http_client)41 end42end...

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1puts Http.client('http://www.google.com/')2puts Http.client('http://www.yahoo.com/')3puts Http.client('http://www.bing.com/')4puts Http.client('http://www.bing.com/')5puts Http.client('http://www.bing.com/')6puts Http.client('http://www.bing.com/')7puts Http.client('http://www.bing.com/')8puts Http.client('http://www.bing.com/')9puts Http.client('http://www.bing.com/')10puts Http.client('http://www.bing.com/')11puts Http.client('http://www.bing.com/')12puts Http.client('http://www.bing.com/')13puts Http.client('http://www.bing.com/')14puts Http.client('http://www.bing.com/')15puts Http.client('http://www.bing.com/')

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1Finished in 0.00116 seconds (files took 0.08652 seconds to load)2Finished in 0.00116 seconds (files took 0.08652 seconds to load)3Failure/Error: it { should validate_presence_of(:first_name) }4 wrong number of arguments (given 1, expected 0)

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1response = Http.get('http://localhost:3000/api/v1/entries')2response = JSON.parse(response.body)3response = Http.post('http://localhost:3000/api/v1/entries', { title: 'New Entry', content: 'New content' })4response = JSON.parse(response.body)5response = Http.put('http://localhost:3000/api/v1/entries/1', { title: 'Updated Entry', content: 'Updated content' })6response = JSON.parse(response.body)7response = Http.delete('http://localhost:3000/api/v1/entries/1')8response = JSON.parse(response.body)9response = Http.get('http://localhost:3000/api/v1/entries/1')10response = JSON.parse(response.body)11response = Http.get('http://localhost:3000/api/v1/entries/1/comments')12response = JSON.parse(response.body)13response = Http.post('http://localhost:3000/api/v1/entries/1/comments', { content: 'New Comment' })14response = JSON.parse(response.body)15response = Http.put('http://localhost:3000/api/v1/entries/1/comments/1', { content: 'Updated Comment' })16response = JSON.parse(response.body)17response = Http.delete('http://localhost:3000/api/v18 def client(adapter)19 def client(adapter)

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1puts Http.client('http://www.google.com/')2puts Http.client('http://www.yahoo.com/')3puts Http.client('http://www.bing.com/')4puts Http.client('http://www.bing.com/')5puts Http.client('http://www.bing.com/')6puts Http.client('http://www.bing.com/')7puts Http.client('http://www.bing.com/')8puts Http.client('http://www.bing.com/')9puts Http.client('http://www.bing.com/')10puts Http.client('http://www.bing.com/')11puts Http.client('http://www.bing.com/')12puts Http.client('http://www.bing.com/')13puts Http.client('http://www.bing.com/')14puts Http.client('http://www.bing.com/')15puts Http.client('http://www.bing.com/')

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1Finished in 0.00116 seconds (files took 0.08652 seconds to load)2Finished in 0.00116 seconds (files took 0.08652 seconds to load)3Failure/Error: it { should validate_presence_of(:first_name) }4 wrong number of arguments (given 1, expected 0)

Full Screen

Full Screen

client

Using AI Code Generation

copy

Full Screen

1response = http.client.get('http://www.google.com')2Finished in 0.00116 seconds (files took 0.08652 seconds to load)3Failure/Error: it { should validate_presence_of(:first_name) }4 wrong number of arguments (given 1, expected 0)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful