class Net::HTTP

Constants

PAGE_CACHE
SERVLETS

Public Instance Methods

do_start() click to toggle source
# File lib/mechanize/test_case.rb, line 520
def do_start
  @started = true
end
Also aliased as: old_do_start
keep_alive?(req, res) click to toggle source
# File lib/mechanize/monkey_patch.rb, line 6
def keep_alive?(req, res)
  return false if %rclose/ =~ req['connection'].to_s
  return false if @seems_1_0_server
  return false if %rclose/      =~ res['connection'].to_s
  return true  if %rkeep-alive/ =~ res['connection'].to_s
  return false if %rclose/      =~ res['proxy-connection'].to_s
  return true  if %rkeep-alive/ =~ res['proxy-connection'].to_s
  (@curr_http_version == '1.1')
end
Also aliased as: old_keep_alive?
old_do_start() click to toggle source
Alias for: do_start
old_keep_alive?(req, res) click to toggle source
Alias for: keep_alive?
old_request(req, *data, &block) click to toggle source
Alias for: request
request(req, *data) { |response| ... } click to toggle source
# File lib/mechanize/test_case.rb, line 556
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'] =~ %rwww-form-urlencoded/ then
                WEBrick::HTTPUtils.parse_query req.body
              elsif req['content-type'] =~ %rboundary=(.+)/ then
                boundary = WEBrick::HTTPUtils.dequote $1
                WEBrick::HTTPUtils.parse_form_data req.body, boundary
              else
                {}
              end

  req.cookies = 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 %r\.txt$/
      res['Content-Type'] = 'text/plain'
    when %r\.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.message

  res.header.each do |k,v|
    v = v.first if v.length == 1
    response[k] = v
  end

  res.cookies.each do |cookie|
    response.add_field 'Set-Cookie', 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
Also aliased as: old_request