How to use http class

Best Atoum code snippet using http

test_http.rb

Source:test_http.rb Github

copy

Full Screen

1# coding: US-ASCII2# frozen_string_literal: false3require 'test/unit'4require 'net/http'5require 'stringio'6require_relative 'utils'7class TestNetHTTP < Test::Unit::TestCase8 def test_class_Proxy9 no_proxy_class = Net::HTTP.Proxy nil10 assert_equal Net::HTTP, no_proxy_class11 proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'12 assert_not_equal Net::HTTP, proxy_class13 assert_operator proxy_class, :<, Net::HTTP14 assert_equal 'proxy.example', proxy_class.proxy_address15 assert_equal 8000, proxy_class.proxy_port16 assert_equal 'user', proxy_class.proxy_user17 assert_equal 'pass', proxy_class.proxy_pass18 http = proxy_class.new 'hostname.example'19 assert_not_predicate http, :proxy_from_env?20 proxy_class = Net::HTTP.Proxy 'proxy.example'21 assert_equal 'proxy.example', proxy_class.proxy_address22 assert_equal 80, proxy_class.proxy_port23 end24 def test_class_Proxy_from_ENV25 clean_http_proxy_env do26 ENV['http_proxy'] = 'http://proxy.example:8000'27 # These are ignored on purpose. See Bug 4388 and Feature 654628 ENV['http_proxy_user'] = 'user'29 ENV['http_proxy_pass'] = 'pass'30 proxy_class = Net::HTTP.Proxy :ENV31 assert_not_equal Net::HTTP, proxy_class32 assert_operator proxy_class, :<, Net::HTTP33 assert_nil proxy_class.proxy_address34 assert_nil proxy_class.proxy_user35 assert_nil proxy_class.proxy_pass36 assert_not_equal 8000, proxy_class.proxy_port37 http = proxy_class.new 'hostname.example'38 assert http.proxy_from_env?39 end40 end41 def test_addr_port42 http = Net::HTTP.new 'hostname.example', nil, nil43 addr_port = http.__send__ :addr_port44 assert_equal 'hostname.example', addr_port45 http.use_ssl = true46 addr_port = http.__send__ :addr_port47 assert_equal 'hostname.example:80', addr_port48 http = Net::HTTP.new '203.0.113.1', nil, nil49 addr_port = http.__send__ :addr_port50 assert_equal '203.0.113.1', addr_port51 http.use_ssl = true52 addr_port = http.__send__ :addr_port53 assert_equal '203.0.113.1:80', addr_port54 http = Net::HTTP.new '2001:db8::1', nil, nil55 addr_port = http.__send__ :addr_port56 assert_equal '[2001:db8::1]', addr_port57 http.use_ssl = true58 addr_port = http.__send__ :addr_port59 assert_equal '[2001:db8::1]:80', addr_port60 end61 def test_edit_path62 http = Net::HTTP.new 'hostname.example', nil, nil63 edited = http.send :edit_path, '/path'64 assert_equal '/path', edited65 http.use_ssl = true66 edited = http.send :edit_path, '/path'67 assert_equal '/path', edited68 end69 def test_edit_path_proxy70 http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'71 edited = http.send :edit_path, '/path'72 assert_equal 'http://hostname.example/path', edited73 http.use_ssl = true74 edited = http.send :edit_path, '/path'75 assert_equal '/path', edited76 end77 def test_proxy_address78 clean_http_proxy_env do79 http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'80 assert_equal 'proxy.example', http.proxy_address81 http = Net::HTTP.new 'hostname.example', nil82 assert_equal nil, http.proxy_address83 end84 end85 def test_proxy_address_no_proxy86 clean_http_proxy_env do87 http = Net::HTTP.new 'hostname.example', nil, 'proxy.example', nil, nil, nil, 'example'88 assert_nil http.proxy_address89 http = Net::HTTP.new '10.224.1.1', nil, 'proxy.example', nil, nil, nil, 'example,10.224.0.0/22'90 assert_nil http.proxy_address91 end92 end93 def test_proxy_from_env_ENV94 clean_http_proxy_env do95 ENV['http_proxy'] = 'http://proxy.example:8000'96 assert_equal false, Net::HTTP.proxy_class?97 http = Net::HTTP.new 'hostname.example'98 assert_equal true, http.proxy_from_env?99 end100 end101 def test_proxy_address_ENV102 clean_http_proxy_env do103 ENV['http_proxy'] = 'http://proxy.example:8000'104 http = Net::HTTP.new 'hostname.example'105 assert_equal 'proxy.example', http.proxy_address106 end107 end108 def test_proxy_eh_no_proxy109 clean_http_proxy_env do110 assert_equal false, Net::HTTP.new('hostname.example', nil, nil).proxy?111 end112 end113 def test_proxy_eh_ENV114 clean_http_proxy_env do115 ENV['http_proxy'] = 'http://proxy.example:8000'116 http = Net::HTTP.new 'hostname.example'117 assert_equal true, http.proxy?118 end119 end120 def test_proxy_eh_ENV_with_user121 clean_http_proxy_env do122 ENV['http_proxy'] = 'http://foo:bar@proxy.example:8000'123 http = Net::HTTP.new 'hostname.example'124 assert_equal true, http.proxy?125 if Net::HTTP::ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE126 assert_equal 'foo', http.proxy_user127 assert_equal 'bar', http.proxy_pass128 else129 assert_nil http.proxy_user130 assert_nil http.proxy_pass131 end132 end133 end134 def test_proxy_eh_ENV_none_set135 clean_http_proxy_env do136 assert_equal false, Net::HTTP.new('hostname.example').proxy?137 end138 end139 def test_proxy_eh_ENV_no_proxy140 clean_http_proxy_env do141 ENV['http_proxy'] = 'http://proxy.example:8000'142 ENV['no_proxy'] = 'hostname.example'143 assert_equal false, Net::HTTP.new('hostname.example').proxy?144 end145 end146 def test_proxy_port147 clean_http_proxy_env do148 http = Net::HTTP.new 'example', nil, 'proxy.example'149 assert_equal 'proxy.example', http.proxy_address150 assert_equal 80, http.proxy_port151 http = Net::HTTP.new 'example', nil, 'proxy.example', 8000152 assert_equal 8000, http.proxy_port153 http = Net::HTTP.new 'example', nil154 assert_equal nil, http.proxy_port155 end156 end157 def test_proxy_port_ENV158 clean_http_proxy_env do159 ENV['http_proxy'] = 'http://proxy.example:8000'160 http = Net::HTTP.new 'hostname.example'161 assert_equal 8000, http.proxy_port162 end163 end164 def test_newobj165 clean_http_proxy_env do166 ENV['http_proxy'] = 'http://proxy.example:8000'167 http = Net::HTTP.newobj 'hostname.example'168 assert_equal false, http.proxy?169 end170 end171 def clean_http_proxy_env172 orig = {173 'http_proxy' => ENV['http_proxy'],174 'http_proxy_user' => ENV['http_proxy_user'],175 'http_proxy_pass' => ENV['http_proxy_pass'],176 'no_proxy' => ENV['no_proxy'],177 }178 orig.each_key do |key|179 ENV.delete key180 end181 yield182 ensure183 orig.each do |key, value|184 ENV[key] = value185 end186 end187 def test_failure_message_includes_failed_domain_and_port188 # hostname to be included in the error message189 host = Struct.new(:to_s).new("<example>")190 port = 2119191 # hack to let TCPSocket.open fail192 def host.to_str; raise SocketError, "open failure"; end193 uri = Struct.new(:scheme, :hostname, :port).new("http", host, port)194 assert_raise_with_message(SocketError, /#{host}:#{port}/) do195 clean_http_proxy_env{ Net::HTTP.get(uri) }196 end197 end198end199module TestNetHTTP_version_1_1_methods200 def test_s_start201 begin202 h = Net::HTTP.start(config('host'), config('port'))203 ensure204 h&.finish205 end206 assert_equal config('host'), h.address207 assert_equal config('port'), h.port208 assert_equal true, h.instance_variable_get(:@proxy_from_env)209 begin210 h = Net::HTTP.start(config('host'), config('port'), :ENV)211 ensure212 h&.finish213 end214 assert_equal config('host'), h.address215 assert_equal config('port'), h.port216 assert_equal true, h.instance_variable_get(:@proxy_from_env)217 begin218 h = Net::HTTP.start(config('host'), config('port'), nil)219 ensure220 h&.finish221 end222 assert_equal config('host'), h.address223 assert_equal config('port'), h.port224 assert_equal false, h.instance_variable_get(:@proxy_from_env)225 end226 def test_s_get227 assert_equal $test_net_http_data,228 Net::HTTP.get(config('host'), '/', config('port'))229 end230 def test_head231 start {|http|232 res = http.head('/')233 assert_kind_of Net::HTTPResponse, res234 assert_equal $test_net_http_data_type, res['Content-Type']235 unless self.is_a?(TestNetHTTP_v1_2_chunked)236 assert_equal $test_net_http_data.size, res['Content-Length'].to_i237 end238 }239 end240 def test_get241 start {|http|242 _test_get__get http243 _test_get__iter http244 _test_get__chunked http245 }246 end247 def _test_get__get(http)248 res = http.get('/')249 assert_kind_of Net::HTTPResponse, res250 assert_kind_of String, res.body251 unless self.is_a?(TestNetHTTP_v1_2_chunked)252 assert_not_nil res['content-length']253 assert_equal $test_net_http_data.size, res['content-length'].to_i254 end255 assert_equal $test_net_http_data_type, res['Content-Type']256 assert_equal $test_net_http_data.size, res.body.size257 assert_equal $test_net_http_data, res.body258 assert_nothing_raised {259 http.get('/', { 'User-Agent' => 'test' }.freeze)260 }261 assert res.decode_content, '[Bug #7924]' if Net::HTTP::HAVE_ZLIB262 end263 def _test_get__iter(http)264 buf = ''265 res = http.get('/') {|s| buf << s }266 assert_kind_of Net::HTTPResponse, res267 # assert_kind_of String, res.body268 unless self.is_a?(TestNetHTTP_v1_2_chunked)269 assert_not_nil res['content-length']270 assert_equal $test_net_http_data.size, res['content-length'].to_i271 end272 assert_equal $test_net_http_data_type, res['Content-Type']273 assert_equal $test_net_http_data.size, buf.size274 assert_equal $test_net_http_data, buf275 # assert_equal $test_net_http_data.size, res.body.size276 # assert_equal $test_net_http_data, res.body277 end278 def _test_get__chunked(http)279 buf = ''280 res = http.get('/') {|s| buf << s }281 assert_kind_of Net::HTTPResponse, res282 # assert_kind_of String, res.body283 unless self.is_a?(TestNetHTTP_v1_2_chunked)284 assert_not_nil res['content-length']285 assert_equal $test_net_http_data.size, res['content-length'].to_i286 end287 assert_equal $test_net_http_data_type, res['Content-Type']288 assert_equal $test_net_http_data.size, buf.size289 assert_equal $test_net_http_data, buf290 # assert_equal $test_net_http_data.size, res.body.size291 # assert_equal $test_net_http_data, res.body292 end293 def test_get__break294 i = 0295 start {|http|296 http.get('/') do |str|297 i += 1298 break299 end300 }301 assert_equal 1, i302 @log_tester = nil # server may encount ECONNRESET303 end304 def test_get__implicit_start305 res = new().get('/')306 assert_kind_of Net::HTTPResponse, res307 assert_kind_of String, res.body308 unless self.is_a?(TestNetHTTP_v1_2_chunked)309 assert_not_nil res['content-length']310 end311 assert_equal $test_net_http_data_type, res['Content-Type']312 assert_equal $test_net_http_data.size, res.body.size313 assert_equal $test_net_http_data, res.body314 end315 def test_get__crlf316 start {|http|317 assert_raise(ArgumentError) do318 http.get("\r")319 end320 assert_raise(ArgumentError) do321 http.get("\n")322 end323 }324 end325 def test_get2326 start {|http|327 http.get2('/') {|res|328 EnvUtil.suppress_warning do329 assert_kind_of Net::HTTPResponse, res330 assert_kind_of Net::HTTPResponse, res.header331 end332 unless self.is_a?(TestNetHTTP_v1_2_chunked)333 assert_not_nil res['content-length']334 end335 assert_equal $test_net_http_data_type, res['Content-Type']336 assert_kind_of String, res.body337 assert_kind_of String, res.entity338 assert_equal $test_net_http_data.size, res.body.size339 assert_equal $test_net_http_data, res.body340 assert_equal $test_net_http_data, res.entity341 }342 }343 end344 def test_post345 start {|http|346 _test_post__base http347 _test_post__file http348 _test_post__no_data http349 }350 end351 def _test_post__base(http)352 uheader = {}353 uheader['Accept'] = 'application/octet-stream'354 uheader['Content-Type'] = 'application/x-www-form-urlencoded'355 data = 'post data'356 res = http.post('/', data, uheader)357 assert_kind_of Net::HTTPResponse, res358 assert_kind_of String, res.body359 assert_equal data, res.body360 assert_equal data, res.entity361 end362 def _test_post__file(http)363 data = 'post data'364 f = StringIO.new365 http.post('/', data, {'content-type' => 'application/x-www-form-urlencoded'}, f)366 assert_equal data, f.string367 end368 def _test_post__no_data(http)369 unless self.is_a?(TestNetHTTP_v1_2_chunked)370 EnvUtil.suppress_warning do371 data = nil372 res = http.post('/', data)373 assert_not_equal '411', res.code374 end375 end376 end377 def test_s_post378 url = "http://#{config('host')}:#{config('port')}/?q=a"379 res = Net::HTTP.post(380 URI.parse(url),381 "a=x")382 assert_equal "application/x-www-form-urlencoded", res["Content-Type"]383 assert_equal "a=x", res.body384 assert_equal url, res["X-request-uri"]385 res = Net::HTTP.post(386 URI.parse(url),387 "hello world",388 "Content-Type" => "text/plain; charset=US-ASCII")389 assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]390 assert_equal "hello world", res.body391 end392 def test_s_post_form393 url = "http://#{config('host')}:#{config('port')}/"394 res = Net::HTTP.post_form(395 URI.parse(url),396 "a" => "x")397 assert_equal ["a=x"], res.body.split(/[;&]/).sort398 res = Net::HTTP.post_form(399 URI.parse(url),400 "a" => "x",401 "b" => "y")402 assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort403 res = Net::HTTP.post_form(404 URI.parse(url),405 "a" => ["x1", "x2"],406 "b" => "y")407 assert_equal url, res['X-request-uri']408 assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort409 res = Net::HTTP.post_form(410 URI.parse(url + '?a=x'),411 "b" => "y")412 assert_equal url + '?a=x', res['X-request-uri']413 assert_equal ["b=y"], res.body.split(/[;&]/).sort414 end415 def test_patch416 start {|http|417 _test_patch__base http418 }419 end420 def _test_patch__base(http)421 uheader = {}422 uheader['Accept'] = 'application/octet-stream'423 uheader['Content-Type'] = 'application/x-www-form-urlencoded'424 data = 'patch data'425 res = http.patch('/', data, uheader)426 assert_kind_of Net::HTTPResponse, res427 assert_kind_of String, res.body428 assert_equal data, res.body429 assert_equal data, res.entity430 end431 def test_timeout_during_HTTP_session432 bug4246 = "expected the HTTP session to have timed out but have not. c.f. [ruby-core:34203]"433 th = nil434 # listen for connections... but deliberately do not read435 TCPServer.open('localhost', 0) {|server|436 port = server.addr[1]437 conn = Net::HTTP.new('localhost', port)438 conn.read_timeout = 0.01439 conn.open_timeout = 0.1440 th = Thread.new do441 assert_raise(Net::ReadTimeout) {442 conn.get('/')443 }444 end445 assert th.join(10), bug4246446 }447 ensure448 th.kill449 th.join450 end451end452module TestNetHTTP_version_1_2_methods453 def test_request454 start {|http|455 _test_request__GET http456 _test_request__accept_encoding http457 _test_request__file http458 # _test_request__range http # WEBrick does not support Range: header.459 _test_request__HEAD http460 _test_request__POST http461 _test_request__stream_body http462 _test_request__uri http463 _test_request__uri_host http464 }465 end466 def _test_request__GET(http)467 req = Net::HTTP::Get.new('/')468 http.request(req) {|res|469 assert_kind_of Net::HTTPResponse, res470 assert_kind_of String, res.body471 unless self.is_a?(TestNetHTTP_v1_2_chunked)472 assert_not_nil res['content-length']473 assert_equal $test_net_http_data.size, res['content-length'].to_i474 end475 assert_equal $test_net_http_data.size, res.body.size476 assert_equal $test_net_http_data, res.body477 assert res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB478 }479 end480 def _test_request__accept_encoding(http)481 req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate')482 http.request(req) {|res|483 assert_kind_of Net::HTTPResponse, res484 assert_kind_of String, res.body485 unless self.is_a?(TestNetHTTP_v1_2_chunked)486 assert_not_nil res['content-length']487 assert_equal $test_net_http_data.size, res['content-length'].to_i488 end489 assert_equal $test_net_http_data.size, res.body.size490 assert_equal $test_net_http_data, res.body491 assert_not_predicate res, :decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB492 }493 end494 def _test_request__file(http)495 req = Net::HTTP::Get.new('/')496 http.request(req) {|res|497 assert_kind_of Net::HTTPResponse, res498 unless self.is_a?(TestNetHTTP_v1_2_chunked)499 assert_not_nil res['content-length']500 assert_equal $test_net_http_data.size, res['content-length'].to_i501 end502 f = StringIO.new("".force_encoding("ASCII-8BIT"))503 res.read_body f504 assert_equal $test_net_http_data.bytesize, f.string.bytesize505 assert_equal $test_net_http_data.encoding, f.string.encoding506 assert_equal $test_net_http_data, f.string507 }508 end509 def _test_request__range(http)510 req = Net::HTTP::Get.new('/')511 req['range'] = 'bytes=0-5'512 assert_equal $test_net_http_data[0,6], http.request(req).body513 end514 def _test_request__HEAD(http)515 req = Net::HTTP::Head.new('/')516 http.request(req) {|res|517 assert_kind_of Net::HTTPResponse, res518 unless self.is_a?(TestNetHTTP_v1_2_chunked)519 assert_not_nil res['content-length']520 assert_equal $test_net_http_data.size, res['content-length'].to_i521 end522 assert_nil res.body523 }524 end525 def _test_request__POST(http)526 data = 'post data'527 req = Net::HTTP::Post.new('/')528 req['Accept'] = $test_net_http_data_type529 req['Content-Type'] = 'application/x-www-form-urlencoded'530 http.request(req, data) {|res|531 assert_kind_of Net::HTTPResponse, res532 unless self.is_a?(TestNetHTTP_v1_2_chunked)533 assert_equal data.size, res['content-length'].to_i534 end535 assert_kind_of String, res.body536 assert_equal data, res.body537 }538 end539 def _test_request__stream_body(http)540 req = Net::HTTP::Post.new('/')541 data = $test_net_http_data542 req.content_length = data.size543 req['Content-Type'] = 'application/x-www-form-urlencoded'544 req.body_stream = StringIO.new(data)545 res = http.request(req)546 assert_kind_of Net::HTTPResponse, res547 assert_kind_of String, res.body548 assert_equal data.size, res.body.size549 assert_equal data, res.body550 end551 def _test_request__path(http)552 uri = URI 'https://hostname.example/'553 req = Net::HTTP::Get.new('/')554 res = http.request(req)555 assert_kind_of URI::Generic, req.uri556 assert_not_equal uri, req.uri557 assert_equal uri, res.uri558 assert_not_same uri, req.uri559 assert_not_same req.uri, res.uri560 end561 def _test_request__uri(http)562 uri = URI 'https://hostname.example/'563 req = Net::HTTP::Get.new(uri)564 res = http.request(req)565 assert_kind_of URI::Generic, req.uri566 assert_not_equal uri, req.uri567 assert_equal req.uri, res.uri568 assert_not_same uri, req.uri569 assert_not_same req.uri, res.uri570 end571 def _test_request__uri_host(http)572 uri = URI 'http://other.example/'573 req = Net::HTTP::Get.new(uri)574 req['host'] = 'hostname.example'575 res = http.request(req)576 assert_kind_of URI::Generic, req.uri577 assert_equal URI("http://hostname.example:#{http.port}"), res.uri578 end579 def test_send_request580 start {|http|581 _test_send_request__GET http582 _test_send_request__HEAD http583 _test_send_request__POST http584 }585 end586 def _test_send_request__GET(http)587 res = http.send_request('GET', '/')588 assert_kind_of Net::HTTPResponse, res589 unless self.is_a?(TestNetHTTP_v1_2_chunked)590 assert_equal $test_net_http_data.size, res['content-length'].to_i591 end592 assert_kind_of String, res.body593 assert_equal $test_net_http_data, res.body594 end595 def _test_send_request__HEAD(http)596 res = http.send_request('HEAD', '/')597 assert_kind_of Net::HTTPResponse, res598 unless self.is_a?(TestNetHTTP_v1_2_chunked)599 assert_not_nil res['content-length']600 assert_equal $test_net_http_data.size, res['content-length'].to_i601 end602 assert_nil res.body603 end604 def _test_send_request__POST(http)605 data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'606 res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded')607 assert_kind_of Net::HTTPResponse, res608 assert_kind_of String, res.body609 assert_equal data.size, res.body.size610 assert_equal data, res.body611 end612 def test_set_form613 require 'tempfile'614 Tempfile.create('ruby-test') {|file|615 file << "\u{30c7}\u{30fc}\u{30bf}"616 data = [617 ['name', 'Gonbei Nanashi'],618 ['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"],619 ['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")],620 ["file", file, filename: "ruby-test"]621 ]622 expected = <<"__EOM__".gsub(/\n/, "\r\n")623--<boundary>624Content-Disposition: form-data; name="name"625Gonbei Nanashi626--<boundary>627Content-Disposition: form-data; name="name"628\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B629--<boundary>630Content-Disposition: form-data; name="s\\"i\\\\o"631\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B632--<boundary>633Content-Disposition: form-data; name="file"; filename="ruby-test"634Content-Type: application/octet-stream635\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF636--<boundary>--637__EOM__638 start {|http|639 _test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})640 _test_set_form_multipart(http, false, data, expected)641 _test_set_form_multipart(http, true, data, expected)642 }643 }644 end645 def _test_set_form_urlencoded(http, data)646 req = Net::HTTP::Post.new('/')647 req.set_form(data)648 res = http.request req649 assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body650 end651 def _test_set_form_multipart(http, chunked_p, data, expected)652 data.each{|k,v|v.rewind rescue nil}653 req = Net::HTTP::Post.new('/')654 req.set_form(data, 'multipart/form-data')655 req['Transfer-Encoding'] = 'chunked' if chunked_p656 res = http.request req657 body = res.body658 assert_match(/\A--(?<boundary>\S+)/, body)659 /\A--(?<boundary>\S+)/ =~ body660 expected = expected.gsub(/<boundary>/, boundary)661 assert_equal(expected, body)662 end663 def test_set_form_with_file664 require 'tempfile'665 Tempfile.create('ruby-test') {|file|666 file.binmode667 file << $test_net_http_data668 filename = File.basename(file.to_path)669 data = [['file', file]]670 expected = <<"__EOM__".gsub(/\n/, "\r\n")671--<boundary>672Content-Disposition: form-data; name="file"; filename="<filename>"673Content-Type: application/octet-stream674<data>675--<boundary>--676__EOM__677 expected.sub!(/<filename>/, filename)678 expected.sub!(/<data>/, $test_net_http_data)679 start {|http|680 data.each{|k,v|v.rewind rescue nil}681 req = Net::HTTP::Post.new('/')682 req.set_form(data, 'multipart/form-data')683 res = http.request req684 body = res.body685 header, _ = body.split(/\r\n\r\n/, 2)686 assert_match(/\A--(?<boundary>\S+)/, body)687 /\A--(?<boundary>\S+)/ =~ body688 expected = expected.gsub(/<boundary>/, boundary)689 assert_match(/^--(?<boundary>\S+)\r\n/, header)690 assert_match(691 /^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,692 header)693 assert_equal(expected, body)694 data.each{|k,v|v.rewind rescue nil}695 req['Transfer-Encoding'] = 'chunked'696 res = http.request req697 #assert_equal(expected, res.body)698 }699 }700 end701end702class TestNetHTTP_v1_2 < Test::Unit::TestCase703 CONFIG = {704 'host' => '127.0.0.1',705 'proxy_host' => nil,706 'proxy_port' => nil,707 }708 include TestNetHTTPUtils709 include TestNetHTTP_version_1_1_methods710 include TestNetHTTP_version_1_2_methods711 def new712 Net::HTTP.version_1_2713 super714 end715end716class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase717 CONFIG = {718 'host' => '127.0.0.1',719 'proxy_host' => nil,720 'proxy_port' => nil,721 'chunked' => true,722 }723 include TestNetHTTPUtils724 include TestNetHTTP_version_1_1_methods725 include TestNetHTTP_version_1_2_methods726 def new727 Net::HTTP.version_1_2728 super729 end730 def test_chunked_break731 assert_nothing_raised("[ruby-core:29229]") {732 start {|http|733 http.request_get('/') {|res|734 res.read_body {|chunk|735 break736 }737 }738 }739 }740 end741end742class TestNetHTTPContinue < Test::Unit::TestCase743 CONFIG = {744 'host' => '127.0.0.1',745 'proxy_host' => nil,746 'proxy_port' => nil,747 'chunked' => true,748 }749 include TestNetHTTPUtils750 def logfile751 @debug = StringIO.new('')752 end753 def mount_proc(&block)754 @server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))755 end756 def test_expect_continue757 mount_proc {|req, res|758 req.continue759 res.body = req.query['body']760 }761 start {|http|762 uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}763 http.continue_timeout = 0.2764 http.request_post('/continue', 'body=BODY', uheader) {|res|765 assert_equal('BODY', res.read_body)766 }767 }768 assert_match(/Expect: 100-continue/, @debug.string)769 assert_match(/HTTP\/1.1 100 continue/, @debug.string)770 end771 def test_expect_continue_timeout772 mount_proc {|req, res|773 sleep 0.2774 req.continue # just ignored because it's '100'775 res.body = req.query['body']776 }777 start {|http|778 uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}779 http.continue_timeout = 0780 http.request_post('/continue', 'body=BODY', uheader) {|res|781 assert_equal('BODY', res.read_body)782 }783 }784 assert_match(/Expect: 100-continue/, @debug.string)785 assert_match(/HTTP\/1.1 100 continue/, @debug.string)786 end787 def test_expect_continue_error788 mount_proc {|req, res|789 res.status = 501790 res.body = req.query['body']791 }792 start {|http|793 uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}794 http.continue_timeout = 0795 http.request_post('/continue', 'body=ERROR', uheader) {|res|796 assert_equal('ERROR', res.read_body)797 }798 }799 assert_match(/Expect: 100-continue/, @debug.string)800 assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)801 end802 def test_expect_continue_error_before_body803 @log_tester = nil804 mount_proc {|req, res|805 raise WEBrick::HTTPStatus::Forbidden806 }807 start {|http|808 uheader = {'content-length' => '5', 'expect' => '100-continue'}809 http.continue_timeout = 1 # allow the server to respond before sending810 http.request_post('/continue', 'data', uheader) {|res|811 assert_equal(res.code, '403')812 }813 }814 assert_match(/Expect: 100-continue/, @debug.string)815 assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)816 end817 def test_expect_continue_error_while_waiting818 mount_proc {|req, res|819 res.status = 501820 res.body = req.query['body']821 }822 start {|http|823 uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}824 http.continue_timeout = 0.5825 http.request_post('/continue', 'body=ERROR', uheader) {|res|826 assert_equal('ERROR', res.read_body)827 }828 }829 assert_match(/Expect: 100-continue/, @debug.string)830 assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)831 end832end833class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase834 CONFIG = {835 'host' => '127.0.0.1',836 'proxy_host' => nil,837 'proxy_port' => nil,838 'chunked' => true,839 }840 include TestNetHTTPUtils841 def logfile842 @debug = StringIO.new('')843 end844 def mount_proc(&block)845 @server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))846 end847 def test_info848 mount_proc {|req, res|849 req.instance_variable_get(:@socket) << "HTTP/1.1 101 Switching Protocols\r\n\r\n"850 res.body = req.query['body']851 }852 start {|http|853 http.continue_timeout = 0.2854 http.request_post('/continue', 'body=BODY') {|res|855 assert_equal('BODY', res.read_body)856 }857 }858 assert_match(/HTTP\/1.1 101 Switching Protocols/, @debug.string)859 end860end861class TestNetHTTPKeepAlive < Test::Unit::TestCase862 CONFIG = {863 'host' => '127.0.0.1',864 'proxy_host' => nil,865 'proxy_port' => nil,866 'RequestTimeout' => 1,867 }868 include TestNetHTTPUtils869 def test_keep_alive_get_auto_reconnect870 start {|http|871 res = http.get('/')872 http.keep_alive_timeout = 1873 assert_kind_of Net::HTTPResponse, res874 assert_kind_of String, res.body875 sleep 1.5876 assert_nothing_raised {877 res = http.get('/')878 }879 assert_kind_of Net::HTTPResponse, res880 assert_kind_of String, res.body881 }882 end883 def test_server_closed_connection_auto_reconnect884 start {|http|885 res = http.get('/')886 http.keep_alive_timeout = 5887 assert_kind_of Net::HTTPResponse, res888 assert_kind_of String, res.body889 sleep 1.5890 assert_nothing_raised {891 # Net::HTTP should detect the closed connection before attempting the892 # request, since post requests cannot be retried.893 res = http.post('/', 'query=foo', 'content-type' => 'application/x-www-form-urlencoded')894 }895 assert_kind_of Net::HTTPResponse, res896 assert_kind_of String, res.body897 }898 end899 def test_keep_alive_get_auto_retry900 start {|http|901 res = http.get('/')902 http.keep_alive_timeout = 5903 assert_kind_of Net::HTTPResponse, res904 assert_kind_of String, res.body905 sleep 1.5906 res = http.get('/')907 assert_kind_of Net::HTTPResponse, res908 assert_kind_of String, res.body909 }910 end911 class MockSocket912 attr_reader :count913 def initialize(success_after: nil)914 @success_after = success_after915 @count = 0916 end917 def close918 end919 def closed?920 end921 def write(_)922 end923 def readline924 @count += 1925 if @success_after && @success_after <= @count926 "HTTP/1.1 200 OK"927 else928 raise Errno::ECONNRESET929 end930 end931 def readuntil(*_)932 ""933 end934 def read_all(_)935 end936 end937 def test_http_retry_success938 start {|http|939 socket = MockSocket.new(success_after: 10)940 http.instance_variable_get(:@socket).close941 http.instance_variable_set(:@socket, socket)942 assert_equal 0, socket.count943 http.max_retries = 10944 res = http.get('/')945 assert_equal 10, socket.count946 assert_kind_of Net::HTTPResponse, res947 assert_kind_of String, res.body948 }949 end950 def test_http_retry_failed951 start {|http|952 socket = MockSocket.new953 http.instance_variable_get(:@socket).close954 http.instance_variable_set(:@socket, socket)955 http.max_retries = 10956 assert_raise(Errno::ECONNRESET){ http.get('/') }957 assert_equal 11, socket.count958 }959 end960 def test_keep_alive_server_close961 def @server.run(sock)962 sock.close963 end964 start {|http|965 assert_raise(EOFError, Errno::ECONNRESET, IOError) {966 http.get('/')967 }968 }969 end970end971class TestNetHTTPLocalBind < Test::Unit::TestCase972 CONFIG = {973 'host' => 'localhost',974 'proxy_host' => nil,975 'proxy_port' => nil,976 }977 include TestNetHTTPUtils978 def test_bind_to_local_host979 @server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip }980 http = Net::HTTP.new(config('host'), config('port'))981 http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address982 assert_not_nil(http.local_host)983 assert_nil(http.local_port)984 res = http.get('/show_ip')985 assert_equal(http.local_host, res.body)986 end987 def test_bind_to_local_port988 @server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s }989 http = Net::HTTP.new(config('host'), config('port'))990 http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address991 http.local_port = Addrinfo.tcp(config('host'), 0).bind {|s|992 s.local_address.ip_port.to_s993 }994 assert_not_nil(http.local_host)995 assert_not_nil(http.local_port)996 res = http.get('/show_port')997 assert_equal(http.local_port, res.body)998 end999end...

