Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/mechanize/test_case.rb,
lib/mechanize/monkey_patch.rb

Overview

:nodoc:

Constant Summary collapse

PAGE_CACHE =
{}

Instance Method Summary collapse

Instance Method Details

#do_startObject



179
180
181
# File 'lib/mechanize/test_case.rb', line 179

def do_start
  @started = true
end

#keep_alive?(req, res) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
# File 'lib/mechanize/monkey_patch.rb', line 7

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_startObject



177
# File 'lib/mechanize/test_case.rb', line 177

alias :old_do_start :do_start

#old_keep_alive?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
# File 'lib/mechanize/monkey_patch.rb', line 5

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_requestObject



185
# File 'lib/mechanize/test_case.rb', line 185

alias :old_request :request

#request(req, *data) {|response| ... } ⇒ Object

Yields:

  • (response)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/mechanize/test_case.rb', line 187

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.cookies = WEBrick::Cookie.parse(req['Cookie'])

  Mechanize::TestCase::REQUESTS << req

  if servlet_klass = MECHANIZE_TEST_CASE_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.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 = nil, _ = nil
    if dest then
      dest << super(clen)
    else
      super clen
    end
  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