def test_seek_wrapper_class_name(self): opener = mechanize.UserAgent() opener.set_seekable_responses(True) try: opener.open(urljoin(self.uri, "nonexistent")) except mechanize.HTTPError, exc: self.assert_("HTTPError instance" in repr(exc))
def get_browser(self): """Browser object response for load cookie and save cookie""" if self.browser: return self.browser else: browser = mechanize.UserAgent() browser.set_handle_equiv(False) browser.set_handle_gzip(False) browser.set_handle_redirect(True) browser.set_handle_robots(False) browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) browser.set_seekable_responses(False) if self.debug: browser.set_debug_http(True) browser.set_debug_redirects(True) browser.set_debug_responses(True) cj = mechanize.LWPCookieJar() try: cj.load(self.cookie_file, ignore_discard=True, ignore_expires=True) except IOError: pass browser.set_cookiejar(cj) browser.addheaders = [ ('User-agent', self.user_agent), ] self.browser = browser return self.browser
def test_non_seekable(self): # check everything still works without response_seek_wrapper and # the .seek() method on response objects ua = mechanize.UserAgent() ua.set_seekable_responses(False) ua.set_handle_equiv(False) response = ua.open(self.uri) self.failIf(hasattr(response, "seek")) data = response.read() self.assert_("Python bits" in data)
def make_user_agent(self): ua = mechanize.UserAgent() self._configure_user_agent(ua) return ua
opener.open(urljoin(self.uri, "nonexistent")) except mechanize.HTTPError, exc: self.assert_("HTTPError instance" in repr(exc)) def test_no_seek(self): # should be possible to turn off UserAgent's .seek() functionality def check_no_seek(opener): r = opener.open(urljoin(self.uri, "bits/cctest2.txt")) self.assert_(not hasattr(r, "seek")) try: opener.open(urljoin(self.uri, "nonexistent")) except mechanize.HTTPError, exc: self.assert_(not hasattr(exc, "seek")) # mechanize.UserAgent opener = mechanize.UserAgent() opener.set_handle_equiv(False) opener.set_seekable_responses(False) opener.set_debug_http(False) check_no_seek(opener) # mechanize.OpenerDirector opener = mechanize.build_opener() check_no_seek(opener) def test_consistent_seek(self): # if we explicitly request that returned response objects have the # .seek() method, then raised HTTPError exceptions should also have the # .seek() method def check(opener, excs_also): r = opener.open(urljoin(self.uri, "bits/cctest2.txt"))