Full Screen

Full Screen

test_generic.rb

Source:test_generic.rb Github

copy

Full Screen

1require 'test/unit'2require 'uri'3class URI::TestGeneric < Test::Unit::TestCase4 def setup5 @url = 'http://a/b/c/d;p?q'6 @base_url = URI.parse(@url)7 end8 def teardown9 end10 def uri_to_ary(uri)11 uri.class.component.collect {|c| uri.send(c)}12 end13 def test_parse14 # 015 assert_kind_of(URI::HTTP, @base_url)16 exp = [17 'http',18 nil, 'a', URI::HTTP.default_port,19 '/b/c/d;p',20 'q',21 nil22 ]23 ary = uri_to_ary(@base_url)24 assert_equal(exp, ary)25 # 126 url = URI.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt')27 assert_kind_of(URI::FTP, url)28 exp = [29 'ftp',30 nil, 'ftp.is.co.za', URI::FTP.default_port,31 'rfc/rfc1808.txt', nil,32 ]33 ary = uri_to_ary(url)34 assert_equal(exp, ary)35 # 1'36 url = URI.parse('ftp://ftp.is.co.za/%2Frfc/rfc1808.txt')37 assert_kind_of(URI::FTP, url)38 exp = [39 'ftp',40 nil, 'ftp.is.co.za', URI::FTP.default_port,41 '/rfc/rfc1808.txt', nil,42 ]43 ary = uri_to_ary(url)44 assert_equal(exp, ary)45 # 246 url = URI.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles')47 assert_kind_of(URI::Generic, url)48 exp = [49 'gopher',50 nil, 'spinaltap.micro.umn.edu', nil, nil,51 '/00/Weather/California/Los%20Angeles', nil,52 nil,53 nil54 ]55 ary = uri_to_ary(url)56 assert_equal(exp, ary)57 # 358 url = URI.parse('http://www.math.uio.no/faq/compression-faq/part1.html')59 assert_kind_of(URI::HTTP, url)60 exp = [61 'http',62 nil, 'www.math.uio.no', URI::HTTP.default_port,63 '/faq/compression-faq/part1.html',64 nil,65 nil66 ]67 ary = uri_to_ary(url)68 assert_equal(exp, ary)69 # 470 url = URI.parse('mailto:mduerst@ifi.unizh.ch')71 assert_kind_of(URI::Generic, url)72 exp = [73 'mailto',74 'mduerst@ifi.unizh.ch',75 []76 ]77 ary = uri_to_ary(url)78 assert_equal(exp, ary)79 # 580 url = URI.parse('news:comp.infosystems.www.servers.unix')81 assert_kind_of(URI::Generic, url)82 exp = [83 'news',84 nil, nil, nil, nil,85 nil, 'comp.infosystems.www.servers.unix',86 nil,87 nil88 ]89 ary = uri_to_ary(url)90 assert_equal(exp, ary)91 # 692 url = URI.parse('telnet://melvyl.ucop.edu/')93 assert_kind_of(URI::Generic, url)94 exp = [95 'telnet',96 nil, 'melvyl.ucop.edu', nil, nil,97 '/', nil,98 nil,99 nil100 ]101 ary = uri_to_ary(url)102 assert_equal(exp, ary)103 # 7104 # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>105 assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b:80/') }106 assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b/') }107 # 8108 # reported by m_seki109 url = URI.parse('file:///foo/bar.txt')110 assert_kind_of(URI::Generic, url)111 url = URI.parse('file:/foo/bar.txt')112 assert_kind_of(URI::Generic, url)113 # 9114 url = URI.parse('ftp://:pass@localhost/')115 assert_equal('', url.user, "[ruby-dev:25667]")116 assert_equal('pass', url.password)117 assert_equal(':pass', url.userinfo, "[ruby-dev:25667]")118 url = URI.parse('ftp://user@localhost/')119 assert_equal('user', url.user)120 assert_equal(nil, url.password)121 assert_equal('user', url.userinfo)122 url = URI.parse('ftp://localhost/')123 assert_equal(nil, url.user)124 assert_equal(nil, url.password)125 assert_equal(nil, url.userinfo)126 end127 def test_merge128 u1 = URI.parse('http://foo')129 u2 = URI.parse('http://foo/')130 u3 = URI.parse('http://foo/bar')131 u4 = URI.parse('http://foo/bar/')132 {133 u1 => {134 'baz' => 'http://foo/baz',135 '/baz' => 'http://foo/baz',136 },137 u2 => {138 'baz' => 'http://foo/baz',139 '/baz' => 'http://foo/baz',140 },141 u3 => {142 'baz' => 'http://foo/baz',143 '/baz' => 'http://foo/baz',144 },145 u4 => {146 'baz' => 'http://foo/bar/baz',147 '/baz' => 'http://foo/baz',148 },149 }.each { |base, map|150 map.each { |url, result|151 expected = URI.parse(result)152 uri = URI.parse(url)153 assert_equal expected, base + url, "<#{base}> + #{url.inspect} to become <#{expected}>"154 assert_equal expected, base + uri, "<#{base}> + <#{uri}> to become <#{expected}>"155 }156 }157 url = URI.parse('http://hoge/a.html') + 'b.html'158 assert_equal('http://hoge/b.html', url.to_s, "[ruby-dev:11508]")159 # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>160 url = URI.parse('http://a/b') + 'http://x/y'161 assert_equal('http://x/y', url.to_s)162 assert_equal(url, URI.parse('') + 'http://x/y')163 assert_equal(url, URI.parse('').normalize + 'http://x/y')164 assert_equal(url, URI.parse('http://a/b').normalize + 'http://x/y')165 u = URI.parse('http://foo/bar/baz')166 assert_equal(nil, u.merge!(""))167 assert_equal(nil, u.merge!(u))168 assert(nil != u.merge!("."))169 assert_equal('http://foo/bar/', u.to_s)170 assert(nil != u.merge!("../baz"))171 assert_equal('http://foo/baz', u.to_s)172 u0 = URI.parse('mailto:foo@example.com')173 u1 = URI.parse('mailto:foo@example.com#bar')174 assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")175 u0 = URI.parse('http://www.example.com/')176 u1 = URI.parse('http://www.example.com/foo/..') + './'177 assert_equal(u0, u1, "[ruby-list:39838]")178 u0 = URI.parse('http://www.example.com/foo/')179 u1 = URI.parse('http://www.example.com/foo/bar/..') + './'180 assert_equal(u0, u1)181 u0 = URI.parse('http://www.example.com/foo/bar/')182 u1 = URI.parse('http://www.example.com/foo/bar/baz/..') + './'183 assert_equal(u0, u1)184 u0 = URI.parse('http://www.example.com/')185 u1 = URI.parse('http://www.example.com/foo/bar/../..') + './'186 assert_equal(u0, u1)187 u0 = URI.parse('http://www.example.com/foo/')188 u1 = URI.parse('http://www.example.com/foo/bar/baz/../..') + './'189 assert_equal(u0, u1)190 u = URI.parse('http://www.example.com/')191 u0 = u + './foo/'192 u1 = u + './foo/bar/..'193 assert_equal(u0, u1, "[ruby-list:39844]")194 u = URI.parse('http://www.example.com/')195 u0 = u + './'196 u1 = u + './foo/bar/../..'197 assert_equal(u0, u1)198 end199 def test_route200 url = URI.parse('http://hoge/a.html').route_to('http://hoge/b.html')201 assert_equal('b.html', url.to_s)202 url = URI.parse('http://hoge/a/').route_to('http://hoge/b/')203 assert_equal('../b/', url.to_s)204 url = URI.parse('http://hoge/a/b').route_to('http://hoge/b/')205 assert_equal('../b/', url.to_s)206 url = URI.parse('http://hoge/a/b/').route_to('http://hoge/b/')207 assert_equal('../../b/', url.to_s)208 url = URI.parse('http://hoge/a/b/').route_to('http://HOGE/b/')209 assert_equal('../../b/', url.to_s)210 url = URI.parse('http://hoge/a/b/').route_to('http://MOGE/b/')211 assert_equal('//MOGE/b/', url.to_s)212 url = URI.parse('http://hoge/b').route_to('http://hoge/b/')213 assert_equal('b/', url.to_s)214 url = URI.parse('http://hoge/b/a').route_to('http://hoge/b/')215 assert_equal('./', url.to_s)216 url = URI.parse('http://hoge/b/').route_to('http://hoge/b')217 assert_equal('../b', url.to_s)218 url = URI.parse('http://hoge/b').route_to('http://hoge/b:c')219 assert_equal('./b:c', url.to_s)220 url = URI.parse('file:///a/b/').route_to('file:///a/b/')221 assert_equal('', url.to_s)222 url = URI.parse('file:///a/b/').route_to('file:///a/b')223 assert_equal('../b', url.to_s)224 url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar')225 assert_equal('#bar', url.to_s)226 url = URI.parse('mailto:foo@example.com#bar').route_to('mailto:foo@example.com')227 assert_equal('', url.to_s)228 url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com')229 assert_equal('', url.to_s)230 end231 def test_rfc3986_examples232# http://a/b/c/d;p?q233# g:h = g:h234 url = @base_url.merge('g:h')235 assert_kind_of(URI::Generic, url)236 assert_equal('g:h', url.to_s)237 url = @base_url.route_to('g:h')238 assert_kind_of(URI::Generic, url)239 assert_equal('g:h', url.to_s)240# http://a/b/c/d;p?q241# g = http://a/b/c/g242 url = @base_url.merge('g')243 assert_kind_of(URI::HTTP, url)244 assert_equal('http://a/b/c/g', url.to_s)245 url = @base_url.route_to('http://a/b/c/g')246 assert_kind_of(URI::Generic, url)247 assert_equal('g', url.to_s)248# http://a/b/c/d;p?q249# ./g = http://a/b/c/g250 url = @base_url.merge('./g')251 assert_kind_of(URI::HTTP, url)252 assert_equal('http://a/b/c/g', url.to_s)253 url = @base_url.route_to('http://a/b/c/g')254 assert_kind_of(URI::Generic, url)255 assert('./g' != url.to_s) # ok256 assert_equal('g', url.to_s)257# http://a/b/c/d;p?q258# g/ = http://a/b/c/g/259 url = @base_url.merge('g/')260 assert_kind_of(URI::HTTP, url)261 assert_equal('http://a/b/c/g/', url.to_s)262 url = @base_url.route_to('http://a/b/c/g/')263 assert_kind_of(URI::Generic, url)264 assert_equal('g/', url.to_s)265# http://a/b/c/d;p?q266# /g = http://a/g267 url = @base_url.merge('/g')268 assert_kind_of(URI::HTTP, url)269 assert_equal('http://a/g', url.to_s)270 url = @base_url.route_to('http://a/g')271 assert_kind_of(URI::Generic, url)272 assert('/g' != url.to_s) # ok273 assert_equal('../../g', url.to_s)274# http://a/b/c/d;p?q275# //g = http://g276 url = @base_url.merge('//g')277 assert_kind_of(URI::HTTP, url)278 assert_equal('http://g', url.to_s)279 url = @base_url.route_to('http://g')280 assert_kind_of(URI::Generic, url)281 assert_equal('//g', url.to_s)282# http://a/b/c/d;p?q283# ?y = http://a/b/c/d;p?y284 url = @base_url.merge('?y')285 assert_kind_of(URI::HTTP, url)286 assert_equal('http://a/b/c/d;p?y', url.to_s)287 url = @base_url.route_to('http://a/b/c/d;p?y')288 assert_kind_of(URI::Generic, url)289 assert_equal('?y', url.to_s)290# http://a/b/c/d;p?q291# g?y = http://a/b/c/g?y292 url = @base_url.merge('g?y')293 assert_kind_of(URI::HTTP, url)294 assert_equal('http://a/b/c/g?y', url.to_s)295 url = @base_url.route_to('http://a/b/c/g?y')296 assert_kind_of(URI::Generic, url)297 assert_equal('g?y', url.to_s)298# http://a/b/c/d;p?q299# #s = http://a/b/c/d;p?q#s300 url = @base_url.merge('#s')301 assert_kind_of(URI::HTTP, url)302 assert_equal('http://a/b/c/d;p?q#s', url.to_s)303 url = @base_url.route_to('http://a/b/c/d;p?q#s')304 assert_kind_of(URI::Generic, url)305 assert_equal('#s', url.to_s)306# http://a/b/c/d;p?q307# g#s = http://a/b/c/g#s308 url = @base_url.merge('g#s')309 assert_kind_of(URI::HTTP, url)310 assert_equal('http://a/b/c/g#s', url.to_s)311 url = @base_url.route_to('http://a/b/c/g#s')312 assert_kind_of(URI::Generic, url)313 assert_equal('g#s', url.to_s)314# http://a/b/c/d;p?q315# g?y#s = http://a/b/c/g?y#s316 url = @base_url.merge('g?y#s')317 assert_kind_of(URI::HTTP, url)318 assert_equal('http://a/b/c/g?y#s', url.to_s)319 url = @base_url.route_to('http://a/b/c/g?y#s')320 assert_kind_of(URI::Generic, url)321 assert_equal('g?y#s', url.to_s)322# http://a/b/c/d;p?q323# ;x = http://a/b/c/;x324 url = @base_url.merge(';x')325 assert_kind_of(URI::HTTP, url)326 assert_equal('http://a/b/c/;x', url.to_s)327 url = @base_url.route_to('http://a/b/c/;x')328 assert_kind_of(URI::Generic, url)329 assert_equal(';x', url.to_s)330# http://a/b/c/d;p?q331# g;x = http://a/b/c/g;x332 url = @base_url.merge('g;x')333 assert_kind_of(URI::HTTP, url)334 assert_equal('http://a/b/c/g;x', url.to_s)335 url = @base_url.route_to('http://a/b/c/g;x')336 assert_kind_of(URI::Generic, url)337 assert_equal('g;x', url.to_s)338# http://a/b/c/d;p?q339# g;x?y#s = http://a/b/c/g;x?y#s340 url = @base_url.merge('g;x?y#s')341 assert_kind_of(URI::HTTP, url)342 assert_equal('http://a/b/c/g;x?y#s', url.to_s)343 url = @base_url.route_to('http://a/b/c/g;x?y#s')344 assert_kind_of(URI::Generic, url)345 assert_equal('g;x?y#s', url.to_s)346# http://a/b/c/d;p?q347# . = http://a/b/c/348 url = @base_url.merge('.')349 assert_kind_of(URI::HTTP, url)350 assert_equal('http://a/b/c/', url.to_s)351 url = @base_url.route_to('http://a/b/c/')352 assert_kind_of(URI::Generic, url)353 assert('.' != url.to_s) # ok354 assert_equal('./', url.to_s)355# http://a/b/c/d;p?q356# ./ = http://a/b/c/357 url = @base_url.merge('./')358 assert_kind_of(URI::HTTP, url)359 assert_equal('http://a/b/c/', url.to_s)360 url = @base_url.route_to('http://a/b/c/')361 assert_kind_of(URI::Generic, url)362 assert_equal('./', url.to_s)363# http://a/b/c/d;p?q364# .. = http://a/b/365 url = @base_url.merge('..')366 assert_kind_of(URI::HTTP, url)367 assert_equal('http://a/b/', url.to_s)368 url = @base_url.route_to('http://a/b/')369 assert_kind_of(URI::Generic, url)370 assert('..' != url.to_s) # ok371 assert_equal('../', url.to_s)372# http://a/b/c/d;p?q373# ../ = http://a/b/374 url = @base_url.merge('../')375 assert_kind_of(URI::HTTP, url)376 assert_equal('http://a/b/', url.to_s)377 url = @base_url.route_to('http://a/b/')378 assert_kind_of(URI::Generic, url)379 assert_equal('../', url.to_s)380# http://a/b/c/d;p?q381# ../g = http://a/b/g382 url = @base_url.merge('../g')383 assert_kind_of(URI::HTTP, url)384 assert_equal('http://a/b/g', url.to_s)385 url = @base_url.route_to('http://a/b/g')386 assert_kind_of(URI::Generic, url)387 assert_equal('../g', url.to_s)388# http://a/b/c/d;p?q389# ../.. = http://a/390 url = @base_url.merge('../..')391 assert_kind_of(URI::HTTP, url)392 assert_equal('http://a/', url.to_s)393 url = @base_url.route_to('http://a/')394 assert_kind_of(URI::Generic, url)395 assert('../..' != url.to_s) # ok396 assert_equal('../../', url.to_s)397# http://a/b/c/d;p?q398# ../../ = http://a/399 url = @base_url.merge('../../')400 assert_kind_of(URI::HTTP, url)401 assert_equal('http://a/', url.to_s)402 url = @base_url.route_to('http://a/')403 assert_kind_of(URI::Generic, url)404 assert_equal('../../', url.to_s)405# http://a/b/c/d;p?q406# ../../g = http://a/g407 url = @base_url.merge('../../g')408 assert_kind_of(URI::HTTP, url)409 assert_equal('http://a/g', url.to_s)410 url = @base_url.route_to('http://a/g')411 assert_kind_of(URI::Generic, url)412 assert_equal('../../g', url.to_s)413# http://a/b/c/d;p?q414# <> = (current document)415 url = @base_url.merge('')416 assert_kind_of(URI::HTTP, url)417 assert_equal('http://a/b/c/d;p?q', url.to_s)418 url = @base_url.route_to('http://a/b/c/d;p?q')419 assert_kind_of(URI::Generic, url)420 assert_equal('', url.to_s)421# http://a/b/c/d;p?q422# /./g = http://a/g423 url = @base_url.merge('/./g')424 assert_kind_of(URI::HTTP, url)425 assert_equal('http://a/g', url.to_s)426# url = @base_url.route_to('http://a/./g')427# assert_kind_of(URI::Generic, url)428# assert_equal('/./g', url.to_s)429# http://a/b/c/d;p?q430# /../g = http://a/g431 url = @base_url.merge('/../g')432 assert_kind_of(URI::HTTP, url)433 assert_equal('http://a/g', url.to_s)434# url = @base_url.route_to('http://a/../g')435# assert_kind_of(URI::Generic, url)436# assert_equal('/../g', url.to_s)437# http://a/b/c/d;p?q438# g. = http://a/b/c/g.439 url = @base_url.merge('g.')440 assert_kind_of(URI::HTTP, url)441 assert_equal('http://a/b/c/g.', url.to_s)442 url = @base_url.route_to('http://a/b/c/g.')443 assert_kind_of(URI::Generic, url)444 assert_equal('g.', url.to_s)445# http://a/b/c/d;p?q446# .g = http://a/b/c/.g447 url = @base_url.merge('.g')448 assert_kind_of(URI::HTTP, url)449 assert_equal('http://a/b/c/.g', url.to_s)450 url = @base_url.route_to('http://a/b/c/.g')451 assert_kind_of(URI::Generic, url)452 assert_equal('.g', url.to_s)453# http://a/b/c/d;p?q454# g.. = http://a/b/c/g..455 url = @base_url.merge('g..')456 assert_kind_of(URI::HTTP, url)457 assert_equal('http://a/b/c/g..', url.to_s)458 url = @base_url.route_to('http://a/b/c/g..')459 assert_kind_of(URI::Generic, url)460 assert_equal('g..', url.to_s)461# http://a/b/c/d;p?q462# ..g = http://a/b/c/..g463 url = @base_url.merge('..g')464 assert_kind_of(URI::HTTP, url)465 assert_equal('http://a/b/c/..g', url.to_s)466 url = @base_url.route_to('http://a/b/c/..g')467 assert_kind_of(URI::Generic, url)468 assert_equal('..g', url.to_s)469# http://a/b/c/d;p?q470# ../../../g = http://a/g471 url = @base_url.merge('../../../g')472 assert_kind_of(URI::HTTP, url)473 assert_equal('http://a/g', url.to_s)474 url = @base_url.route_to('http://a/g')475 assert_kind_of(URI::Generic, url)476 assert('../../../g' != url.to_s) # ok? yes, it confuses you477 assert_equal('../../g', url.to_s) # and it is clearly478# http://a/b/c/d;p?q479# ../../../../g = http://a/g480 url = @base_url.merge('../../../../g')481 assert_kind_of(URI::HTTP, url)482 assert_equal('http://a/g', url.to_s)483 url = @base_url.route_to('http://a/g')484 assert_kind_of(URI::Generic, url)485 assert('../../../../g' != url.to_s) # ok? yes, it confuses you486 assert_equal('../../g', url.to_s) # and it is clearly487# http://a/b/c/d;p?q488# ./../g = http://a/b/g489 url = @base_url.merge('./../g')490 assert_kind_of(URI::HTTP, url)491 assert_equal('http://a/b/g', url.to_s)492 url = @base_url.route_to('http://a/b/g')493 assert_kind_of(URI::Generic, url)494 assert('./../g' != url.to_s) # ok495 assert_equal('../g', url.to_s)496# http://a/b/c/d;p?q497# ./g/. = http://a/b/c/g/498 url = @base_url.merge('./g/.')499 assert_kind_of(URI::HTTP, url)500 assert_equal('http://a/b/c/g/', url.to_s)501 url = @base_url.route_to('http://a/b/c/g/')502 assert_kind_of(URI::Generic, url)503 assert('./g/.' != url.to_s) # ok504 assert_equal('g/', url.to_s)505# http://a/b/c/d;p?q506# g/./h = http://a/b/c/g/h507 url = @base_url.merge('g/./h')508 assert_kind_of(URI::HTTP, url)509 assert_equal('http://a/b/c/g/h', url.to_s)510 url = @base_url.route_to('http://a/b/c/g/h')511 assert_kind_of(URI::Generic, url)512 assert('g/./h' != url.to_s) # ok513 assert_equal('g/h', url.to_s)514# http://a/b/c/d;p?q515# g/../h = http://a/b/c/h516 url = @base_url.merge('g/../h')517 assert_kind_of(URI::HTTP, url)518 assert_equal('http://a/b/c/h', url.to_s)519 url = @base_url.route_to('http://a/b/c/h')520 assert_kind_of(URI::Generic, url)521 assert('g/../h' != url.to_s) # ok522 assert_equal('h', url.to_s)523# http://a/b/c/d;p?q524# g;x=1/./y = http://a/b/c/g;x=1/y525 url = @base_url.merge('g;x=1/./y')526 assert_kind_of(URI::HTTP, url)527 assert_equal('http://a/b/c/g;x=1/y', url.to_s)528 url = @base_url.route_to('http://a/b/c/g;x=1/y')529 assert_kind_of(URI::Generic, url)530 assert('g;x=1/./y' != url.to_s) # ok531 assert_equal('g;x=1/y', url.to_s)532# http://a/b/c/d;p?q533# g;x=1/../y = http://a/b/c/y534 url = @base_url.merge('g;x=1/../y')535 assert_kind_of(URI::HTTP, url)536 assert_equal('http://a/b/c/y', url.to_s)537 url = @base_url.route_to('http://a/b/c/y')538 assert_kind_of(URI::Generic, url)539 assert('g;x=1/../y' != url.to_s) # ok540 assert_equal('y', url.to_s)541# http://a/b/c/d;p?q542# g?y/./x = http://a/b/c/g?y/./x543 url = @base_url.merge('g?y/./x')544 assert_kind_of(URI::HTTP, url)545 assert_equal('http://a/b/c/g?y/./x', url.to_s)546 url = @base_url.route_to('http://a/b/c/g?y/./x')547 assert_kind_of(URI::Generic, url)548 assert_equal('g?y/./x', url.to_s)549# http://a/b/c/d;p?q550# g?y/../x = http://a/b/c/g?y/../x551 url = @base_url.merge('g?y/../x')552 assert_kind_of(URI::HTTP, url)553 assert_equal('http://a/b/c/g?y/../x', url.to_s)554 url = @base_url.route_to('http://a/b/c/g?y/../x')555 assert_kind_of(URI::Generic, url)556 assert_equal('g?y/../x', url.to_s)557# http://a/b/c/d;p?q558# g#s/./x = http://a/b/c/g#s/./x559 url = @base_url.merge('g#s/./x')560 assert_kind_of(URI::HTTP, url)561 assert_equal('http://a/b/c/g#s/./x', url.to_s)562 url = @base_url.route_to('http://a/b/c/g#s/./x')563 assert_kind_of(URI::Generic, url)564 assert_equal('g#s/./x', url.to_s)565# http://a/b/c/d;p?q566# g#s/../x = http://a/b/c/g#s/../x567 url = @base_url.merge('g#s/../x')568 assert_kind_of(URI::HTTP, url)569 assert_equal('http://a/b/c/g#s/../x', url.to_s)570 url = @base_url.route_to('http://a/b/c/g#s/../x')571 assert_kind_of(URI::Generic, url)572 assert_equal('g#s/../x', url.to_s)573# http://a/b/c/d;p?q574# http:g = http:g ; for validating parsers575# | http://a/b/c/g ; for backwards compatibility576 url = @base_url.merge('http:g')577 assert_kind_of(URI::HTTP, url)578 assert_equal('http:g', url.to_s)579 url = @base_url.route_to('http:g')580 assert_kind_of(URI::Generic, url)581 assert_equal('http:g', url.to_s)582 end583 def test_join584 assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar'))585 assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar'))586 assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/'))587 assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz'))588 assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz'))589 assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/'))590 assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz'))591 assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge'))592 assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz'))593 assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))594 assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))595 assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))596 assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge'))597 assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge'))598 assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge'))599 end600 # ruby-dev:16728601 def test_set_component602 uri = URI.parse('http://foo:bar@baz')603 assert_equal('oof', uri.user = 'oof')604 assert_equal('http://oof:bar@baz', uri.to_s)605 assert_equal('rab', uri.password = 'rab')606 assert_equal('http://oof:rab@baz', uri.to_s)607 assert_equal('foo', uri.userinfo = 'foo')608 assert_equal('http://foo:rab@baz', uri.to_s)609 assert_equal(['foo', 'bar'], uri.userinfo = ['foo', 'bar'])610 assert_equal('http://foo:bar@baz', uri.to_s)611 assert_equal(['foo'], uri.userinfo = ['foo'])612 assert_equal('http://foo:bar@baz', uri.to_s)613 assert_equal('zab', uri.host = 'zab')614 assert_equal('http://foo:bar@zab', uri.to_s)615 uri.port = ""616 assert_nil(uri.port)617 uri.port = "80"618 assert_equal(80, uri.port)619 uri.port = "080"620 assert_equal(80, uri.port)621 uri.port = " 080 "622 assert_equal(80, uri.port)623 assert_equal(8080, uri.port = 8080)624 assert_equal('http://foo:bar@zab:8080', uri.to_s)625 assert_equal('/', uri.path = '/')626 assert_equal('http://foo:bar@zab:8080/', uri.to_s)627 assert_equal('a=1', uri.query = 'a=1')628 assert_equal('http://foo:bar@zab:8080/?a=1', uri.to_s)629 assert_equal('b123', uri.fragment = 'b123')630 assert_equal('http://foo:bar@zab:8080/?a=1#b123', uri.to_s)631 assert_equal('a[]=1', uri.query = 'a[]=1')632 assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)633 uri = URI.parse('http://foo:bar@zab:8080/?a[]=1#b123')634 assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)635 uri = URI.parse('http://example.com')636 assert_raise(URI::InvalidURIError) { uri.password = 'bar' }637 assert_equal("foo\nbar", uri.query = "foo\nbar")638 uri.userinfo = 'foo:bar'639 assert_equal('http://foo:bar@example.com?foobar', uri.to_s)640 assert_raise(URI::InvalidURIError) { uri.registry = 'bar' }641 assert_raise(URI::InvalidURIError) { uri.opaque = 'bar' }642 uri = URI.parse('mailto:foo@example.com')643 assert_raise(URI::InvalidURIError) { uri.user = 'bar' }644 assert_raise(URI::InvalidURIError) { uri.password = 'bar' }645 assert_raise(URI::InvalidURIError) { uri.userinfo = ['bar', 'baz'] }646 assert_raise(URI::InvalidURIError) { uri.host = 'bar' }647 assert_raise(URI::InvalidURIError) { uri.port = 'bar' }648 assert_raise(URI::InvalidURIError) { uri.path = 'bar' }649 assert_raise(URI::InvalidURIError) { uri.query = 'bar' }650 uri = URI.parse('foo:bar')651 assert_raise(URI::InvalidComponentError) { uri.opaque = '/baz' }652 uri.opaque = 'xyzzy'653 assert_equal('foo:xyzzy', uri.to_s)654 end655 def test_set_scheme656 uri = URI.parse 'HTTP://example'657 assert_equal 'http://example', uri.to_s658 end659 def test_ipv6660 assert_equal("[::1]", URI("http://[::1]/bar/baz").host)661 assert_equal("::1", URI("http://[::1]/bar/baz").hostname)662 u = URI("http://foo/bar")663 assert_equal("http://foo/bar", u.to_s)664 u.hostname = "::1"665 assert_equal("http://[::1]/bar", u.to_s)666 end667 def test_build668 u = URI::Generic.build(['http', nil, 'example.com', 80, nil, '/foo', nil, nil, nil])669 assert_equal('http://example.com:80/foo', u.to_s)670 u = URI::Generic.build(:scheme => "http", :host => "::1", :path => "/bar/baz")671 assert_equal("http://[::1]/bar/baz", u.to_s)672 assert_equal("[::1]", u.host)673 assert_equal("::1", u.hostname)674 u = URI::Generic.build(:scheme => "http", :host => "[::1]", :path => "/bar/baz")675 assert_equal("http://[::1]/bar/baz", u.to_s)676 assert_equal("[::1]", u.host)677 assert_equal("::1", u.hostname)678 end679 def test_build2680 u = URI::Generic.build2(path: "/foo bar/baz")681 assert_equal('/foo%20bar/baz', u.to_s)682 u = URI::Generic.build2(['http', nil, 'example.com', 80, nil, '/foo bar' , nil, nil, nil])683 assert_equal('http://example.com:80/foo%20bar', u.to_s)684 end685 # 192.0.2.0/24 is TEST-NET. [RFC3330]686 def test_find_proxy687 assert_raise(URI::BadURIError){ URI("foo").find_proxy }688 with_env({}) {689 assert_nil(URI("http://192.0.2.1/").find_proxy)690 assert_nil(URI("ftp://192.0.2.1/").find_proxy)691 }692 with_env('http_proxy'=>'http://127.0.0.1:8080') {693 assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)694 assert_nil(URI("ftp://192.0.2.1/").find_proxy)695 }696 with_env('ftp_proxy'=>'http://127.0.0.1:8080') {697 assert_nil(URI("http://192.0.2.1/").find_proxy)698 assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy)699 }700 with_env('REQUEST_METHOD'=>'GET') {701 assert_nil(URI("http://192.0.2.1/").find_proxy)702 }703 with_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {704 assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)705 }706 with_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {707 assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)708 assert_nil(URI("http://192.0.2.2/").find_proxy)709 }710 with_env('http_proxy'=>'') {711 assert_nil(URI("http://192.0.2.1/").find_proxy)712 assert_nil(URI("ftp://192.0.2.1/").find_proxy)713 }714 with_env('ftp_proxy'=>'') {715 assert_nil(URI("http://192.0.2.1/").find_proxy)716 assert_nil(URI("ftp://192.0.2.1/").find_proxy)717 }718 end719 def test_find_proxy_case_sensitive_env720 with_env('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {721 assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)722 }723 with_env('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {724 assert_nil(nil, URI("http://192.0.2.1/").find_proxy)725 }726 with_env('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {727 assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy)728 }729 end unless RUBY_PLATFORM =~ /mswin|mingw/730 def with_env(h)731 ['http', 'https', 'ftp'].each do |scheme|732 name = "#{scheme}_proxy"733 h[name] ||= nil734 h["CGI_#{name.upcase}"] ||= nil735 end736 begin737 old = {}738 h.each_key {|k| old[k] = ENV[k] }739 h.each {|k, v| ENV[k] = v }740 yield741 ensure742 h.each_key {|k| ENV[k] = old[k] }743 end744 end745end...

