Class: Net::HTTP

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

Overview

:nodoc:

Constant Summary collapse

PAGE_CACHE =
{}

Instance Method Summary collapse

Instance Method Details

#do_startObject



195
196
197
# File 'lib/mechanize/test_case.rb', line 195

def do_start
  @started = true
end

#old_do_startObject



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

alias :old_do_start :do_start

#old_requestObject



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

alias :old_request :request

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

Yields:

  • (response)


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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/mechanize/test_case.rb', line 203

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]
      ::File.open("#{Mechanize::TestCase::TEST_DIR}/#{filename}", 'rb') do |io|
        PAGE_CACHE[filename] = io.read
      end
    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