Class: Net::HTTP
- Inherits:
-
Object
- Object
- Net::HTTP
- Defined in:
- lib/mechanize/monkey_patch.rb,
lib/mechanize/test_case.rb
Overview
:stopdoc:
Constant Summary collapse
- SERVLETS =
{ '/gzip' => GzipServlet, '/form_post' => FormServlet, '/basic_auth' => BasicAuthServlet, '/form post' => FormServlet, '/response_code' => ResponseCodeServlet, '/http_refresh' => HttpRefreshServlet, '/content_type_test' => ContentTypeServlet, '/referer' => RefererServlet, '/file_upload' => FileUploadServlet, '/one_cookie' => OneCookieServlet, '/one_cookie_no_space' => OneCookieNoSpacesServlet, '/many_cookies' => ManyCookiesServlet, '/many_cookies_as_string' => ManyCookiesAsStringServlet, '/ntlm' => NTLMServlet, '/send_cookies' => SendCookiesServlet, '/quoted_value_cookie' => QuotedValueCookieServlet, '/if_modified_since' => ModifiedSinceServlet, '/http_headers' => HeaderServlet, '/infinite_redirect' => InfiniteRedirectServlet, '/infinite_refresh' => InfiniteRefreshServlet, '/redirect' => RedirectServlet, '/refresh_without_url' => RefreshWithoutUrl, '/refresh_with_empty_url' => RefreshWithEmptyUrl, '/digest_auth' => DigestAuthServlet, '/verb' => VerbServlet, }
- PAGE_CACHE =
{}
Instance Method Summary collapse
- #do_start ⇒ Object
- #keep_alive?(req, res) ⇒ Boolean
- #old_do_start ⇒ Object
- #old_keep_alive? ⇒ Boolean
- #old_request ⇒ Object
- #request(req, *data) {|response| ... } ⇒ Object
Instance Method Details
#do_start ⇒ Object
512 513 514 |
# File 'lib/mechanize/test_case.rb', line 512 def do_start @started = true end |
#keep_alive?(req, res) ⇒ Boolean
6 7 8 9 10 11 12 13 14 |
# File 'lib/mechanize/monkey_patch.rb', line 6 def keep_alive?(req, res) return false if /close/i =~ req['connection'].to_s return false if @seems_1_0_server return false if /close/i =~ res['connection'].to_s return true if /keep-alive/i =~ res['connection'].to_s return false if /close/i =~ res['proxy-connection'].to_s return true if /keep-alive/i =~ res['proxy-connection'].to_s (@curr_http_version == '1.1') end |
#old_do_start ⇒ Object
510 |
# File 'lib/mechanize/test_case.rb', line 510 alias :old_do_start :do_start |
#old_keep_alive? ⇒ Boolean
4 5 6 7 8 9 10 11 12 |
# File 'lib/mechanize/monkey_patch.rb', line 4 def keep_alive?(req, res) return false if /close/i =~ req['connection'].to_s return false if @seems_1_0_server return false if /close/i =~ res['connection'].to_s return true if /keep-alive/i =~ res['connection'].to_s return false if /close/i =~ res['proxy-connection'].to_s return true if /keep-alive/i =~ res['proxy-connection'].to_s (@curr_http_version == '1.1') end |
#old_request ⇒ Object
546 |
# File 'lib/mechanize/test_case.rb', line 546 alias :old_request :request |
#request(req, *data) {|response| ... } ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/mechanize/test_case.rb', line 548 def request(req, *data, &block) url = URI.parse(req.path) path = WEBrick::HTTPUtils.unescape(url.path) path = '/index.html' if path == '/' res = ::Response.new res.query_params = url.query req.query = if 'POST' != req.method && url.query then WEBrick::HTTPUtils.parse_query url.query elsif req['content-type'] =~ /www-form-urlencoded/ then WEBrick::HTTPUtils.parse_query req.body elsif req['content-type'] =~ /boundary=(.+)/ then boundary = WEBrick::HTTPUtils.dequote $1 WEBrick::HTTPUtils.parse_form_data req.body, boundary else {} end req. = WEBrick::Cookie.parse(req['Cookie']) Mechanize::TestCase::REQUESTS << req if servlet_klass = SERVLETS[path] servlet = servlet_klass.new({}) servlet.send "do_#{req.method}", req, res else filename = "htdocs#{path.gsub(/[^\/\\.\w\s]/, '_')}" unless PAGE_CACHE[filename] open("#{Mechanize::TestCase::TEST_DIR}/#{filename}", 'rb') { |io| PAGE_CACHE[filename] = io.read } end res.body = PAGE_CACHE[filename] case filename when /\.txt$/ res['Content-Type'] = 'text/plain' when /\.jpg$/ res['Content-Type'] = 'image/jpeg' end end res['Content-Type'] ||= 'text/html' res.code ||= "200" response_klass = Net::HTTPResponse::CODE_TO_OBJ[res.code.to_s] response = response_klass.new res.http_version, res.code, res. res.header.each do |k,v| v = v.first if v.length == 1 response[k] = v end res..each do || response.add_field 'Set-Cookie', .to_s end response['Content-Type'] ||= 'text/html' response['Content-Length'] = res['Content-Length'] || res.body.length.to_s io = StringIO.new(res.body) response.instance_variable_set :@socket, io def io.read clen, dest, _ dest << string[0, clen] end body_exist = req.response_body_permitted? && response_klass.body_permitted? response.instance_variable_set :@body_exist, body_exist yield response if block_given? response end |