Full Screen

Full Screen

test_fake_web.rb

Source:test_fake_web.rb Github

copy

Full Screen

1require File.join(File.dirname(__FILE__), "test_helper")2class TestFakeWeb < Test::Unit::TestCase3 def test_register_uri4 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "example")5 assert FakeWeb.registered_uri?(:get, 'http://mock/test_example.txt')6 end7 def test_register_uri_with_wrong_number_of_arguments8 assert_raises ArgumentError do9 FakeWeb.register_uri("http://example.com")10 end11 assert_raises ArgumentError do12 FakeWeb.register_uri(:get, "http://example.com", "/example", :body => "example")13 end14 end15 def test_registered_uri_with_wrong_number_of_arguments16 assert_raises ArgumentError do17 FakeWeb.registered_uri?18 end19 assert_raises ArgumentError do20 FakeWeb.registered_uri?(:get, "http://example.com", "/example")21 end22 end23 def test_response_for_with_wrong_number_of_arguments24 assert_raises ArgumentError do25 FakeWeb.response_for26 end27 assert_raises ArgumentError do28 FakeWeb.response_for(:get, "http://example.com", "/example")29 end30 end31 def test_register_uri_without_domain_name32 assert_raises URI::InvalidURIError do33 FakeWeb.register_uri(:get, 'test_example2.txt', File.dirname(__FILE__) + '/fixtures/test_example.txt')34 end35 end36 def test_register_uri_with_port_and_check_with_port37 FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')38 assert FakeWeb.registered_uri?(:get, 'http://example.com:3000/')39 end40 def test_register_uri_with_port_and_check_without_port41 FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'foo')42 assert !FakeWeb.registered_uri?(:get, 'http://example.com/')43 end44 def test_register_uri_with_default_port_for_http_and_check_without_port45 FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'foo')46 assert FakeWeb.registered_uri?(:get, 'http://example.com/')47 end48 def test_register_uri_with_default_port_for_https_and_check_without_port49 FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'foo')50 assert FakeWeb.registered_uri?(:get, 'https://example.com/')51 end52 def test_register_uri_with_no_port_for_http_and_check_with_default_port53 FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')54 assert FakeWeb.registered_uri?(:get, 'http://example.com:80/')55 end56 def test_register_uri_with_no_port_for_https_and_check_with_default_port57 FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')58 assert FakeWeb.registered_uri?(:get, 'https://example.com:443/')59 end60 def test_register_uri_with_no_port_for_https_and_check_with_443_on_http61 FakeWeb.register_uri(:get, 'https://example.com/', :body => 'foo')62 assert !FakeWeb.registered_uri?(:get, 'http://example.com:443/')63 end64 def test_register_uri_with_no_port_for_http_and_check_with_80_on_https65 FakeWeb.register_uri(:get, 'http://example.com/', :body => 'foo')66 assert !FakeWeb.registered_uri?(:get, 'https://example.com:80/')67 end68 def test_register_uri_for_any_method_explicitly69 FakeWeb.register_uri(:any, "http://example.com/rpc_endpoint", :body => "OK")70 assert FakeWeb.registered_uri?(:get, "http://example.com/rpc_endpoint")71 assert FakeWeb.registered_uri?(:post, "http://example.com/rpc_endpoint")72 assert FakeWeb.registered_uri?(:put, "http://example.com/rpc_endpoint")73 assert FakeWeb.registered_uri?(:delete, "http://example.com/rpc_endpoint")74 assert FakeWeb.registered_uri?(:any, "http://example.com/rpc_endpoint")75 capture_stderr do # silence deprecation warning76 assert FakeWeb.registered_uri?("http://example.com/rpc_endpoint")77 end78 end79 def test_register_uri_for_get_method_only80 FakeWeb.register_uri(:get, "http://example.com/users", :body => "User list")81 assert FakeWeb.registered_uri?(:get, "http://example.com/users")82 assert !FakeWeb.registered_uri?(:post, "http://example.com/users")83 assert !FakeWeb.registered_uri?(:put, "http://example.com/users")84 assert !FakeWeb.registered_uri?(:delete, "http://example.com/users")85 assert !FakeWeb.registered_uri?(:any, "http://example.com/users")86 capture_stderr do # silence deprecation warning87 assert !FakeWeb.registered_uri?("http://example.com/users")88 end89 end90 def test_clean_registry_affects_registered_uri91 FakeWeb.register_uri(:get, "http://example.com", :body => "registered")92 assert FakeWeb.registered_uri?(:get, "http://example.com")93 FakeWeb.clean_registry94 assert !FakeWeb.registered_uri?(:get, "http://example.com")95 end96 def test_clean_registry_affects_net_http_requests97 FakeWeb.register_uri(:get, "http://example.com", :body => "registered")98 response = Net::HTTP.start("example.com") { |query| query.get("/") }99 assert_equal "registered", response.body100 FakeWeb.clean_registry101 assert_raise FakeWeb::NetConnectNotAllowedError do102 Net::HTTP.start("example.com") { |query| query.get("/") }103 end104 end105 def test_response_for_with_registered_uri106 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')107 assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body108 end109 def test_response_for_with_unknown_uri110 assert_nil FakeWeb.response_for(:get, 'http://example.com/')111 end112 def test_response_for_with_put_method113 FakeWeb.register_uri(:put, "http://example.com", :body => "response")114 assert_equal 'response', FakeWeb.response_for(:put, "http://example.com").body115 end116 def test_response_for_with_any_method_explicitly117 FakeWeb.register_uri(:any, "http://example.com", :body => "response")118 assert_equal 'response', FakeWeb.response_for(:get, "http://example.com").body119 assert_equal 'response', FakeWeb.response_for(:any, "http://example.com").body120 end121 def test_content_for_registered_uri_with_port_and_request_with_port122 FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'test example content')123 response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }124 assert_equal 'test example content', response.body125 end126 def test_content_for_registered_uri_with_default_port_for_http_and_request_without_port127 FakeWeb.register_uri(:get, 'http://example.com:80/', :body => 'test example content')128 response = Net::HTTP.start('example.com') { |http| http.get('/') }129 assert_equal 'test example content', response.body130 end131 def test_content_for_registered_uri_with_no_port_for_http_and_request_with_default_port132 FakeWeb.register_uri(:get, 'http://example.com/', :body => 'test example content')133 response = Net::HTTP.start('example.com', 80) { |http| http.get('/') }134 assert_equal 'test example content', response.body135 end136 def test_content_for_registered_uri_with_default_port_for_https_and_request_with_default_port137 FakeWeb.register_uri(:get, 'https://example.com:443/', :body => 'test example content')138 http = Net::HTTP.new('example.com', 443)139 http.use_ssl = true140 response = http.get('/')141 assert_equal 'test example content', response.body142 end143 def test_content_for_registered_uri_with_no_port_for_https_and_request_with_default_port144 FakeWeb.register_uri(:get, 'https://example.com/', :body => 'test example content')145 http = Net::HTTP.new('example.com', 443)146 http.use_ssl = true147 response = http.get('/')148 assert_equal 'test example content', response.body149 end150 def test_content_for_registered_uris_with_ports_on_same_domain_and_request_without_port151 FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')152 FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')153 response = Net::HTTP.start('example.com') { |http| http.get('/') }154 assert_equal 'port 80', response.body155 end156 def test_content_for_registered_uris_with_ports_on_same_domain_and_request_with_port157 FakeWeb.register_uri(:get, 'http://example.com:3000/', :body => 'port 3000')158 FakeWeb.register_uri(:get, 'http://example.com/', :body => 'port 80')159 response = Net::HTTP.start('example.com', 3000) { |http| http.get('/') }160 assert_equal 'port 3000', response.body161 end162 def test_content_for_registered_uri_with_get_method_only163 FakeWeb.allow_net_connect = false164 FakeWeb.register_uri(:get, "http://example.com/", :body => "test example content")165 http = Net::HTTP.new('example.com')166 assert_equal 'test example content', http.get('/').body167 assert_raises(FakeWeb::NetConnectNotAllowedError) { http.post('/', nil) }168 assert_raises(FakeWeb::NetConnectNotAllowedError) { http.put('/', nil) }169 assert_raises(FakeWeb::NetConnectNotAllowedError) { http.delete('/') }170 end171 def test_content_for_registered_uri_with_any_method_explicitly172 FakeWeb.allow_net_connect = false173 FakeWeb.register_uri(:any, "http://example.com/", :body => "test example content")174 http = Net::HTTP.new('example.com')175 assert_equal 'test example content', http.get('/').body176 assert_equal 'test example content', http.post('/', nil).body177 assert_equal 'test example content', http.put('/', nil).body178 assert_equal 'test example content', http.delete('/').body179 end180 def test_content_for_registered_uri_with_any_method_implicitly181 FakeWeb.allow_net_connect = false182 capture_stderr do # silence deprecation warning183 FakeWeb.register_uri("http://example.com/", :body => "test example content")184 end185 http = Net::HTTP.new('example.com')186 assert_equal 'test example content', http.get('/').body187 assert_equal 'test example content', http.post('/', nil).body188 assert_equal 'test example content', http.put('/', nil).body189 assert_equal 'test example content', http.delete('/').body190 end191 def test_mock_request_with_block192 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')193 response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }194 assert_equal 'test example content', response.body195 end196 def test_request_with_registered_body_yields_the_response_body_to_a_request_block197 FakeWeb.register_uri(:get, "http://example.com", :body => "content")198 body = nil199 Net::HTTP.start("example.com") do |http|200 http.get("/") do |response_body|201 body = response_body202 end203 end204 assert_equal "content", body205 end206 def test_request_with_registered_response_yields_the_response_body_to_a_request_block207 fake_response = Net::HTTPOK.new('1.1', '200', 'OK')208 fake_response.instance_variable_set(:@body, "content")209 FakeWeb.register_uri(:get, 'http://example.com', :response => fake_response)210 body = nil211 Net::HTTP.start("example.com") do |http|212 http.get("/") do |response_body|213 body = response_body214 end215 end216 assert_equal "content", body217 end218 def test_mock_request_with_undocumented_full_uri_argument_style219 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')220 response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt') }221 assert_equal 'test example content', response.body222 end223 def test_mock_request_with_undocumented_full_uri_argument_style_and_query224 FakeWeb.register_uri(:get, 'http://mock/test_example.txt?a=b', :body => 'test query content')225 response = Net::HTTP.start('mock') { |query| query.get('http://mock/test_example.txt?a=b') }226 assert_equal 'test query content', response.body227 end228 def test_mock_post229 FakeWeb.register_uri(:post, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')230 response = Net::HTTP.start('mock') { |query| query.post('/test_example.txt', '') }231 assert_equal 'test example content', response.body232 end233 def test_mock_post_with_string_as_registered_uri234 FakeWeb.register_uri(:post, 'http://mock/test_string.txt', :body => 'foo')235 response = Net::HTTP.start('mock') { |query| query.post('/test_string.txt', '') }236 assert_equal 'foo', response.body237 end238 def test_mock_get_with_request_as_registered_uri239 fake_response = Net::HTTPOK.new('1.1', '200', 'OK')240 FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)241 response = Net::HTTP.start('mock') { |query| query.get('/test_response') }242 assert_equal fake_response, response243 end244 def test_mock_get_with_request_from_file_as_registered_uri245 FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')246 response = Net::HTTP.start('www.google.com') { |query| query.get('/') }247 assert_equal '200', response.code248 assert response.body.include?('<title>Google</title>')249 end250 def test_mock_post_with_request_from_file_as_registered_uri251 FakeWeb.register_uri(:post, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')252 response = Net::HTTP.start('www.google.com') { |query| query.post('/', '') }253 assert_equal "200", response.code254 assert response.body.include?('<title>Google</title>')255 end256 def test_proxy_request257 FakeWeb.register_uri(:get, 'http://www.example.com/', :body => "hello world")258 FakeWeb.register_uri(:get, 'http://your.proxy.host/', :body => "lala")259 response = nil260 Net::HTTP::Proxy('your.proxy.host', 8080).start('www.example.com') do |http|261 response = http.get('/')262 end263 assert_equal "hello world", response.body264 end265 def test_https_request266 FakeWeb.register_uri(:get, 'https://www.example.com/', :body => "Hello World")267 http = Net::HTTP.new('www.example.com', 443)268 http.use_ssl = true269 response = http.get('/')270 assert_equal "Hello World", response.body271 end272 def test_register_unimplemented_response273 FakeWeb.register_uri(:get, 'http://mock/unimplemented', :response => 1)274 assert_raises StandardError do275 Net::HTTP.start('mock') { |q| q.get('/unimplemented') }276 end277 end278 def test_real_http_request279 FakeWeb.allow_net_connect = true280 setup_expectations_for_real_apple_hot_news_request281 resp = nil282 Net::HTTP.start('images.apple.com') do |query|283 resp = query.get('/main/rss/hotnews/hotnews.rss')284 end285 assert resp.body.include?('Apple')286 assert resp.body.include?('News')287 end288 def test_real_http_request_with_undocumented_full_uri_argument_style289 FakeWeb.allow_net_connect = true290 setup_expectations_for_real_apple_hot_news_request(:path => 'http://images.apple.com/main/rss/hotnews/hotnews.rss')291 resp = nil292 Net::HTTP.start('images.apple.com') do |query|293 resp = query.get('http://images.apple.com/main/rss/hotnews/hotnews.rss')294 end295 assert resp.body.include?('Apple')296 assert resp.body.include?('News')297 end298 def test_real_https_request299 FakeWeb.allow_net_connect = true300 setup_expectations_for_real_apple_hot_news_request(:port => 443)301 http = Net::HTTP.new('images.apple.com', 443)302 http.use_ssl = true303 http.verify_mode = OpenSSL::SSL::VERIFY_NONE # silence certificate warning304 response = http.get('/main/rss/hotnews/hotnews.rss')305 assert response.body.include?('Apple')306 assert response.body.include?('News')307 end308 def test_real_request_on_same_domain_as_mock309 FakeWeb.allow_net_connect = true310 setup_expectations_for_real_apple_hot_news_request311 FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')312 resp = nil313 Net::HTTP.start('images.apple.com') do |query|314 resp = query.get('/main/rss/hotnews/hotnews.rss')315 end316 assert resp.body.include?('Apple')317 assert resp.body.include?('News')318 end319 def test_mock_request_on_real_domain320 FakeWeb.register_uri(:get, 'http://images.apple.com/test_string.txt', :body => 'foo')321 resp = nil322 Net::HTTP.start('images.apple.com') do |query|323 resp = query.get('/test_string.txt')324 end325 assert_equal 'foo', resp.body326 end327 def test_mock_post_that_raises_exception328 FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => StandardError)329 assert_raises(StandardError) do330 Net::HTTP.start('mock') do |query|331 query.post('/raising_exception.txt', 'some data')332 end333 end334 end335 def test_mock_post_that_raises_an_http_error336 FakeWeb.register_uri(:post, 'http://mock/raising_exception.txt', :exception => Net::HTTPError)337 assert_raises(Net::HTTPError) do338 Net::HTTP.start('mock') do |query|339 query.post('/raising_exception.txt', '')340 end341 end342 end343 def test_raising_an_exception_that_requires_an_argument_to_instantiate344 FakeWeb.register_uri(:get, "http://example.com/timeout.txt", :exception => Timeout::Error)345 assert_raises(Timeout::Error) do346 Net::HTTP.get(URI.parse("http://example.com/timeout.txt"))347 end348 end349 def test_mock_instance_syntax350 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')351 response = nil352 uri = URI.parse('http://mock/test_example.txt')353 http = Net::HTTP.new(uri.host, uri.port)354 response = http.start do355 http.get(uri.path)356 end357 assert_equal 'test example content', response.body358 end359 def test_mock_via_nil_proxy360 response = nil361 proxy_address = nil362 proxy_port = nil363 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')364 uri = URI.parse('http://mock/test_example.txt')365 http = Net::HTTP::Proxy(proxy_address, proxy_port).new(366 uri.host, (uri.port or 80))367 response = http.start do368 http.get(uri.path)369 end370 assert_equal 'test example content', response.body371 end372 def test_response_type373 FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => "test")374 response = Net::HTTP.start('mock') { |http| http.get('/test_example.txt') }375 assert_kind_of Net::HTTPSuccess, response376 end377 def test_mock_request_that_raises_an_http_error_with_a_specific_status378 FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])379 exception = assert_raises(Net::HTTPError) do380 Net::HTTP.start('mock') { |http| http.get('/raising_exception.txt') }381 end382 assert_equal '404', exception.response.code383 assert_equal 'Not Found', exception.response.msg384 end385 def test_mock_rotate_responses386 FakeWeb.register_uri(:get, 'http://mock/multiple_test_example.txt',387 [ {:body => File.dirname(__FILE__) + '/fixtures/test_example.txt', :times => 2},388 {:body => "thrice", :times => 3},389 {:body => "ever_more"} ])390 uri = URI.parse('http://mock/multiple_test_example.txt')391 2.times { assert_equal 'test example content', Net::HTTP.get(uri) }392 3.times { assert_equal 'thrice', Net::HTTP.get(uri) }393 4.times { assert_equal 'ever_more', Net::HTTP.get(uri) }394 end395 def test_mock_request_using_response_with_transfer_encoding_header_has_valid_transfer_encoding_header396 FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_with_transfer_encoding')397 response = Net::HTTP.start('www.google.com') { |query| query.get('/') }398 assert_not_nil response['transfer-encoding']399 assert response['transfer-encoding'] == 'chunked'400 end401 def test_mock_request_using_response_without_transfer_encoding_header_does_not_have_a_transfer_encoding_header402 FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_without_transfer_encoding')403 response = nil404 response = Net::HTTP.start('www.google.com') { |query| query.get('/') }405 assert !response.key?('transfer-encoding')406 end407 def test_mock_request_using_response_from_curl_has_original_transfer_encoding_header408 FakeWeb.register_uri(:get, 'http://www.google.com/', :response => File.dirname(__FILE__) + '/fixtures/google_response_from_curl')409 response = Net::HTTP.start('www.google.com') { |query| query.get('/') }410 assert_not_nil response['transfer-encoding']411 assert response['transfer-encoding'] == 'chunked'412 end413 def test_txt_file_should_have_three_lines414 FakeWeb.register_uri(:get, 'http://www.google.com/', :body => File.dirname(__FILE__) + '/fixtures/test_txt_file')415 response = Net::HTTP.start('www.google.com') { |query| query.get('/') }416 assert response.body.split(/\n/).size == 3, "response has #{response.body.split(/\n/).size} lines should have 3"417 end418 def test_requiring_fakeweb_instead_of_fake_web419 require "fakeweb"420 end421 def test_registering_with_string_containing_null_byte422 # Regression test for File.exists? raising an ArgumentError ("string423 # contains null byte") since :response first tries to find by filename.424 # The string should be treated as a response body, instead, and an425 # EOFError is raised when the byte is encountered.426 FakeWeb.register_uri(:get, "http://example.com", :response => "test\0test")427 assert_raise EOFError do428 Net::HTTP.get(URI.parse("http://example.com"))429 end430 FakeWeb.register_uri(:get, "http://example.com", :body => "test\0test")431 body = Net::HTTP.get(URI.parse("http://example.com"))432 assert_equal "test\0test", body433 end434 def test_registering_with_string_that_is_a_directory_name435 # Similar to above, but for Errno::EISDIR being raised since File.exists?436 # returns true for directories437 FakeWeb.register_uri(:get, "http://example.com", :response => File.dirname(__FILE__))438 assert_raise EOFError do439 body = Net::HTTP.get(URI.parse("http://example.com"))440 end441 FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__))442 body = Net::HTTP.get(URI.parse("http://example.com"))443 assert_equal File.dirname(__FILE__), body444 end445 def test_http_version_from_string_response446 FakeWeb.register_uri(:get, "http://example.com", :body => "example")447 response = Net::HTTP.start("example.com") { |http| http.get("/") }448 assert_equal "1.0", response.http_version449 end450 def test_http_version_from_file_response451 FakeWeb.register_uri(:get, "http://example.com", :body => File.dirname(__FILE__) + '/fixtures/test_example.txt')452 response = Net::HTTP.start("example.com") { |http| http.get("/") }453 assert_equal "1.0", response.http_version454 end455 def test_version456 assert_equal "1.2.6", FakeWeb::VERSION457 end458end...

Full Screen

Full Screen

plus_spec.rb

Source:plus_spec.rb Github

copy

Full Screen

2require 'uri'3#an alias of URI#merge4describe "URI#+" do5 it "replaces the end of the path of the URI when added to a string that looks like a relative path" do6 (URI('http://foo') + 'bar').should == URI("http://foo/bar")7 (URI('http://foo/baz') + 'bar').should == URI("http://foo/bar")8 (URI('http://foo/baz/') + 'bar').should == URI("http://foo/baz/bar")9 (URI('mailto:foo@example.com') + "#bar").should == URI("mailto:foo@example.com#bar")10 end11 it "replaces the entire path of the URI when added to a string that begins with a /" do12 (URI('http://foo/baz/') + '/bar').should == URI("http://foo/bar")13 end14 it "replaces the entire url when added to a string that looks like a full url" do15 (URI.parse('http://a/b') + 'http://x/y').should == URI("http://x/y")16 (URI.parse('telnet:example.com') + 'http://x/y').should == URI("http://x/y")17 end18 it "canonicalizes the URI's path, removing ../'s" do19 (URI.parse('http://a/b/c/../') + "./").should == URI("http://a/b/")20 (URI.parse('http://a/b/c/../') + ".").should == URI("http://a/b/")21 (URI.parse('http://a/b/c/') + "../").should == URI("http://a/b/")22 (URI.parse('http://a/b/c/../../') + "./").should == URI("http://a/")23 (URI.parse('http://a/b/c/') + "../e/").should == URI("http://a/b/e/")24 (URI.parse('http://a/b/c/') + "../e/../").should == URI("http://a/b/")25 (URI.parse('http://a/b/../c/') + ".").should == URI("http://a/c/")26 (URI.parse('http://a/b/c/../../../') + ".").should == URI("http://a/")27 end28 it "doesn't conconicalize the path when adding to the empty string" do29 (URI.parse('http://a/b/c/../') + "").should == URI("http://a/b/c/../")30 end31 it "raises a URI::BadURIError when adding two relative URIs" do32 lambda {URI.parse('a/b/c') + "d"}.should raise_error(URI::BadURIError)33 end34 #Todo: make more BDD?35 it "conforms to the merge specifications from rfc 2396" do36 @url = 'http://a/b/c/d;p?q'37 @base_url = URI.parse(@url)38# http://a/b/c/d;p?q39# g:h = g:h40 url = @base_url.merge('g:h')41 url.should be_kind_of(URI::Generic)42 url.to_s.should == 'g:h'43 url = @base_url.route_to('g:h')44 url.should be_kind_of(URI::Generic)45 url.to_s.should == 'g:h'46# http://a/b/c/d;p?q47# g = http://a/b/c/g48 url = @base_url.merge('g')49 url.should be_kind_of(URI::HTTP)50 url.to_s.should == 'http://a/b/c/g'51 url = @base_url.route_to('http://a/b/c/g')52 url.should be_kind_of(URI::Generic)53 url.to_s.should == 'g'54# http://a/b/c/d;p?q55# ./g = http://a/b/c/g56 url = @base_url.merge('./g')57 url.should be_kind_of(URI::HTTP)58 url.to_s.should == 'http://a/b/c/g'59 url = @base_url.route_to('http://a/b/c/g')60 url.should be_kind_of(URI::Generic)61 url.to_s.should_not == './g' # ok62 url.to_s.should == 'g'63# http://a/b/c/d;p?q64# g/ = http://a/b/c/g/65 url = @base_url.merge('g/')66 url.should be_kind_of(URI::HTTP)67 url.to_s.should == 'http://a/b/c/g/'68 url = @base_url.route_to('http://a/b/c/g/')69 url.should be_kind_of(URI::Generic)70 url.to_s.should == 'g/'71# http://a/b/c/d;p?q72# /g = http://a/g73 url = @base_url.merge('/g')74 url.should be_kind_of(URI::HTTP)75 url.to_s.should == 'http://a/g'76 url = @base_url.route_to('http://a/g')77 url.should be_kind_of(URI::Generic)78 url.to_s.should_not == '/g' # ok79 url.to_s.should == '../../g'80# http://a/b/c/d;p?q81# //g = http://g82 url = @base_url.merge('//g')83 url.should be_kind_of(URI::HTTP)84 url.to_s.should == 'http://g'85 url = @base_url.route_to('http://g')86 url.should be_kind_of(URI::Generic)87 url.to_s.should == '//g'88# http://a/b/c/d;p?q89# ?y = http://a/b/c/?y90 url = @base_url.merge('?y')91 url.should be_kind_of(URI::HTTP)92 url.to_s.should == 'http://a/b/c/d;p?y'93 url = @base_url.route_to('http://a/b/c/?y')94 url.should be_kind_of(URI::Generic)95 url.to_s.should == '?y'96# http://a/b/c/d;p?q97# g?y = http://a/b/c/g?y98 url = @base_url.merge('g?y')99 url.should be_kind_of(URI::HTTP)100 url.to_s.should == 'http://a/b/c/g?y'101 url = @base_url.route_to('http://a/b/c/g?y')102 url.should be_kind_of(URI::Generic)103 url.to_s.should == 'g?y'104# http://a/b/c/d;p?q105# #s = (current document)#s106 url = @base_url.merge('#s')107 url.should be_kind_of(URI::HTTP)108 url.to_s.should == @base_url.to_s + '#s'109 url = @base_url.route_to(@base_url.to_s + '#s')110 url.should be_kind_of(URI::Generic)111 url.to_s.should == '#s'112# http://a/b/c/d;p?q113# g#s = http://a/b/c/g#s114 url = @base_url.merge('g#s')115 url.should be_kind_of(URI::HTTP)116 url.to_s.should == 'http://a/b/c/g#s'117 url = @base_url.route_to('http://a/b/c/g#s')118 url.should be_kind_of(URI::Generic)119 url.to_s.should == 'g#s'120# http://a/b/c/d;p?q121# g?y#s = http://a/b/c/g?y#s122 url = @base_url.merge('g?y#s')123 url.should be_kind_of(URI::HTTP)124 url.to_s.should == 'http://a/b/c/g?y#s'125 url = @base_url.route_to('http://a/b/c/g?y#s')126 url.should be_kind_of(URI::Generic)127 url.to_s.should == 'g?y#s'128# http://a/b/c/d;p?q129# ;x = http://a/b/c/;x130 url = @base_url.merge(';x')131 url.should be_kind_of(URI::HTTP)132 url.to_s.should == 'http://a/b/c/;x'133 url = @base_url.route_to('http://a/b/c/;x')134 url.should be_kind_of(URI::Generic)135 url.to_s.should == ';x'136# http://a/b/c/d;p?q137# g;x = http://a/b/c/g;x138 url = @base_url.merge('g;x')139 url.should be_kind_of(URI::HTTP)140 url.to_s.should == 'http://a/b/c/g;x'141 url = @base_url.route_to('http://a/b/c/g;x')142 url.should be_kind_of(URI::Generic)143 url.to_s.should == 'g;x'144# http://a/b/c/d;p?q145# g;x?y#s = http://a/b/c/g;x?y#s146 url = @base_url.merge('g;x?y#s')147 url.should be_kind_of(URI::HTTP)148 url.to_s.should == 'http://a/b/c/g;x?y#s'149 url = @base_url.route_to('http://a/b/c/g;x?y#s')150 url.should be_kind_of(URI::Generic)151 url.to_s.should == 'g;x?y#s'152# http://a/b/c/d;p?q153# . = http://a/b/c/154 url = @base_url.merge('.')155 url.should be_kind_of(URI::HTTP)156 url.to_s.should == 'http://a/b/c/'157 url = @base_url.route_to('http://a/b/c/')158 url.should be_kind_of(URI::Generic)159 url.to_s.should_not == '.' # ok160 url.to_s.should == './'161# http://a/b/c/d;p?q162# ./ = http://a/b/c/163 url = @base_url.merge('./')164 url.should be_kind_of(URI::HTTP)165 url.to_s.should == 'http://a/b/c/'166 url = @base_url.route_to('http://a/b/c/')167 url.should be_kind_of(URI::Generic)168 url.to_s.should == './'169# http://a/b/c/d;p?q170# .. = http://a/b/171 url = @base_url.merge('..')172 url.should be_kind_of(URI::HTTP)173 url.to_s.should == 'http://a/b/'174 url = @base_url.route_to('http://a/b/')175 url.should be_kind_of(URI::Generic)176 url.to_s.should_not == '..' # ok177 url.to_s.should == '../'178# http://a/b/c/d;p?q179# ../ = http://a/b/180 url = @base_url.merge('../')181 url.should be_kind_of(URI::HTTP)182 url.to_s.should == 'http://a/b/'183 url = @base_url.route_to('http://a/b/')184 url.should be_kind_of(URI::Generic)185 url.to_s.should == '../'186# http://a/b/c/d;p?q187# ../g = http://a/b/g188 url = @base_url.merge('../g')189 url.should be_kind_of(URI::HTTP)190 url.to_s.should == 'http://a/b/g'191 url = @base_url.route_to('http://a/b/g')192 url.should be_kind_of(URI::Generic)193 url.to_s.should == '../g'194# http://a/b/c/d;p?q195# ../.. = http://a/196 url = @base_url.merge('../..')197 url.should be_kind_of(URI::HTTP)198 url.to_s.should == 'http://a/'199 url = @base_url.route_to('http://a/')200 url.should be_kind_of(URI::Generic)201 url.to_s.should_not == '../..' # ok202 url.to_s.should == '../../'203# http://a/b/c/d;p?q204# ../../ = http://a/205 url = @base_url.merge('../../')206 url.should be_kind_of(URI::HTTP)207 url.to_s.should == 'http://a/'208 url = @base_url.route_to('http://a/')209 url.should be_kind_of(URI::Generic)210 url.to_s.should == '../../'211# http://a/b/c/d;p?q212# ../../g = http://a/g213 url = @base_url.merge('../../g')214 url.should be_kind_of(URI::HTTP)215 url.to_s.should == 'http://a/g'216 url = @base_url.route_to('http://a/g')217 url.should be_kind_of(URI::Generic)218 url.to_s.should == '../../g'219# http://a/b/c/d;p?q220# <> = (current document)221 url = @base_url.merge('')222 url.should be_kind_of(URI::HTTP)223 url.to_s.should == 'http://a/b/c/d;p?q'224 url = @base_url.route_to('http://a/b/c/d;p?q')225 url.should be_kind_of(URI::Generic)226 url.to_s.should == ''227# http://a/b/c/d;p?q228# /./g = http://a/./g229 url = @base_url.merge('/./g')230 url.should be_kind_of(URI::HTTP)231 url.to_s.should == 'http://a/g'232 url = @base_url.route_to('http://a/./g')233 url.should be_kind_of(URI::Generic)234 url.to_s.should == '/./g'235# http://a/b/c/d;p?q236# /../g = http://a/../g237 url = @base_url.merge('/../g')238 url.should be_kind_of(URI::HTTP)239 url.to_s.should == 'http://a/g'240 url = @base_url.route_to('http://a/../g')241 url.should be_kind_of(URI::Generic)242 url.to_s.should == '/../g'243# http://a/b/c/d;p?q244# g. = http://a/b/c/g.245 url = @base_url.merge('g.')246 url.should be_kind_of(URI::HTTP)247 url.to_s.should == 'http://a/b/c/g.'248 url = @base_url.route_to('http://a/b/c/g.')249 url.should be_kind_of(URI::Generic)250 url.to_s.should == 'g.'251# http://a/b/c/d;p?q252# .g = http://a/b/c/.g253 url = @base_url.merge('.g')254 url.should be_kind_of(URI::HTTP)255 url.to_s.should == 'http://a/b/c/.g'256 url = @base_url.route_to('http://a/b/c/.g')257 url.should be_kind_of(URI::Generic)258 url.to_s.should == '.g'259# http://a/b/c/d;p?q260# g.. = http://a/b/c/g..261 url = @base_url.merge('g..')262 url.should be_kind_of(URI::HTTP)263 url.to_s.should == 'http://a/b/c/g..'264 url = @base_url.route_to('http://a/b/c/g..')265 url.should be_kind_of(URI::Generic)266 url.to_s.should == 'g..'267# http://a/b/c/d;p?q268# ..g = http://a/b/c/..g269 url = @base_url.merge('..g')270 url.should be_kind_of(URI::HTTP)271 url.to_s.should == 'http://a/b/c/..g'272 url = @base_url.route_to('http://a/b/c/..g')273 url.should be_kind_of(URI::Generic)274 url.to_s.should == '..g'275# http://a/b/c/d;p?q276# ../../../g = http://a/../g277 url = @base_url.merge('../../../g')278 url.should be_kind_of(URI::HTTP)279 url.to_s.should == 'http://a/g'280 url = @base_url.route_to('http://a/../g')281 url.should be_kind_of(URI::Generic)282 url.to_s.should_not == '../../../g' # ok? yes, it confuses you283 url.to_s.should == '/../g' # and it is clearly284# http://a/b/c/d;p?q285# ../../../../g = http://a/../../g286 url = @base_url.merge('../../../../g')287 url.should be_kind_of(URI::HTTP)288 url.to_s.should == 'http://a/g'289 url = @base_url.route_to('http://a/../../g')290 url.should be_kind_of(URI::Generic)291 url.to_s.should_not == '../../../../g' # ok? yes, it confuses you292 url.to_s.should == '/../../g' # and it is clearly293# http://a/b/c/d;p?q294# ./../g = http://a/b/g295 url = @base_url.merge('./../g')296 url.should be_kind_of(URI::HTTP)297 url.to_s.should == 'http://a/b/g'298 url = @base_url.route_to('http://a/b/g')299 url.should be_kind_of(URI::Generic)300 url.to_s.should_not == './../g' # ok301 url.to_s.should == '../g'302# http://a/b/c/d;p?q303# ./g/. = http://a/b/c/g/304 url = @base_url.merge('./g/.')305 url.should be_kind_of(URI::HTTP)306 url.to_s.should == 'http://a/b/c/g/'307 url = @base_url.route_to('http://a/b/c/g/')308 url.should be_kind_of(URI::Generic)309 url.to_s.should_not == './g/.' # ok310 url.to_s.should == 'g/'311# http://a/b/c/d;p?q312# g/./h = http://a/b/c/g/h313 url = @base_url.merge('g/./h')314 url.should be_kind_of(URI::HTTP)315 url.to_s.should == 'http://a/b/c/g/h'316 url = @base_url.route_to('http://a/b/c/g/h')317 url.should be_kind_of(URI::Generic)318 url.to_s.should_not == 'g/./h' # ok319 url.to_s.should == 'g/h'320# http://a/b/c/d;p?q321# g/../h = http://a/b/c/h322 url = @base_url.merge('g/../h')323 url.should be_kind_of(URI::HTTP)324 url.to_s.should == 'http://a/b/c/h'325 url = @base_url.route_to('http://a/b/c/h')326 url.should be_kind_of(URI::Generic)327 url.to_s.should_not == 'g/../h' # ok328 url.to_s.should == 'h'329# http://a/b/c/d;p?q330# g;x=1/./y = http://a/b/c/g;x=1/y331 url = @base_url.merge('g;x=1/./y')332 url.should be_kind_of(URI::HTTP)333 url.to_s.should == 'http://a/b/c/g;x=1/y'334 url = @base_url.route_to('http://a/b/c/g;x=1/y')335 url.should be_kind_of(URI::Generic)336 url.to_s.should_not == 'g;x=1/./y' # ok337 url.to_s.should == 'g;x=1/y'338# http://a/b/c/d;p?q339# g;x=1/../y = http://a/b/c/y340 url = @base_url.merge('g;x=1/../y')341 url.should be_kind_of(URI::HTTP)342 url.to_s.should == 'http://a/b/c/y'343 url = @base_url.route_to('http://a/b/c/y')344 url.should be_kind_of(URI::Generic)345 url.to_s.should_not == 'g;x=1/../y' # ok346 url.to_s.should == 'y'347# http://a/b/c/d;p?q348# g?y/./x = http://a/b/c/g?y/./x349 url = @base_url.merge('g?y/./x')350 url.should be_kind_of(URI::HTTP)351 url.to_s.should == 'http://a/b/c/g?y/./x'352 url = @base_url.route_to('http://a/b/c/g?y/./x')353 url.should be_kind_of(URI::Generic)354 url.to_s.should == 'g?y/./x'355# http://a/b/c/d;p?q356# g?y/../x = http://a/b/c/g?y/../x357 url = @base_url.merge('g?y/../x')358 url.should be_kind_of(URI::HTTP)359 url.to_s.should == 'http://a/b/c/g?y/../x'360 url = @base_url.route_to('http://a/b/c/g?y/../x')361 url.should be_kind_of(URI::Generic)362 url.to_s.should == 'g?y/../x'363# http://a/b/c/d;p?q364# g#s/./x = http://a/b/c/g#s/./x365 url = @base_url.merge('g#s/./x')366 url.should be_kind_of(URI::HTTP)367 url.to_s.should == 'http://a/b/c/g#s/./x'368 url = @base_url.route_to('http://a/b/c/g#s/./x')369 url.should be_kind_of(URI::Generic)370 url.to_s.should == 'g#s/./x'371# http://a/b/c/d;p?q372# g#s/../x = http://a/b/c/g#s/../x373 url = @base_url.merge('g#s/../x')374 url.should be_kind_of(URI::HTTP)375 url.to_s.should == 'http://a/b/c/g#s/../x'376 url = @base_url.route_to('http://a/b/c/g#s/../x')377 url.should be_kind_of(URI::Generic)378 url.to_s.should == 'g#s/../x'379# http://a/b/c/d;p?q380# http:g = http:g ; for validating parsers381# | http://a/b/c/g ; for backwards compatibility382 url = @base_url.merge('http:g')383 url.should be_kind_of(URI::HTTP)384 url.to_s.should == 'http:g'385 url = @base_url.route_to('http:g')386 url.should be_kind_of(URI::Generic)387 url.to_s.should == 'http:g'388 end389end390#TODO: incorporate these tests:391#392# u = URI.parse('http://foo/bar/baz')393# assert_equal(nil, u.merge!(""))394# assert_equal(nil, u.merge!(u))395# assert(nil != u.merge!("."))396# assert_equal('http://foo/bar/', u.to_s)397# assert(nil != u.merge!("../baz"))398# assert_equal('http://foo/baz', u.to_s)...

