Search code examples
rubymechanizefakeweb

ruby Fakeweb error if a Mechanize agent's read_timeout= is called


I'm using Mechanize to spider some websites. While spidering I save pages to files that I use later with Fakeweb to do tests.

My Mechanize agent is created this way:

Mechanize.new do |a| 
  a.read_timeout = 20 # doesn't work with Fakeweb?
  a.max_history = 1 
end

When I run my app enabling Fakeweb to fetch files instead of actual Internet access, my log throws these messages for every uri I try

W, [2011-08-20T18:49:45.764749 #14526]  WARN -- : undefined method `read_timeout=' for #<FakeWeb::StubSocket:0xb72c150c>

If I comment the second line in the above code (# a.read_timeout = 20 ...), it works perfectly. No problem at all. Any idea on how to enable read_timout and make Fakeweb work?

TIA


Solution

  • Monkey patching is often a kludge but I think it is reasonable here:

    module FakeWeb
      class StubSocket
        def read_timeout=(ignored)
        end
      end
    end
    

    Timeouts don't have much meaning in the fake world so ignoring them seems like a reasonable thing to do.

    You might even consider sending a pull request to the author.