How to use http method of Curl Package

Best Webmock_ruby code snippet using Curl.http

tc_curl_easy.rb

Source:tc_curl_easy.rb Github

copy

Full Screen

...8 # in a Timeout block you should reset the thread current handle 9 Curl.reset10 end11 def test_nested_easy_methods12 easy = Curl.get(TestServlet.url) {|http|13 res = Curl.get(TestServlet.url + '/not_here')14 assert_equal 404, res.response_code15 }16 assert_equal 200, easy.response_code17 end18 def test_curlopt_stderr_with_file19 # does not work with Tempfile directly20 path = Tempfile.new('curb_test_curlopt_stderr').path21 File.open(path, 'w') do |file|22 easy = Curl::Easy.new(TestServlet.url)23 easy.verbose = true24 easy.setopt(Curl::CURLOPT_STDERR, file)25 easy.perform26 end27 output = File.read(path)28 assert_match(/HTTP\/1\.1\ 200\ OK(?:\ )?/, output)29 assert_match('Host: 127.0.0.1:9129', output)30 end31 def test_curlopt_stderr_with_io32 path = Tempfile.new('curb_test_curlopt_stderr').path33 fd = IO.sysopen(path, 'w')34 io = IO.for_fd(fd)35 easy = Curl::Easy.new(TestServlet.url)36 easy.verbose = true37 easy.setopt(Curl::CURLOPT_STDERR, io)38 easy.perform39 output = File.read(path)40 assert_match(output, 'HTTP/1.1 200 OK')41 assert_match(output, 'Host: 127.0.0.1:9129')42 end43 def test_curlopt_stderr_fails_with_tempdir44 Tempfile.open('curb_test_curlopt_stderr') do |tempfile|45 easy = Curl::Easy.new(TestServlet.url)46 assert_raise(TypeError) do47 easy.setopt(Curl::CURLOPT_STDERR, tempfile)48 end49 end50 end51 def test_curlopt_stderr_fails_with_stringio52 stringio = StringIO.new53 easy = Curl::Easy.new(TestServlet.url)54 assert_raise(TypeError) do55 easy.setopt(Curl::CURLOPT_STDERR, stringio)56 end57 end58 def test_curlopt_stderr_fails_with_string59 string = String.new60 easy = Curl::Easy.new(TestServlet.url)61 assert_raise(TypeError) do62 easy.setopt(Curl::CURLOPT_STDERR, string)63 end64 end65 def test_exception66 begin67 Curl.get('NOT_FOUND_URL')68 rescue69 assert true70 rescue Exception71 assert false, "We should raise StandardError"72 end73 end74 def test_threads75 t = []76 5.times do77 t << Thread.new do78 5.times do79 c = Curl.get($TEST_URL)80 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)81 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)82 end83 end84 end85 t.each {|x| x.join }86 end87 def test_class_perform_01 88 assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL)89 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)90 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body)91 end 92 def test_class_perform_0293 data = ""94 assert_instance_of Curl::Easy, c = Curl::Easy.perform($TEST_URL) { |curl| curl.on_body { |d| data << d; d.length } } 95 assert_nil c.body_str96 assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)97 end 98 def test_class_perform_0399 assert_raise(Curl::Err::CouldntReadError) { Curl::Easy.perform($TEST_URL + "nonexistent") }100 end 101 102 def test_new_01103 c = Curl::Easy.new104 assert_equal Curl::Easy, c.class105 assert_nil c.url106 assert_nil c.body_str107 assert_nil c.header_str108 end109 def test_new_02110 c = Curl::Easy.new($TEST_URL)111 assert_equal $TEST_URL, c.url112 end113 114 def test_new_03115 blk = lambda { |i| i.length }116 117 c = Curl::Easy.new do |curl|118 curl.on_body(&blk)119 end120 121 assert_nil c.url 122 assert_equal blk, c.on_body # sets handler nil, returns old handler123 assert_equal nil, c.on_body124 end125 126 def test_new_04127 blk = lambda { |i| i.length }128 129 c = Curl::Easy.new($TEST_URL) do |curl|130 curl.on_body(&blk)131 end132 133 assert_equal $TEST_URL, c.url134 assert_equal blk, c.on_body # sets handler nil, returns old handler135 assert_equal nil, c.on_body136 end137 class Foo < Curl::Easy ; end138 def test_new_05139 # can use Curl::Easy as a base class140 c = Foo.new141 assert_equal Foo, c.class142 c.url = $TEST_URL143 c.perform144 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)145 end146 # test invalid use of new147 def test_new_06148 Curl::Easy.new(TestServlet.url) do|curl|149 curl.http_post150 assert_equal "POST\n", curl.body_str151 end152 end153 def test_escape154 c = Curl::Easy.new155 156 assert_equal "one%20two", c.escape('one two')157 assert_equal "one%00two%20three", c.escape("one\000two three") 158 end159 160 def test_unescape161 c = Curl::Easy.new162 163 assert_equal "one two", c.unescape('one%20two')164 165 # prior to 7.15.4 embedded nulls cannot be unescaped166 if Curl::VERNUM >= 0x070f04167 assert_equal "one\000two three", c.unescape("one%00two%20three")168 end169 end170 171 def test_headers172 c = Curl::Easy.new($TEST_URL)173 174 assert_equal({}, c.headers)175 c.headers = "Expect:"176 assert_equal "Expect:", c.headers177 c.headers = ["Expect:", "User-Agent: myapp-0.0.0"]178 assert_equal ["Expect:", "User-Agent: myapp-0.0.0"], c.headers179 end 180 def test_get_01 181 c = Curl::Easy.new($TEST_URL) 182 assert_equal true, c.http_get183 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)184 end 185 def test_get_02186 data = ""187 c = Curl::Easy.new($TEST_URL) do |curl|188 curl.on_body { |d| data << d; d.length }189 end190 191 assert_equal true, c.http_get 192 193 assert_nil c.body_str194 assert_match(/^# DO NOT REMOVE THIS COMMENT/, data)195 end 196 def test_get_03197 c = Curl::Easy.new($TEST_URL + "nonexistent") 198 assert_raise(Curl::Err::CouldntReadError) { c.http_get }199 assert_equal "", c.body_str200 assert_equal "", c.header_str201 end 202 def test_last_effective_url_01203 c = Curl::Easy.new($TEST_URL)204 205 assert_equal $TEST_URL, c.url206 assert_nil c.last_effective_url207 208 assert c.http_get209 210 assert_equal c.url, c.last_effective_url211 c.url = "file://some/new.url"212 213 assert_not_equal c.last_effective_url, c.url214 end215 def test_http_get_block216 curl = Curl::Easy.http_get(TestServlet.url) do|c|217 c.follow_location = true218 c.max_redirects = 3219 end220 assert_equal curl.url, curl.last_effective_url221 assert_equal 'GET', curl.body_str222 end223 224 def test_local_port_01225 c = Curl::Easy.new($TEST_URL)226 227 assert_nil c.local_port 228 assert_nil c.local_port_range229 assert_nil c.proxy_port230 231 c.local_port = 88232 assert_equal 88, c.local_port 233 assert_nil c.local_port_range234 assert_nil c.proxy_port235 236 c.local_port = nil237 assert_nil c.local_port 238 assert_nil c.local_port_range239 assert_nil c.proxy_port240 end241 242 def test_local_port_02243 c = Curl::Easy.new($TEST_URL)244 245 assert_nil c.local_port 246 assert_raise(ArgumentError) { c.local_port = 0 }247 assert_raise(ArgumentError) { c.local_port = 65536 }248 assert_raise(ArgumentError) { c.local_port = -1 }249 end250 251 def test_local_port_range_01252 c = Curl::Easy.new($TEST_URL)253 254 assert_nil c.local_port_range255 assert_nil c.local_port256 assert_nil c.proxy_port257 c.local_port_range = 88258 assert_equal 88, c.local_port_range259 assert_nil c.local_port260 assert_nil c.proxy_port261 262 c.local_port_range = nil263 264 assert_nil c.local_port_range265 assert_nil c.local_port266 assert_nil c.proxy_port267 end268 269 def test_local_port_range_02270 c = Curl::Easy.new($TEST_URL)271 272 assert_nil c.local_port_range 273 assert_raise(ArgumentError) { c.local_port_range = 0 }274 assert_raise(ArgumentError) { c.local_port_range = 65536 }275 assert_raise(ArgumentError) { c.local_port_range = -1 }276 end277 278 def test_proxy_url_01279 c = Curl::Easy.new($TEST_URL)280 281 assert_equal $TEST_URL, c.url282 assert_nil c.proxy_url283 284 c.proxy_url = "http://some.proxy" 285 assert_equal $TEST_URL, c.url286 assert_equal "http://some.proxy", c.proxy_url287 288 c.proxy_url = nil289 assert_equal $TEST_URL, c.url290 assert_nil c.proxy_url291 end292 293 def test_proxy_port_01294 c = Curl::Easy.new($TEST_URL)295 296 assert_nil c.local_port297 assert_nil c.local_port_range 298 assert_nil c.proxy_port 299 300 c.proxy_port = 88301 assert_equal 88, c.proxy_port 302 assert_nil c.local_port303 assert_nil c.local_port_range304 305 c.proxy_port = nil306 assert_nil c.proxy_port 307 assert_nil c.local_port308 assert_nil c.local_port_range309 end310 311 def test_proxy_port_02312 c = Curl::Easy.new($TEST_URL)313 314 assert_nil c.proxy_port 315 assert_raise(ArgumentError) { c.proxy_port = 0 }316 assert_raise(ArgumentError) { c.proxy_port = 65536 }317 assert_raise(ArgumentError) { c.proxy_port = -1 }318 end319 320 def test_proxy_type_01321 c = Curl::Easy.new($TEST_URL)322 323 assert_nil c.proxy_type324 325 c.proxy_type = 3326 assert_equal 3, c.proxy_type327 328 c.proxy_type = nil329 assert_nil c.proxy_type330 end331 332 def test_http_auth_types_01333 c = Curl::Easy.new($TEST_URL)334 335 assert_nil c.http_auth_types336 337 c.http_auth_types = 3338 assert_equal 3, c.http_auth_types339 340 c.http_auth_types = nil341 assert_nil c.http_auth_types342 end343 344 def test_proxy_auth_types_01345 c = Curl::Easy.new($TEST_URL)346 347 assert_nil c.proxy_auth_types348 349 c.proxy_auth_types = 3350 assert_equal 3, c.proxy_auth_types351 352 c.proxy_auth_types = nil353 assert_nil c.proxy_auth_types354 end355 356 def test_max_redirects_01357 c = Curl::Easy.new($TEST_URL)358 359 assert_nil c.max_redirects360 361 c.max_redirects = 3362 assert_equal 3, c.max_redirects363 364 c.max_redirects = nil365 assert_nil c.max_redirects366 end367 def test_timeout_with_floats368 c = Curl::Easy.new($TEST_URL)369 c.timeout = 1.5370 assert_equal 1500, c.timeout_ms371 assert_equal 1.5, c.timeout372 end373 def test_timeout_with_negative374 c = Curl::Easy.new($TEST_URL)375 c.timeout = -1.5376 assert_equal 0, c.timeout377 assert_equal 0, c.timeout_ms378 c.timeout = -4.8379 assert_equal 0, c.timeout380 assert_equal 0, c.timeout_ms381 end382 def test_timeout_01383 c = Curl::Easy.new($TEST_URL)384 assert_equal 0, c.timeout385 c.timeout = 3386 assert_equal 3, c.timeout387 c.timeout = 0388 assert_equal 0, c.timeout389 end390 def test_timeout_ms_01391 c = Curl::Easy.new($TEST_URL)392 assert_equal 0, c.timeout_ms393 c.timeout_ms = 100394 assert_equal 100, c.timeout_ms395 c.timeout_ms = nil396 assert_equal 0, c.timeout_ms397 end398 def test_timeout_ms_with_floats399 c = Curl::Easy.new($TEST_URL)400 c.timeout_ms = 55.5401 assert_equal 55, c.timeout_ms402 assert_equal 0.055, c.timeout403 end404 def test_timeout_ms_with_negative405 c = Curl::Easy.new($TEST_URL)406 c.timeout_ms = -1.5407 assert_equal 0, c.timeout408 assert_equal 0, c.timeout_ms409 c.timeout_ms = -4.8410 assert_equal 0, c.timeout411 assert_equal 0, c.timeout_ms412 end413 def test_connect_timeout_01414 c = Curl::Easy.new($TEST_URL)415 416 assert_nil c.connect_timeout417 418 c.connect_timeout = 3419 assert_equal 3, c.connect_timeout420 421 c.connect_timeout = nil422 assert_nil c.connect_timeout423 end424 def test_connect_timeout_ms_01425 c = Curl::Easy.new($TEST_URL)426 assert_nil c.connect_timeout_ms427 c.connect_timeout_ms = 100428 assert_equal 100, c.connect_timeout_ms429 c.connect_timeout_ms = nil430 assert_nil c.connect_timeout_ms431 end432 def test_ftp_response_timeout_01433 c = Curl::Easy.new($TEST_URL)434 435 assert_nil c.ftp_response_timeout436 437 c.ftp_response_timeout = 3438 assert_equal 3, c.ftp_response_timeout439 440 c.ftp_response_timeout = nil441 assert_nil c.ftp_response_timeout442 end443 444 def test_dns_cache_timeout_01445 c = Curl::Easy.new($TEST_URL)446 447 assert_equal 60, c.dns_cache_timeout448 449 c.dns_cache_timeout = nil450 assert_nil c.dns_cache_timeout451 452 c.dns_cache_timeout = 30453 assert_equal 30, c.dns_cache_timeout454 end455 456 def test_low_speed_limit_01457 c = Curl::Easy.new($TEST_URL)458 459 assert_nil c.low_speed_limit460 461 c.low_speed_limit = 3462 assert_equal 3, c.low_speed_limit463 464 c.low_speed_limit = nil465 assert_nil c.low_speed_limit466 end467 468 def test_low_speed_time_01469 c = Curl::Easy.new($TEST_URL)470 471 assert_nil c.low_speed_time472 473 c.low_speed_time = 3474 assert_equal 3, c.low_speed_time475 476 c.low_speed_time = nil477 assert_nil c.low_speed_time478 end479 480 def test_on_body481 blk = lambda { |i| i.length }482 483 c = Curl::Easy.new 484 c.on_body(&blk)485 486 assert_equal blk, c.on_body # sets handler nil, returns old handler487 assert_equal nil, c.on_body488 end489 def test_inspect_with_no_url490 c = Curl::Easy.new491 assert_equal '#<Curl::Easy>', c.inspect492 end493 494 def test_inspect_with_short_url495 c = Curl::Easy.new('http://www.google.com/')496 assert_equal "#<Curl::Easy http://www.google.com/>", c.inspect497 end498 499 def test_inspect_truncates_to_64_chars500 base_url = 'http://www.google.com/'501 truncated_url = base_url + 'x' * (64 - '#<Curl::Easy >'.size - base_url.size)502 long_url = truncated_url + 'yyyy'503 c = Curl::Easy.new(long_url)504 assert_equal 64, c.inspect.size505 assert_equal "#<Curl::Easy #{truncated_url}>", c.inspect506 end507 508 def test_on_header509 blk = lambda { |i| i.length }510 511 c = Curl::Easy.new 512 c.on_header(&blk)513 514 assert_equal blk, c.on_header # sets handler nil, returns old handler515 assert_equal nil, c.on_header516 end517 518 def test_on_progress519 blk = lambda { |*args| true }520 521 c = Curl::Easy.new 522 c.on_progress(&blk)523 524 assert_equal blk, c.on_progress # sets handler nil, returns old handler525 assert_equal nil, c.on_progress526 end527 528 def test_on_debug529 blk = lambda { |*args| true }530 531 c = Curl::Easy.new 532 c.on_debug(&blk)533 534 assert_equal blk, c.on_debug # sets handler nil, returns old handler535 assert_equal nil, c.on_debug536 end537 538 def test_proxy_tunnel539 c = Curl::Easy.new 540 assert !c.proxy_tunnel?541 assert c.proxy_tunnel = true542 assert c.proxy_tunnel?543 end544 545 def test_fetch_file_time546 c = Curl::Easy.new 547 assert !c.fetch_file_time?548 assert c.fetch_file_time = true549 assert c.fetch_file_time?550 end551 552 def test_ssl_verify_peer553 c = Curl::Easy.new 554 assert c.ssl_verify_peer?555 assert !c.ssl_verify_peer = false556 assert !c.ssl_verify_peer?557 end558 559 def test_ssl_verify_host560 c = Curl::Easy.new 561 assert c.ssl_verify_host?562 c.ssl_verify_host = 0563 c.ssl_verify_host = false564 assert !c.ssl_verify_host?565 end566 567 def test_header_in_body568 c = Curl::Easy.new 569 assert !c.header_in_body?570 assert c.header_in_body = true571 assert c.header_in_body?572 end573 574 def test_use_netrc575 c = Curl::Easy.new 576 assert !c.use_netrc?577 assert c.use_netrc = true578 assert c.use_netrc?579 end580 581 def test_follow_location582 c = Curl::Easy.new 583 assert !c.follow_location?584 assert c.follow_location = true585 assert c.follow_location?586 end587 588 def test_unrestricted_auth589 c = Curl::Easy.new 590 assert !c.unrestricted_auth?591 assert c.unrestricted_auth = true592 assert c.unrestricted_auth?593 end 594 595 def test_multipart_form_post596 c = Curl::Easy.new597 assert !c.multipart_form_post?598 assert c.multipart_form_post = true599 assert c.multipart_form_post?600 end601 def test_ignore_content_length602 c = Curl::Easy.new603 assert !c.ignore_content_length?604 assert c.ignore_content_length = true605 assert c.ignore_content_length?606 end607 def test_resolve_mode608 c = Curl::Easy.new609 assert_equal :auto, c.resolve_mode610 c.resolve_mode = :ipv4611 assert_equal :ipv4, c.resolve_mode 612 c.resolve_mode = :ipv6613 assert_equal :ipv6, c.resolve_mode 614 assert_raises(ArgumentError) { c.resolve_mode = :bad }615 end616 def test_enable_cookies617 c = Curl::Easy.new618 assert !c.enable_cookies?619 assert c.enable_cookies = true620 assert c.enable_cookies?621 end622 def test_cookies_option623 c = Curl::Easy.new624 assert_nil c.cookies625 assert_equal "name1=content1; name2=content2;", c.cookies = "name1=content1; name2=content2;"626 assert_equal "name1=content1; name2=content2;", c.cookies627 end628 def test_cookiefile629 c = Curl::Easy.new630 assert_nil c.cookiefile631 assert_equal "some.file", c.cookiefile = "some.file"632 assert_equal "some.file", c.cookiefile 633 end634 def test_cookiejar635 c = Curl::Easy.new636 assert_nil c.cookiejar637 assert_equal "some.file", c.cookiejar = "some.file"638 assert_equal "some.file", c.cookiejar 639 end640 def test_cookielist641 c = Curl::Easy.new TestServlet.url642 c.enable_cookies = true643 c.post_body = URI.encode_www_form('c' => 'somename=somevalue')644 assert_nil c.cookielist645 c.perform646 assert_match(/somevalue/, c.cookielist.join(''))647 end648 def test_on_success649 curl = Curl::Easy.new($TEST_URL) 650 on_success_called = false651 curl.on_success {|c|652 on_success_called = true653 assert_not_nil c.body_str654 assert_equal "", c.header_str655 assert_match(/^# DO NOT REMOVE THIS COMMENT/, c.body_str)656 }657 curl.perform658 assert on_success_called, "Success handler not called" 659 end660 def test_on_success_with_on_failure661 curl = Curl::Easy.new(TestServlet.url + '/error')662 on_failure_called = false663 curl.on_success {|c| } # make sure we get the failure call even though this handler is defined664 curl.on_failure {|c,code| on_failure_called = true }665 curl.perform666 assert_equal 500, curl.response_code667 assert on_failure_called, "Failure handler not called" 668 end669 def test_on_success_with_on_missing670 curl = Curl::Easy.new(TestServlet.url + '/not_here')671 on_missing_called = false672 curl.on_success {|c| } # make sure we get the missing call even though this handler is defined673 curl.on_missing {|c,code| on_missing_called = true }674 curl.perform675 assert_equal 404, curl.response_code676 assert on_missing_called, "Missing handler not called" 677 end678 def test_on_success_with_on_redirect679 curl = Curl::Easy.new(TestServlet.url + '/redirect')680 on_redirect_called = false681 curl.on_success {|c| } # make sure we get the redirect call even though this handler is defined682 curl.on_redirect {|c,code| on_redirect_called = true }683 curl.perform684 assert_equal 302, curl.response_code685 assert on_redirect_called, "Redirect handler not called" 686 end687 688 def test_get_remote689 curl = Curl::Easy.new(TestServlet.url)690 curl.http_get691 assert_equal 'GET', curl.body_str692 end693 694 def test_post_remote695 curl = Curl::Easy.new(TestServlet.url)696 curl.http_post([Curl::PostField.content('document_id', 5)])697 assert_equal "POST\ndocument_id=5", curl.unescape(curl.body_str)698 end699 def test_post_remote_is_easy_handle700 # see: http://pastie.org/560852 and701 # http://groups.google.com/group/curb---ruby-libcurl-bindings/browse_thread/thread/216bb2d9b037f347?hl=en702 [:post, :get, :head, :delete].each do |method|703 retries = 0704 begin705 count = 0706 Curl::Easy.send("http_#{method}", TestServlet.url) do|c|707 count += 1708 assert_equal Curl::Easy, c.class709 end710 assert_equal 1, count, "For request method: #{method.to_s.upcase}"711 rescue Curl::Err::HostResolutionError => e # travis-ci.org fails to resolve... try again?712 retries+=1713 retry if retries < 3714 raise e715 end716 end717 end718 # see: https://github.com/rvanlieshout/curb/commit/8bcdefddc0162484681ebd1a92d52a642666a445719 def test_post_multipart_array_remote720 curl = Curl::Easy.new(TestServlet.url)721 curl.multipart_form_post = true722 fields = [723 Curl::PostField.file('foo', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown'))),724 Curl::PostField.file('bar', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))725 ]726 curl.http_post(fields)727 assert_match(/HTTP POST file upload/, curl.body_str)728 assert_match(/Content-Disposition: form-data/, curl.body_str)729 end730 731 def test_post_with_body_remote732 curl = Curl::Easy.new(TestServlet.url)733 curl.post_body = 'foo=bar&encoded%20string=val'734 735 curl.perform736 737 assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str738 assert_equal 'foo=bar&encoded%20string=val', curl.post_body739 end740 741 def test_form_post_body_remote742 curl = Curl::Easy.new(TestServlet.url)743 curl.http_post('foo=bar', 'encoded%20string=val')744 745 assert_equal "POST\nfoo=bar&encoded%20string=val", curl.body_str746 assert_equal 'foo=bar&encoded%20string=val', curl.post_body747 end748 def test_post_multipart_file_remote749 curl = Curl::Easy.new(TestServlet.url)750 curl.multipart_form_post = true751 pf = Curl::PostField.file('readme', File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown')))752 curl.http_post(pf)753 assert_match(/HTTP POST file upload/, curl.body_str)754 assert_match(/Content-Disposition: form-data/, curl.body_str)755 end756 def test_delete_remote757 curl = Curl::Easy.new(TestServlet.url)758 curl.http_delete759 assert_equal 'DELETE', curl.body_str760 end761 def test_arbitrary_http_verb762 curl = Curl::Easy.new(TestServlet.url)763 curl.http('PURGE')764 assert_equal 'PURGE', curl.body_str765 end766 def test_head_remote767 curl = Curl::Easy.new(TestServlet.url)768 curl.http_head769 redirect = curl.header_str.match(/Location: (.*)/)770 assert_equal '', curl.body_str771 assert_match('/nonexistent', redirect[1])772 end773 def test_head_accessor774 curl = Curl::Easy.new(TestServlet.url)775 curl.head = true776 curl.perform777 redirect = curl.header_str.match(/Location: (.*)/)778 assert_equal '', curl.body_str779 assert_match('/nonexistent', redirect[1])780 curl.head = false781 curl.perform782 assert_equal 'GET', curl.body_str783 end784 def test_put_remote785 curl = Curl::Easy.new(TestServlet.url)786 curl.headers['Content-Type'] = 'application/json'787 assert curl.http_put("message")788 assert_match(/^PUT/, curl.body_str)789 assert_match(/message$/, curl.body_str)790 assert_match(/message$/, curl.body)791 assert_match(/application\/json/, curl.header_str)792 assert_match(/application\/json/, curl.head)793 end 794 795 def test_put_data796 curl = Curl::Easy.new(TestServlet.url)797 curl.put_data = 'message'798 799 curl.perform800 801 assert_match(/^PUT/, curl.body_str)802 assert_match(/message$/, curl.body_str)803 end804 # https://github.com/taf2/curb/issues/101805 def test_put_data_null_bytes806 curl = Curl::Easy.new(TestServlet.url)807 curl.put_data = "a\0b"808 809 curl.perform810 811 assert_match(/^PUT/, curl.body_str)812 assert_match("a\0b", curl.body_str)813 end814 def test_put_nil_data_no_crash815 curl = Curl::Easy.new(TestServlet.url)816 curl.put_data = nil817 818 curl.perform819 end820 def test_put_remote_file821 curl = Curl::Easy.new(TestServlet.url)822 File.open(__FILE__,'rb') do|f|823 assert curl.http_put(f)824 end825 assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')826 end827 828 def test_put_class_method829 count = 0830 curl = Curl::Easy.http_put(TestServlet.url,File.open(__FILE__,'rb')) do|c|831 count += 1832 assert_equal Curl::Easy, c.class833 end834 assert_equal 1, count835 assert_equal "PUT\n#{File.read(__FILE__)}", curl.body_str.tr("\r", '')836 end837 # Generate a self-signed cert with838 # openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 \839 # -keyout tests/cert.pem -out tests/cert.pem840 def test_cert841 curl = Curl::Easy.new(TestServlet.url)842 curl.cert= File.join(File.dirname(__FILE__),"cert.pem")843 assert_match(/cert.pem$/,curl.cert)844 end845 def test_cert_with_password846 curl = Curl::Easy.new(TestServlet.url)847 path = File.join(File.dirname(__FILE__),"cert.pem")848 curl.certpassword = 'password'849 curl.cert = path850 assert_match(/cert.pem$/,curl.cert)851 end852 def test_cert_type853 curl = Curl::Easy.new(TestServlet.url)854 curl.certtype= "DER"855 assert_equal "DER", curl.certtype856 end857 def test_default_certtype858 curl = Curl::Easy.new(TestServlet.url)859 assert_nil curl.certtype860 curl.certtype = "PEM"861 assert_equal "PEM", curl.certtype862 end863 # Generate a CA cert with instructions at864 # http://technocage.com/~caskey/openssl/865 def test_ca_cert866 curl = Curl::Easy.new(TestServlet.url)867 curl.cacert= File.join(File.dirname(__FILE__),"cacert.pem")868 assert_match(/cacert.pem$/, curl.cacert)869 end870 def test_user_agent871 curl = Curl::Easy.new(TestServlet.url)872 curl.useragent= "Curb-Easy/Ruby"873 assert_equal "Curb-Easy/Ruby",curl.useragent874 end875 def test_username_password876 curl = Curl::Easy.new(TestServlet.url)877 curl.username = "foo"878 curl.password = "bar"879 if !curl.username.nil?880 assert_equal "foo", curl.username881 assert_equal "bar", curl.password882 else883 curl.userpwd = "foo:bar"884 end885 curl.http_auth_types = :basic886 #curl.verbose = true887 curl.perform888 assert_equal 'Basic Zm9vOmJhcg==', $auth_header889 $auth_header = nil890 # curl checks the auth type supported by the server, so we have to create a 891 # new easy handle if we're going to change the auth type...892 curl = Curl::Easy.new(TestServlet.url)893 curl.username = "foo"894 curl.password = "bar"895 if curl.username.nil?896 curl.userpwd = "foo:bar"897 end898 curl.http_auth_types = :ntlm899 curl.perform900 assert_equal 'NTLM TlRMTVNTUAABAAAABoIIAAAAAAAAAAAAAAAAAAAAAAA=', $auth_header901 end902 def test_primary_ip903 curl = Curl::Easy.new(TestServlet.url)904 if curl.respond_to?(:primary_ip)905 curl.perform906 assert_equal '127.0.0.1', curl.primary_ip907 end908 end909 def test_post_streaming910 readme = File.expand_path(File.join(File.dirname(__FILE__),'..','README.markdown'))911 912 pf = Curl::PostField.file("filename", readme)913 easy = Curl::Easy.new914 easy.url = TestServlet.url915 easy.multipart_form_post = true916 easy.http_post(pf)917 assert_not_equal(0,easy.body_str.size)918 assert_equal(easy.body_str.tr("\r", ''), File.read(readme))919 end920 def test_easy_close921 easy = Curl::Easy.new922 easy.close923 easy.url = TestServlet.url924 easy.http_get925 end926 def test_easy_reset927 easy = Curl::Easy.new928 easy.url = TestServlet.url + "?query=foo"929 easy.http_get930 settings = easy.reset931 assert settings.key?(:url)932 assert settings.key?(:body_data)933 assert settings.key?(:header_data)934 easy.url = TestServlet.url935 easy.http_get936 end937 def test_easy_use_http_versions938 easy = Curl::Easy.new939 easy.url = TestServlet.url + "?query=foo"940 #puts "http none: #{Curl::HTTP_NONE.inspect}"941 #puts "http1.0: #{Curl::HTTP_1_0.inspect}"942 #puts "http1.1: #{Curl::HTTP_1_1.inspect}"943 easy.version = Curl::HTTP_1_1944 #easy.verbose = true945 easy.http_get946 end947 def test_easy_http_verbs948 curl = Curl::Easy.new(TestServlet.url)949 curl.http_delete950 assert_equal 'DELETE', curl.body_str951 curl.http_get952 assert_equal 'GET', curl.body_str953 curl.http_post954 assert_equal "POST\n", curl.body_str955 curl.http('PURGE')956 assert_equal 'PURGE', curl.body_str957 curl.http_put('hello')958 assert_equal "PUT\nhello", curl.body_str959 curl.http('COPY')960 assert_equal 'COPY', curl.body_str961 end962 def test_easy_http_verbs_must_respond_to_str963 # issue http://github.com/taf2/curb/issues#issue/45964 assert_nothing_raised do965 c = Curl::Easy.new ; c.url = 'http://example.com' ; c.http(:get)966 end967 assert_raise RuntimeError do968 c = Curl::Easy.new ; c.url = 'http://example.com' ; c.http(FooNoToS.new)969 end970 end971 # http://github.com/taf2/curb/issues/#issue/33972 def test_easy_http_verbs_with_errors973 curl = Curl::Easy.new("http://127.0.0.1:9012/") # test will fail if http server on port 9012974 assert_raise Curl::Err::ConnectionFailedError do975 curl.http_delete976 end977 curl.url = TestServlet.url978 curl.http_get979 assert_equal 'GET', curl.body_str980 end981 def test_easy_can_put_with_content_length982 curl = Curl::Easy.new(TestServlet.url)983 rd, wr = IO.pipe984 buf = (("hello")* (1000 / 5))985 producer = Thread.new do986 5.times do987 wr << buf988 sleep 0.1 # act as a slow producer989 end990 end991 consumer = Thread.new do992 #curl.verbose = true993 curl.headers['Content-Length'] = buf.size * 5994 curl.headers['User-Agent'] = "Something else"995 curl.headers['Content-Type'] = "text/javascript"996 curl.headers['Date'] = Time.now.httpdate997 curl.headers['Host'] = 's3.amazonaws.com'998 curl.headers['Accept'] = '*/*'999 curl.headers['Authorization'] = 'Foo Bar Biz Baz'1000 curl.http_put(rd)1001 assert_match(/^PUT/, curl.body_str)1002 assert_match(/hello$/, curl.body_str)1003 curl.header_str1004 curl.body_str1005 end1006 producer.join1007 wr.close1008 consumer.join1009 end1010 def test_get_set_multi_on_easy1011 easy = Curl::Easy.new1012 assert_nil easy.multi1013 multi = Curl::Multi.new1014 easy.multi = multi...