Full Screen

Full Screen

test_https.rb

Source:test_https.rb Github

copy

Full Screen

1# frozen_string_literal: false2require "test/unit"3begin4 require 'net/https'5 require 'stringio'6 require 'timeout'7 require File.expand_path("utils", File.dirname(__FILE__))8rescue LoadError9 # should skip this test10end11class TestNetHTTPS < Test::Unit::TestCase12 include TestNetHTTPUtils13 def self.fixture(key)14 File.read(File.expand_path("../fixtures/#{key}", __dir__))15 end16 CA_CERT = OpenSSL::X509::Certificate.new(fixture("cacert.pem"))17 SERVER_KEY = OpenSSL::PKey.read(fixture("server.key"))18 SERVER_CERT = OpenSSL::X509::Certificate.new(fixture("server.crt"))19 DHPARAMS = OpenSSL::PKey::DH.new(fixture("dhparams.pem"))20 TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }21 CONFIG = {22 'host' => '127.0.0.1',23 'proxy_host' => nil,24 'proxy_port' => nil,25 'ssl_enable' => true,26 'ssl_certificate' => SERVER_CERT,27 'ssl_private_key' => SERVER_KEY,28 'ssl_tmp_dh_callback' => proc { DHPARAMS },29 }30 def test_get31 http = Net::HTTP.new("localhost", config("port"))32 http.use_ssl = true33 http.cert_store = TEST_STORE34 certs = []35 http.verify_callback = Proc.new do |preverify_ok, store_ctx|36 certs << store_ctx.current_cert37 preverify_ok38 end39 http.request_get("/") {|res|40 assert_equal($test_net_http_data, res.body)41 }42 assert_equal(CA_CERT.to_der, certs[0].to_der)43 assert_equal(SERVER_CERT.to_der, certs[1].to_der)44 rescue SystemCallError45 skip $!46 end47 def test_post48 http = Net::HTTP.new("localhost", config("port"))49 http.use_ssl = true50 http.cert_store = TEST_STORE51 data = config('ssl_private_key').to_der52 http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|53 assert_equal(data, res.body)54 }55 rescue SystemCallError56 skip $!57 end58 def test_session_reuse59 http = Net::HTTP.new("localhost", config("port"))60 http.use_ssl = true61 http.cert_store = TEST_STORE62 http.start63 http.get("/")64 http.finish65 http.start66 http.get("/")67 http.finish # three times due to possible bug in OpenSSL 0.9.868 sid = http.instance_variable_get(:@ssl_session).id69 http.start70 http.get("/")71 socket = http.instance_variable_get(:@socket).io72 assert socket.session_reused?73 assert_equal sid, http.instance_variable_get(:@ssl_session).id74 http.finish75 rescue SystemCallError76 skip $!77 end78 def test_session_reuse_but_expire79 http = Net::HTTP.new("localhost", config("port"))80 http.use_ssl = true81 http.cert_store = TEST_STORE82 http.ssl_timeout = -183 http.start84 http.get("/")85 http.finish86 sid = http.instance_variable_get(:@ssl_session).id87 http.start88 http.get("/")89 socket = http.instance_variable_get(:@socket).io90 assert_equal false, socket.session_reused?91 assert_not_equal sid, http.instance_variable_get(:@ssl_session).id92 http.finish93 rescue SystemCallError94 skip $!95 end96 if ENV["RUBY_OPENSSL_TEST_ALL"]97 def test_verify98 http = Net::HTTP.new("ssl.netlab.jp", 443)99 http.use_ssl = true100 assert(101 (http.request_head("/"){|res| } rescue false),102 "The system may not have default CA certificate store."103 )104 end105 end106 def test_verify_none107 http = Net::HTTP.new("localhost", config("port"))108 http.use_ssl = true109 http.verify_mode = OpenSSL::SSL::VERIFY_NONE110 http.request_get("/") {|res|111 assert_equal($test_net_http_data, res.body)112 }113 rescue SystemCallError114 skip $!115 end116 def test_certificate_verify_failure117 http = Net::HTTP.new("localhost", config("port"))118 http.use_ssl = true119 ex = assert_raise(OpenSSL::SSL::SSLError){120 begin121 http.request_get("/") {|res| }122 rescue SystemCallError123 skip $!124 end125 }126 assert_match(/certificate verify failed/, ex.message)127 unless /mswin|mingw/ =~ RUBY_PLATFORM128 # on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by129 # WEBrick130 @log_tester = lambda {|log|131 assert_equal(1, log.length)132 assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])133 }134 end135 end136 def test_identity_verify_failure137 http = Net::HTTP.new("127.0.0.1", config("port"))138 http.use_ssl = true139 http.verify_callback = Proc.new do |preverify_ok, store_ctx|140 true141 end142 ex = assert_raise(OpenSSL::SSL::SSLError){143 http.request_get("/") {|res| }144 }145 assert_match(/hostname \"127.0.0.1\" does not match/, ex.message)146 end147 def test_timeout_during_SSL_handshake148 bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"149 # listen for connections... but deliberately do not complete SSL handshake150 TCPServer.open('localhost', 0) {|server|151 port = server.addr[1]152 conn = Net::HTTP.new('localhost', port)153 conn.use_ssl = true154 conn.read_timeout = 0.01155 conn.open_timeout = 0.01156 th = Thread.new do157 assert_raise(Net::OpenTimeout) {158 conn.get('/')159 }160 end161 assert th.join(10), bug4246162 }163 end164 def test_min_version165 http = Net::HTTP.new("127.0.0.1", config("port"))166 http.use_ssl = true167 http.min_version = :TLS1168 http.verify_callback = Proc.new do |preverify_ok, store_ctx|169 true170 end171 ex = assert_raise(OpenSSL::SSL::SSLError){172 http.request_get("/") {|res| }173 }174 assert_match(/hostname \"127.0.0.1\" does not match/, ex.message)175 end176 def test_max_version177 http = Net::HTTP.new("127.0.0.1", config("port"))178 http.use_ssl = true179 http.max_version = :SSL2180 http.verify_callback = Proc.new do |preverify_ok, store_ctx|181 true182 end183 @log_tester = lambda {|_| }184 ex = assert_raise(OpenSSL::SSL::SSLError){185 http.request_get("/") {|res| }186 }187 re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version/188 assert_match(re_msg, ex.message)189 end190end if defined?(OpenSSL::SSL)...

