January 24th, 2012

Update: my original post only worked in Zope 2.10 and below. There’s a Zope bug about this. I’ve updated the post with a work around to fix this.

Today I was trying to fix a bug whereby the image on one of my custom portlets was returning a 500 HTTP response to a HEAD request.  It took my quite some time to figure out how to write a test for this so I thought I’d write a quick post to share what I learnt.

Once you realise the zope.testbrowser.browser.Browser.open accepts a urllib2.Request object as a parameter as well as a string then it all falls into place.  I could only get this working from a doc test:

     >>> from Products.Five.testbrowser import Browser
     >>> browser = Browser()
     >>> from urllib2 import Request
     >>> class HeadRequest(Request):
     ...     def get_method(self):
     ...         return "HEAD"
     >>> head_request = HeadRequest(url_of_object_to_test)
     >>> browser.mech_browser.open(head_request) # in 2.10 you can call open directly
     >>> browser.headers['content-type']

Getting my image to support HEAD requests took a bit of work but that’s a different post…

Comments are closed.