Mocking Net::HTTP For Testing

Quite often at work I find myself having to write code (and therefore tests) that use the Net::HTTP class to read XML from some external REST based service. In this brief article I will explain two minor updates I perform on the Net:HTTP and File classes when in my testing environment.

Most of the services I interact with provide QA and Staging environments along with their Production system. Often times though I am performing other actions with other external services based on the data I am reading from the XML document. Because of this I prefer to mock up the XML response with data that will allow me to test all cases.

The following code uses a few Rails only extensions like alias_method_chain. It is quite simple to factor these out, but I will leave that as a challenge to the reader.

module Net
   class HTTP
     def get_with_file(path, *args)
       case File.basename(path)
       when /activations/
         mock_path = File.join(File.dirname(__FILE__),  "activations.xml")
         return File.open(mock_path)
       else
         get_without_file(path, *args)
       end
     end
     alias_method_chain :get, :file
   end
end

The above class opens Net::HTTP and adds the get_with_file method. Its processing is fairly simple, it looks at the basename of the path provided and uses that to match inside a case statement. Using this case statement I can now open a file, or proceed to another method (get_without_file in this case). Following this method definition is a call to alias_method_chain that renames Net::HTTP.get to Net::HTTP::get_without_file, and links Net::HTTP.get to Net::HTTP::get_with_file.

When using a file to represent an HTTP response object you would normally use the following code and be done.

class File
  alias_method :body, :read
end

This will provide the desired results and make the File appear to be a simple HTTP response. The problem with the simple rewrite to File is that if you call response.body more than once, you will receive nil after the first try. This is because you have reached the end of the file the first time you read through it. To combat this problem I use the following code instead of the simple code above.

class File
   def read_with_memory
     @file_contents ||= read_without_memory
     return @file_contents
   end
   alias_method_chain :read, :memory
   alias_method :body, :read_with_memory

   def header
     {
       'status' => "201 Created",
       'location' => "http://somethingunimportant.com/orders/1"
     }
   end

   def code
     "201"
   end
end

The header and code methods aren’t at all required, but were needed due to the calling code of mine using them to verify the Net::HTTP response object before starting to process the data. The read_with_memory method, like the other two is the important change. This method reads the file into an instance variable and saves it. Each subsequent call returns the instance variable, and not the end of the File.

Adding a sitemap.xml file to Mephisto

Should be an easy task, right? We’ll it wasn’t bad, but it could be easier. After a bunch of googling and failed attempts I settled on this mephisto sitemap plugin. According to the blog it should be easy, just script/plugin install and away you go…

Unfortunately, this doesn’t work out of the box, a few very minor changes are required. I found both of these in the comments on the blog release, but thought it might be helpful for someone else to have a concise walk through here.

Before beginning though, you might want to check out piston if you’re a subversion user.

The first step is to add the follow line to lib/mephisto/routing.rb

      map.connect '/sitemap.xml', :controller => 'sitemap', :action => 'index'

For some unexplained reason the add_route call in vendor/plugins/mephisto_sitemap/lib/sitemap.rb doesn’t seem to be executing properly. If you’re worried you can comment it out, but it doesn’t seem to be causing an issue.

Next, you need to make a change to the second line in vendor/plugins/mephisto_sitemap/lib/sitemap_controller.rb

  self.view_paths = File.join(File.dirname(__FILE__),'../', 'views')

This is required due to a change in edge rails where template_root has been switched to views_path.

Now you should be ready to go. Restart your server and hit up **/sitemap.xml** to see your new plugin in action. Also, don’t forget to visit Google’s Sitemaps for Webmasters and add your sitemap.