Full Screen

Full Screen

request_spec.rb

Source:request_spec.rb Github

copy

Full Screen

1require File.expand_path('../../../../../spec_helper', __FILE__)2require 'net/http'3require File.expand_path('../fixtures/http_server', __FILE__)4describe "Net::HTTP#request" do5 before :each do6 NetHTTPSpecs.start_server7 @http = Net::HTTP.start("localhost", NetHTTPSpecs.port)8 end9 after :each do10 @http.finish if @http.started?11 NetHTTPSpecs.stop_server12 end13 describe "when passed request_object" do14 it "makes a HTTP Request based on the passed request_object" do15 response = @http.request(Net::HTTP::Get.new("/request"), "test=test")16 response.body.should == "Request type: GET"17 response = @http.request(Net::HTTP::Head.new("/request"), "test=test")18 response.body.should be_nil19 response = @http.request(Net::HTTP::Post.new("/request"), "test=test")20 response.body.should == "Request type: POST"21 response = @http.request(Net::HTTP::Put.new("/request"), "test=test")22 response.body.should == "Request type: PUT"23 response = @http.request(Net::HTTP::Proppatch.new("/request"), "test=test")24 response.body.should == "Request type: PROPPATCH"25 response = @http.request(Net::HTTP::Lock.new("/request"), "test=test")26 response.body.should == "Request type: LOCK"27 response = @http.request(Net::HTTP::Unlock.new("/request"), "test=test")28 response.body.should == "Request type: UNLOCK"29 # TODO: Does not work?30 #response = @http.request(Net::HTTP::Options.new("/request"), "test=test")31 #response.body.should be_nil32 response = @http.request(Net::HTTP::Propfind.new("/request"), "test=test")33 response.body.should == "Request type: PROPFIND"34 response = @http.request(Net::HTTP::Delete.new("/request"), "test=test")35 response.body.should == "Request type: DELETE"36 response = @http.request(Net::HTTP::Move.new("/request"), "test=test")37 response.body.should == "Request type: MOVE"38 response = @http.request(Net::HTTP::Copy.new("/request"), "test=test")39 response.body.should == "Request type: COPY"40 response = @http.request(Net::HTTP::Mkcol.new("/request"), "test=test")41 response.body.should == "Request type: MKCOL"42 response = @http.request(Net::HTTP::Trace.new("/request"), "test=test")43 response.body.should == "Request type: TRACE"44 end45 end46 describe "when passed request_object and request_body" do47 it "sends the passed request_body when making the HTTP Request" do48 response = @http.request(Net::HTTP::Get.new("/request/body"), "test=test")49 response.body.should == "test=test"50 response = @http.request(Net::HTTP::Head.new("/request/body"), "test=test")51 response.body.should be_nil52 response = @http.request(Net::HTTP::Post.new("/request/body"), "test=test")53 response.body.should == "test=test"54 response = @http.request(Net::HTTP::Put.new("/request/body"), "test=test")55 response.body.should == "test=test"56 response = @http.request(Net::HTTP::Proppatch.new("/request/body"), "test=test")57 response.body.should == "test=test"58 response = @http.request(Net::HTTP::Lock.new("/request/body"), "test=test")59 response.body.should == "test=test"60 response = @http.request(Net::HTTP::Unlock.new("/request/body"), "test=test")61 response.body.should == "test=test"62 # TODO: Does not work?63 #response = @http.request(Net::HTTP::Options.new("/request/body"), "test=test")64 #response.body.should be_nil65 response = @http.request(Net::HTTP::Propfind.new("/request/body"), "test=test")66 response.body.should == "test=test"67 response = @http.request(Net::HTTP::Delete.new("/request/body"), "test=test")68 response.body.should == "test=test"69 response = @http.request(Net::HTTP::Move.new("/request/body"), "test=test")70 response.body.should == "test=test"71 response = @http.request(Net::HTTP::Copy.new("/request/body"), "test=test")72 response.body.should == "test=test"73 response = @http.request(Net::HTTP::Mkcol.new("/request/body"), "test=test")74 response.body.should == "test=test"75 response = @http.request(Net::HTTP::Trace.new("/request/body"), "test=test")76 response.body.should == "test=test"77 end78 end79end...