Full Screen

Full Screen

curb_spec.rb

Source:curb_spec.rb Github

copy

Full Screen

...7 include_examples "with WebMock"8 describe "when doing PUTs" do9 it "should stub them" do10 stub_request(:put, "www.example.com").with(body: "01234")11 expect(http_request(:put, "http://www.example.com", body: "01234").12 status).to eq("200")13 end14 end15 end16 describe "Curb features" do17 before(:each) do18 WebMock.disable_net_connect!19 WebMock.reset!20 end21 describe "callbacks" do22 before(:each) do23 @curl = Curl::Easy.new24 @curl.url = "http://example.com"25 end26 describe 'on_debug' do27 it "should call on_debug" do28 stub_request(:any, "example.com").29 to_return(status: 200, headers: { 'Server' => 'nginx' }, body: { hello: :world }.to_json)30 test = []31 @curl.on_debug do |message, operation|32 test << "#{operation} -> #{message}"33 end34 @curl.headers['Content-Type'] = 'application/json'35 @curl.http_post({ hello: :world }.to_json)36 expect(test).to_not be_empty37 end38 end39 it "should call on_success with 2xx response" do40 body = "on_success fired"41 stub_request(:any, "example.com").to_return(body: body)42 test = nil43 @curl.on_success do |c|44 test = c.body_str45 end46 @curl.http_get47 expect(test).to eq(body)48 end49 it "should call on_missing with 4xx response" do50 response_code = 40351 stub_request(:any, "example.com").52 to_return(status: [response_code, "None shall pass"])53 test = nil54 @curl.on_missing do |c, code|55 test = code56 end57 @curl.http_get58 expect(test).to eq(response_code)59 end60 it "should call on_failure with 5xx response" do61 response_code = 59962 stub_request(:any, "example.com").63 to_return(status: [response_code, "Server On Fire"])64 test = nil65 @curl.on_failure do |c, code|66 test = code67 end68 @curl.http_get69 expect(test).to eq(response_code)70 end71 it "should call on_body when response body is read" do72 body = "on_body fired"73 stub_request(:any, "example.com").74 to_return(body: body)75 test = nil76 @curl.on_body do |data|77 test = data78 end79 @curl.http_get80 expect(test).to eq(body)81 end82 it "should call on_body for each chunk with chunked response" do83 stub_request(:any, "example.com").84 to_return(body: ["first_chunk", "second_chunk"],85 headers: {"Transfer-Encoding" => "chunked"})86 test = []87 @curl.on_body do |data|88 test << data89 end90 @curl.http_get91 expect(test).to eq(["first_chunk", "second_chunk"])92 end93 it "should call on_header when response headers are read" do94 stub_request(:any, "example.com").95 to_return(headers: {one: 1})96 test = []97 @curl.on_header do |data|98 test << data99 end100 @curl.http_get101 expect(test).to eq([102 "HTTP/1.1 200 \r\n",103 'One: 1'104 ])105 end106 it "should call on_complete when request is complete" do107 body = "on_complete fired"108 stub_request(:any, "example.com").to_return(body: body)109 test = nil110 @curl.on_complete do |curl|111 test = curl.body_str112 end113 @curl.http_get114 expect(test).to eq(body)115 end116 it "should call on_progress when portion of response body is read" do117 stub_request(:any, "example.com").to_return(body: "01234")118 test = nil119 @curl.on_progress do |*args|120 expect(args.length).to eq(4)121 args.each {|arg| expect(arg.is_a?(Float)).to eq(true) }122 test = true123 end124 @curl.http_get125 expect(test).to eq(true)126 end127 it "should call callbacks in correct order on successful request" do128 stub_request(:any, "example.com")129 order = []130 @curl.on_success {|*args| order << :on_success }131 @curl.on_missing {|*args| order << :on_missing }132 @curl.on_failure {|*args| order << :on_failure }133 @curl.on_header {|*args| order << :on_header }134 @curl.on_body {|*args| order << :on_body }135 @curl.on_complete {|*args| order << :on_complete }136 @curl.on_progress {|*args| order << :on_progress }137 @curl.http_get138 expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_success])139 end140 it "should call callbacks in correct order on failed request" do141 stub_request(:any, "example.com").to_return(status: [500, ""])142 order = []143 @curl.on_success {|*args| order << :on_success }144 @curl.on_missing {|*args| order << :on_missing }145 @curl.on_failure {|*args| order << :on_failure }146 @curl.on_header {|*args| order << :on_header }147 @curl.on_body {|*args| order << :on_body }148 @curl.on_complete {|*args| order << :on_complete }149 @curl.on_progress {|*args| order << :on_progress }150 @curl.http_get151 expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_failure])152 end153 it "should call callbacks in correct order on missing request" do154 stub_request(:any, "example.com").to_return(status: [403, ""])155 order = []156 @curl.on_success {|*args| order << :on_success }157 @curl.on_missing {|*args| order << :on_missing }158 @curl.on_failure {|*args| order << :on_failure }159 @curl.on_header {|*args| order << :on_header }160 @curl.on_body {|*args| order << :on_body }161 @curl.on_complete {|*args| order << :on_complete }162 @curl.on_progress {|*args| order << :on_progress }163 @curl.http_get164 expect(order).to eq([:on_progress,:on_header,:on_body,:on_complete,:on_missing])165 end166 end167 describe '#last_effective_url' do168 before(:each) do169 @curl = Curl::Easy.new170 @curl.url = "http://example.com"171 end172 context 'when not following redirects' do173 before { @curl.follow_location = false }174 it 'should be the same as #url even with a location header' do175 stub_request(:any, 'example.com').176 to_return(body: "abc",177 status: 302,178 headers: { 'Location' => 'http://www.example.com' })179 @curl.http_get180 expect(@curl.last_effective_url).to eq('http://example.com')181 end182 end183 context 'when following redirects' do184 before { @curl.follow_location = true }185 it 'should be the same as #url when no location header is present' do186 stub_request(:any, "example.com")187 @curl.http_get188 expect(@curl.last_effective_url).to eq('http://example.com')189 end190 it 'should be the value of the location header when present' do191 stub_request(:any, 'example.com').192 to_return(headers: { 'Location' => 'http://www.example.com' })193 stub_request(:any, 'www.example.com')194 @curl.http_get195 expect(@curl.last_effective_url).to eq('http://www.example.com')196 end197 it 'should work with more than one redirect' do198 stub_request(:any, 'example.com').199 to_return(headers: { 'Location' => 'http://www.example.com' })200 stub_request(:any, 'www.example.com').201 to_return(headers: { 'Location' => 'http://blog.example.com' })202 stub_request(:any, 'blog.example.com')203 @curl.http_get204 expect(@curl.last_effective_url).to eq('http://blog.example.com')205 end206 it 'should maintain the original url' do207 stub_request(:any, 'example.com').208 to_return(headers: { 'Location' => 'http://www.example.com' })209 stub_request(:any, 'www.example.com')210 @curl.http_get211 expect(@curl.url).to eq('http://example.com')212 end213 it 'should have the redirected-to attrs (body, response code)' do214 stub_request(:any, 'example.com').215 to_return(body: 'request A',216 status: 302,217 headers: { 'Location' => 'http://www.example.com' })218 stub_request(:any, 'www.example.com').to_return(body: 'request B')219 @curl.http_get220 expect(@curl.body_str).to eq('request B')221 expect(@curl.response_code).to eq(200)222 end223 it 'should follow more than one redirect' do224 stub_request(:any, 'example.com').225 to_return(headers: { 'Location' => 'http://www.example.com' })226 stub_request(:any, 'www.example.com').227 to_return(headers: { 'Location' => 'http://blog.example.com' })228 stub_request(:any, 'blog.example.com').to_return(body: 'blog post')229 @curl.http_get230 expect(@curl.url).to eq('http://example.com')231 expect(@curl.body_str).to eq('blog post')232 end233 end234 end235 describe "#content_type" do236 before(:each) do237 @curl = Curl::Easy.new238 @curl.url = "http://example.com"239 end240 context "when response includes Content-Type header" do241 it "returns correct content_type" do242 content_type = "application/json"243 stub_request(:any, 'example.com').244 to_return(body: "abc",245 status: 200,246 headers: { 'Content-Type' => content_type })247 @curl.http_get248 expect(@curl.content_type).to eq(content_type)249 end250 end251 context "when response does not include Content-Type header" do252 it "returns nil for content_type" do253 stub_request(:any, 'example.com').254 to_return(body: "abc",255 status: 200 )256 @curl.http_get257 expect(@curl.content_type).to be_nil258 end259 end260 end261 describe "#chunked_response?" do262 before(:each) do263 @curl = Curl::Easy.new264 @curl.url = "http://example.com"265 end266 it "is true when Transfer-Encoding is 'chunked' and body responds to each" do267 stub_request(:any, 'example.com').268 to_return(body: ["abc", "def"],269 status: 200,270 headers: { 'Transfer-Encoding' => 'chunked' })271 @curl.http_get272 expect(@curl).to be_chunked_response273 end274 it "is false when Transfer-Encoding is not 'chunked'" do275 stub_request(:any, 'example.com').276 to_return(body: ["abc", "def"],277 status: 200)278 @curl.http_get279 expect(@curl).not_to be_chunked_response280 end281 it "is false when Transfer-Encoding is 'chunked' but body does not respond to each" do282 stub_request(:any, 'example.com').283 to_return(body: "abc",284 status: 200)285 @curl.http_get286 expect(@curl).not_to be_chunked_response287 end288 end289 end290 describe "Webmock with Curb" do291 describe "using #http for requests" do292 it_should_behave_like "Curb"293 include CurbSpecHelper::DynamicHttp294 it "should work with uppercase arguments" do295 stub_request(:get, "www.example.com").to_return(body: "abc")296 c = Curl::Easy.new297 c.url = "http://www.example.com"298 c.http(:GET)299 expect(c.body_str).to eq("abc")300 end301 it "should alias body to body_str" do302 stub_request(:get, "www.example.com").to_return(body: "abc")303 c = Curl::Easy.new304 c.url = "http://www.example.com"305 c.http(:GET)306 expect(c.body).to eq("abc")307 end308 it "supports array headers passed to Curl::Easy" do309 stub_request(:get, "www.example.com").with(headers: {'X-One' => '1'}).to_return(body: "abc")310 c = Curl::Easy.new311 c.url = "http://www.example.com"312 c.headers = ["X-One: 1"]313 c.http(:GET)314 expect(c.body).to eq("abc")315 end316 describe 'match request body' do317 it 'for post' do318 stub_request(:post, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")319 response = Curl.post("http://www.example.com", {foo: :nhe})320 expect(response.body_str).to eq("abc")321 end322 it 'for patch' do323 stub_request(:patch, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")324 response = Curl.patch("http://www.example.com", {foo: :nhe})325 expect(response.body_str).to eq("abc")326 end327 it 'for put' do328 stub_request(:put, "www.example.com").with(body: 'foo=nhe').to_return(body: "abc")329 response = Curl.put("http://www.example.com", {foo: :nhe})330 expect(response.body_str).to eq("abc")331 end332 end333 end334 describe "using #http_* methods for requests" do335 it_should_behave_like "Curb"336 include CurbSpecHelper::NamedHttp337 it "should work with blank arguments for post" do338 stub_request(:post, "www.example.com").with(body: "01234")339 c = Curl::Easy.new340 c.url = "http://www.example.com"341 c.post_body = "01234"342 c.http_post343 expect(c.response_code).to eq(200)344 end345 it "should work with several body arguments for post using the class method" do346 stub_request(:post, "www.example.com").with(body: {user: {first_name: 'Bartosz', last_name: 'Blimke'}})347 c = Curl::Easy.http_post "http://www.example.com", 'user[first_name]=Bartosz', 'user[last_name]=Blimke'348 expect(c.response_code).to eq(200)349 end350 it "should work with blank arguments for put" do351 stub_request(:put, "www.example.com").with(body: "01234")352 c = Curl::Easy.new353 c.url = "http://www.example.com"354 c.put_data = "01234"355 c.http_put356 expect(c.response_code).to eq(200)357 end358 it "should work with multiple arguments for post" do359 data = { name: "john", address: "111 example ave" }360 stub_request(:post, "www.example.com").with(body: data)361 c = Curl::Easy.new362 c.url = "http://www.example.com"363 c.http_post Curl::PostField.content('name', data[:name]), Curl::PostField.content('address', data[:address])364 expect(c.response_code).to eq(200)365 end366 end367 describe "using #perform for requests" do368 it_should_behave_like "Curb"369 include CurbSpecHelper::Perform370 end371 describe "using .http_* methods for requests" do372 it_should_behave_like "Curb"373 include CurbSpecHelper::ClassNamedHttp374 end375 describe "using .perform for requests" do376 it_should_behave_like "Curb"377 include CurbSpecHelper::ClassPerform378 end379 describe "using .reset" do380 before do381 @curl = Curl::Easy.new382 @curl.url = "http://example.com"383 body = "on_success fired"384 stub_request(:any, "example.com").to_return(body: body)385 @curl.http_get386 end387 it "should clear the body_str" do388 @curl.reset389 expect(@curl.body_str).to eq(nil)390 end391 end392 end393end...

Full Screen

Full Screen

curb_spec_helper.rb

Source:curb_spec_helper.rb Github

copy

Full Screen

1require 'ostruct'2module CurbSpecHelper3 def http_request(method, uri, options = {}, &block)4 uri = Addressable::URI.heuristic_parse(uri)5 body = options[:body]6 curl = curb_http_request(uri, method, body, options)7 status, response_headers =8 WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(curl.header_str)9 # Deal with the fact that the HTTP spec allows multi-values headers10 # to either be a single entry with a comma-separated listed of11 # values, or multiple separate entries12 response_headers.keys.each do |k|13 v = response_headers[k]14 if v.is_a?(Array)15 response_headers[k] = v.join(', ')16 end17 end18 OpenStruct.new(19 body: curl.body_str,20 headers: WebMock::Util::Headers.normalize_headers(response_headers),21 status: curl.response_code.to_s,22 message: status23 )24 end25 def setup_request(uri, curl, options={})26 curl ||= Curl::Easy.new27 curl.url = uri.to_s28 if options[:basic_auth]29 curl.http_auth_types = :basic30 curl.username = options[:basic_auth][0]31 curl.password = options[:basic_auth][1]32 end33 curl.timeout = 3034 curl.connect_timeout = 3035 if headers = options[:headers]36 headers.each {|k,v| curl.headers[k] = v }37 end38 curl39 end40 def client_timeout_exception_class41 Curl::Err::TimeoutError42 end43 def connection_refused_exception_class44 Curl::Err::ConnectionFailedError45 end46 def http_library47 :curb48 end49 module DynamicHttp50 def curb_http_request(uri, method, body, options)51 curl = setup_request(uri, nil, options)52 case method53 when :post54 curl.post_body = body55 when :put56 curl.put_data = body57 end58 curl.http(method.to_s.upcase)59 curl60 end61 end62 module NamedHttp63 def curb_http_request(uri, method, body, options)64 curl = setup_request(uri, nil, options)65 case method66 when :put, :post67 curl.send( "http_#{method}", body )68 else69 curl.send( "http_#{method}" )70 end71 curl72 end73 end74 module Perform75 def curb_http_request(uri, method, body, options)76 curl = setup_request(uri, nil, options)77 case method78 when :post79 curl.post_body = body80 when :put81 curl.put_data = body82 when :head83 curl.head = true84 when :delete85 curl.delete = true86 end87 curl.perform88 curl89 end90 end91 module ClassNamedHttp92 def curb_http_request(uri, method, body, options)93 args = ["http_#{method}", uri]94 args << body if method == :post || method == :put95 c = Curl::Easy.send(*args) do |curl|96 setup_request(uri, curl, options)97 end98 c99 end100 end101 module ClassPerform102 def curb_http_request(uri, method, body, options)103 args = ["http_#{method}", uri]104 args << body if method == :post || method == :put105 c = Curl::Easy.send(*args) do |curl|106 setup_request(uri, curl, options)107 case method108 when :post109 curl.post_body = body110 when :put111 curl.put_data = body112 when :head113 curl.head = true114 when :delete115 curl.delete = true116 end117 end...

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1c = Curl::Easy.new("http://www.google.com/")2c = Curl::Easy.new("http://www.google.com/")3c = Curl::Easy.new("http://www.google.com/")4c = Curl::Easy.new("http://www.google.com/")5c = Curl::Easy.new("http://www.google.com/")6c = Curl::Easy.new("http://www.google.com/")7c = Curl::Easy.new("http://www.google.com/")8c = Curl::Easy.new("http://www.google.com/")9c = Curl::Easy.new("http://www.google.com/")10c = Curl::Easy.new("http://www.google.com/")

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1c = Curl::Easy.new("http://www.example.com")2doc = Nokogiri::HTML(html)3doc.css('a').each do |link|4url = URI.parse('http://www.example.com')5req = Net::HTTP::Get.new(url.path)6res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}7doc = Nokogiri::HTML(html)8doc.css('a').each do |link|9html = open("http://www.example.com")10doc = Nokogiri::HTML(html)11doc.css('a').each do |link|12doc = Nokogiri::HTML(html)13doc.css('a').each do |link|14html = HTTParty.get('http://www.example.com')15doc = Nokogiri::HTML(html)16doc.css('a').each do |link|17html = client.get_content('http://www.example.com')18doc = Nokogiri::HTML(html)19doc.css('a').each do |link|

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1c = Curl::Easy.new("http://www.google.com")2c = Curl::Easy.new("http://www.google.com")3c = Curl::Easy.new("http://www.google.com")4c.http('GET')5c = Curl::Easy.new("http://www.google.com")6c.http(Curl::Get)7c = Curl::Easy.new("http://www.google.com")8c.http(Curl::Get, nil)9c = Curl::Easy.new("http://www.google.com")10c.http(Curl::Get, nil, nil)11c = Curl::Easy.new("http://www.google.com")12c.http(Curl::Get, nil, nil, nil)13c = Curl::Easy.new("http://www.google.com")14c.http(Curl::Get, nil, nil, nil, nil)

Full Screen

Full Screen

http

Using AI Code Generation

copy

Full Screen

1c = Curl::Easy.new(url) do |curl|2 curl.headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"3c = Curl::Easy.new(url) do |curl|4 curl.headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"5c = Curl::Easy.new(url) do |curl|6 curl.headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"7c = Curl::Easy.new(url) do |curl|8 curl.headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"9c = Curl::Easy.new(url) do |curl|10 curl.headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful