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.open(head_request)
>>> browser.headers['content-type']
'image/jpeg'
Getting my image to support HEAD requests took a bit of work but that’s a different post…