Full Screen

Full Screen

new_spec.rb

Source:new_spec.rb Github

copy

Full Screen

1require File.expand_path('../../../../../spec_helper', __FILE__)2require 'net/http'3describe "Net::HTTP.new" do4 describe "when passed address" do5 before :each do6 @http = Net::HTTP.new("localhost")7 end8 it "returns a Net::HTTP instance" do9 @http.proxy?.should be_false10 @http.instance_of?(Net::HTTP).should be_true11 end12 it "sets the new Net::HTTP instance's address to the passed address" do13 @http.address.should == "localhost"14 end15 it "sets the new Net::HTTP instance's port to the default HTTP port" do16 @http.port.should eql(Net::HTTP.default_port)17 end18 it "does not start the new Net::HTTP instance" do19 @http.started?.should be_false20 end21 end22 describe "when passed address, port" do23 before :each do24 @http = Net::HTTP.new("localhost", 3333)25 end26 it "returns a Net::HTTP instance" do27 @http.proxy?.should be_false28 @http.instance_of?(Net::HTTP).should be_true29 end30 it "sets the new Net::HTTP instance's address to the passed address" do31 @http.address.should == "localhost"32 end33 it "sets the new Net::HTTP instance's port to the passed port" do34 @http.port.should eql(3333)35 end36 it "does not start the new Net::HTTP instance" do37 @http.started?.should be_false38 end39 end40 describe "when passed address, port, *proxy_options" do41 it "returns a Net::HTTP instance" do42 http = Net::HTTP.new("localhost", 3333, "localhost")43 http.proxy?.should be_true44 http.instance_of?(Net::HTTP).should be_true45 http.should be_kind_of(Net::HTTP)46 end47 it "correctly sets the passed Proxy options" do48 http = Net::HTTP.new("localhost", 3333, "localhost")49 http.proxy_address.should == "localhost"50 http.proxy_port.should eql(80)51 http.proxy_user.should be_nil52 http.proxy_pass.should be_nil53 http = Net::HTTP.new("localhost", 3333, "localhost", 1234)54 http.proxy_address.should == "localhost"55 http.proxy_port.should eql(1234)56 http.proxy_user.should be_nil57 http.proxy_pass.should be_nil58 http = Net::HTTP.new("localhost", 3333, "localhost", 1234, "rubyspec")59 http.proxy_address.should == "localhost"60 http.proxy_port.should eql(1234)61 http.proxy_user.should == "rubyspec"62 http.proxy_pass.should be_nil63 http = Net::HTTP.new("localhost", 3333, "localhost", 1234, "rubyspec", "rocks")64 http.proxy_address.should == "localhost"65 http.proxy_port.should eql(1234)66 http.proxy_user.should == "rubyspec"67 http.proxy_pass.should == "rocks"68 end69 end70end...

