def openNoVisit(self, url_or_path, data=None, site_relative=True): """ Copy/paste from zope.testbrowser.Browser.open() to allow opening an URL without changing the current page. See L{open}. @see zope.testbrowser.interfaces.IBrowser """ if site_relative: # In case url_or_path is an absolute URL, urljoin() will return # it, otherwise it is a relative path and will be concatenated to # ERP5 base URL url_or_path = urlparse.urljoin(self._erp5_base_url, url_or_path) if isinstance(data, dict): data = urllib.urlencode(data) url = self._absoluteUrl(url_or_path) self._logger.debug("Opening: " + url) if data is not None: def make_request(args): return self.testapp.post(url, data, **args) else: def make_request(args): return self.testapp.get(url, **args) return self._processRequest(url, make_request, no_visit=True)
def _absoluteUrl(self, url): absolute = url.startswith('http://') or url.startswith('https://') if absolute: return str(url) if self._response is None: raise BrowserStateError("can't fetch relative reference: not viewing any document") return str(urlparse.urljoin(self._getBaseUrl(), url))
def _absoluteUrl(self, url): absolute = url.startswith('http://') or url.startswith('https://') if absolute: return str(url) if self._response is None: raise BrowserStateError( "can't fetch relative reference: not viewing any document") return str(urlparse.urljoin(self._getBaseUrl(), url))
def _processRequest(self, url, make_request): with self._preparedRequest(url) as reqargs: self._history.add(self._response) resp = make_request(reqargs) remaining_redirects = 100 # infinite loops protection while resp.status_int in REDIRECTS and remaining_redirects: remaining_redirects -= 1 url = urlparse.urljoin(url, resp.headers['location']) with self._preparedRequest(url) as reqargs: resp = self.testapp.get(url, **reqargs) assert remaining_redirects > 0, "redirects chain looks infinite" self._setResponse(resp) self._checkStatus()
def open(self, url_or_path=None, data=None, site_relative=True): """ Open a relative (to the ERP5 base URL) or absolute URL. If the given URL is not given, then it will open the home ERP5 page. If C{site_relative} is False, it will open the URL within the current context. @param url_or_path: Relative or absolute URL @type url_or_path: str """ if site_relative: # In case url_or_path is an absolute URL, urljoin() will return # it, otherwise it is a relative path and will be concatenated to # ERP5 base URL url_or_path = urlparse.urljoin(self._erp5_base_url, url_or_path) if isinstance(data, dict): data = urllib.urlencode(data) self._logger.debug("Opening: " + url_or_path) super(Browser, self).open(url_or_path, data)
def _processRequest(self, url, make_request, no_visit=False): """ Monkey patched for openNoVisit() """ from zope.testbrowser.browser import REDIRECTS from zope.testbrowser._compat import urlparse with self._preparedRequest(url, no_visit=no_visit) as reqargs: if not no_visit: self._history.add(self._response) resp = make_request(reqargs) remaining_redirects = 100 # infinite loops protection while resp.status_int in REDIRECTS and remaining_redirects: remaining_redirects -= 1 # BEGIN: Bugfix location = resp.headers['location'] if '?' in location: location_without_query_string, query_string = location.split( '?') location = (location_without_query_string + '?' + urllib.urlencode(urlparse.parse_qs( query_string, strict_parsing=True), doseq=True)) # END: Bugfix url = urlparse.urljoin(url, location) with self._preparedRequest(url, no_visit=no_visit) as reqargs: resp = self.testapp.get(url, **reqargs) assert remaining_redirects > 0, "redirects chain looks infinite" if not no_visit: self._setResponse(resp) self._checkStatus() return resp