def open(self, path): return urllib2.urlopen(http_console_server.url(path))
http_console_server.register("/bar", lambda x: post_only_string, require_post=True) # Re-registering the same path is not allowed. self.assertRaises(ValueError, http_console_server.register, "/foo", lambda x: test_string) # We should now be able to hit "/foo" and get back out test string. f = self.open("/foo") self.assertEqual(test_string, f.read()) self.assertEqual("text/html", f.info().type) # Trying to get /bar with a GET will fail with a 405. try: self.open("/bar") self.assertTrue(False) except urllib2.URLError, ex: self.assertEqual(405, ex.code) # We should be able to fetch /bar if we force a POST. request = urllib2.Request(http_console_server.url("/bar"), "") self.assertEqual("POST", request.get_method()) self.assertEqual(post_only_string, urllib2.urlopen(request).read()) if __name__ == "__main__": unittest.main()