Full Screen

Full Screen

normalization.rb

Source:normalization.rb Github

copy

Full Screen

1module URISpec2 # Not an exhaustive list. Refer to rfc39863 NORMALIZED_FORMS = [4 { normalized: "http://example.com/",5 equivalent: %w{ hTTp://example.com/6 http://exaMple.com/7 http://exa%4dple.com/8 http://exa%4Dple.com/9 http://exa%6dple.com/10 http://exa%6Dple.com/11 http://@example.com/12 http://example.com:/13 http://example.com:80/14 http://example.com15 },16 different: %w{ http://example.com/#17 http://example.com/?18 http://example.com:8888/19 http:///example.com20 http:example.com21 https://example.com/22 },23 },24 { normalized: "http://example.com/index.html",25 equivalent: %w{ http://example.com/index.ht%6dl26 http://example.com/index.ht%6Dl27 },28 different: %w{ http://example.com/index.hTMl29 http://example.com/index.ht%4dl30 http://example.com/index31 http://example.com/32 http://example.com/33 },34 },35 { normalized: "http://example.com/x?y#z",36 equivalent: %w{ http://example.com/x?y#%7a37 http://example.com/x?y#%7A38 http://example.com/x?%79#z39 },40 different: %w{ http://example.com/x?Y#z41 http://example.com/x?y#Z42 http://example.com/x?y=#z43 http://example.com/x?y44 http://example.com/x#z45 },46 },47 { normalized: "http://example.com/x?q=a%20b",48 equivalent: %w{49 },50 different: %w{ http://example.com/x?q=a+b51 },52 },53 ]54end...

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1require_once 'atoum/http/classes/autoloader.php';2atoum\http\classes\autoloader::get()3 ->addDirectory(__DIR__.'/atoum/http/classes')4;5require_once 'atoum/atoum/classes/autoloader.php';6atoum\atoum\classes\autoloader::get()7 ->addDirectory(__DIR__.'/atoum/atoum/classes')8;9require_once 'atoum/http/test/classes/autoloader.php';10atoum\http\test\classes\autoloader::get()11 ->addDirectory(__DIR__.'/atoum/http/test/classes')12;13require_once 'atoum/http/test/units/classes/autoloader.php';14atoum\http\test\units\classes\autoloader::get()15 ->addDirectory(__DIR__.'/atoum/http/test/units/classes')16;17require_once 'atoum/http/test/units/asserters/classes/autoloader.php';18atoum\http\test\units\asserters\classes\autoloader::get()19 ->addDirectory(__DIR__.'/atoum/http/test/units/asserters/classes')20;21require_once 'atoum/http/test/units/asserters/adapter/classes/autoloader.php';22atoum\http\test\units\asserters\adapter\classes\autoloader::get()23 ->addDirectory(__DIR__.'/atoum/http/test/units/asserters/adapter/classes')24;25require_once 'atoum/http/test/units/asserters/adapter/classes/autoloader.php';26atoum\http\test\units\asserters\adapter\classes\autoloader::get()27 ->addDirectory(__DIR__.'/atoum/http/test/units/asserters/adapter/classes')28;

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1namespace atoum\http\tests\units\classes;2use atoum\http\classes as http;3{4 public function testGet()5 {6 ->given($this->newTestedInstance)7 ->isInstanceOf('atoum\http\classes\http')8 ;9 }10}11namespace atoum\http\tests\units\classes;12use atoum\http\classes as http;13{14 public function testGet()15 {16 ->given($this->newTestedInstance)17 ->isInstanceOf('atoum\http\classes\http')18 ;19 }20}21Last edited by jblond (2012-12-07 13:31:48)

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1namespace Atoum\AtoumBundle\Tests\Units\Classes;2use atoum\atoum;3{4 public function testGet()5 {6 ->given(7 ->when(8 $this->testedInstance->get()9 ->boolean($this->testedInstance->isSuccess())10 ->isTrue()11 ;12 }13}14namespace Atoum\AtoumBundle\Tests\Units\Classes;15use atoum\atoum;16{17 public function testGet()18 {19 ->given(20 ->when(21 $this->testedInstance->get()22 ->boolean($this->testedInstance->isSuccess())23 ->isTrue()24 ;25 }26}27namespace Atoum\AtoumBundle\Tests\Units\Classes;28use atoum\atoum;29{30 public function testGet()31 {32 ->given(33 ->when(34 $this->testedInstance->get()35 ->boolean($this->testedInstance->isSuccess())36 ->isTrue()37 ;38 }39}40namespace Atoum\AtoumBundle\Tests\Units\Classes;41use atoum\atoum;42{43 public function testGet()44 {45 ->given(46 ->when(47 $this->testedInstance->get()48 ->boolean($this->testedInstance->isSuccess())49 ->isTrue()50 ;51 }52}

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1$atoum = new \atoum\atoum\http();2$atoum = new atoum\atoum\http();3$atoum = new atoum\http();4$atoum = new http();5$atoum = new \atoum\atoum\http();6$atoum = new atoum\atoum\http();7$atoum = new atoum\http();8$atoum = new http();9$atoum = new \atoum\atoum\http();10$atoum = new atoum\atoum\http();11$atoum = new atoum\http();12$atoum = new http();13$atoum = new \atoum\atoum\http();14$atoum = new atoum\atoum\http();15$atoum = new atoum\http();16$atoum = new http();17$atoum = new \atoum\atoum\http();18$atoum = new atoum\atoum\http();19$atoum = new atoum\http();

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1require_once 'atoum/classes/http.php';2$http = new http();3echo $http->body;4require_once 'atoum/classes/http.php';5$http = new http();6echo $http->body;7require_once 'atoum/classes/http.php';8$http = new http();9echo $http->body;10require_once 'atoum/classes/http.php';11$http = new http();12echo $http->body;13require_once 'atoum/classes/http.php';14$http = new http();15echo $http->body;16require_once 'atoum/classes/http.php';17$http = new http();18echo $http->body;19require_once 'atoum/classes/http.php';20$http = new http();21echo $http->body;22require_once 'atoum/classes/http.php';23$http = new http();24echo $http->body;25require_once 'atoum/classes/http.php';26$http = new http();27echo $http->body;28require_once 'atoum/classes/http.php';29$http = new http();

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1{2public function get($url, $parameters = array(), $files = array(), $headers = array(), $cookies = array())3{4return parent::get($url, $parameters, $files, $headers, $cookies);5}6}7$test = new http();

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1$httpClient = new \atoum\http\client();2$httpClient->get('1.php');3var_dump($httpClient->getResponseBody());4$httpClient = new \atoum\http\client();5$httpClient->get('2.php');6var_dump($httpClient->getResponseBody());7$httpClient = new \atoum\http\client();8$httpClient->get('3.php');9var_dump($httpClient->getResponseBody());10$httpClient = new \atoum\http\client();11$httpClient->get('4.php');12var_dump($httpClient->getResponseBody());13$httpClient = new \atoum\http\client();14$httpClient->get('5.php');15var_dump($httpClient->getResponseBody());16$httpClient = new \atoum\http\client();17$httpClient->get('6.php');18var_dump($httpClient->getResponseBody());19$httpClient = new \atoum\http\client();20$httpClient->get('7.php');21var_dump($httpClient->getResponseBody());

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1require_once 'Atoum/Http.php';2$http = new Atoum_Http();3$response = $http->get($url);4echo $response;5require_once 'Atoum/Http.php';6$http = new Atoum_Http();7$response = $http->post($url, array('search' => 'php'));8echo $response;9require_once 'Atoum/Http.php';10$http = new Atoum_Http();11$response = $http->get($url);12$header = $response->getHeader();13$body = $response->getBody();14echo $header;15echo $body;16getHeader()17getBody()18getHeaderField($field)19getHeaderFields()20getHeaderFieldNames()21getHeaderFieldValues($field)22getHeaderFieldValuesByName($name)23getHeaderFieldValuesByNames($names)24getHeaderFieldValuesByRegex($regex)25getHeaderFieldValuesByRegexes($regexes)26getHeaderFieldValuesByRegexes($regexes)27require_once 'Atoum/Http.php';28$http = new Atoum_Http();29$response = $http->get($url);30$header = $response->getHeader();31$body = $response->getBody();32echo $header;33echo $body;

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 Atoum automation tests on LambdaTest cloud grid

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

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful