def get(self, url, params=None, headers=None, extra_environ=None, status=None, expect_errors=False, xhr=False): """ Do a GET request given the url path. :param params: A query string, or a dictionary that will be encoded into a query string. You may also include a URL query string on the ``url``. :param headers: Extra headers to send. :type headers: dictionary :param extra_environ: Environmental variables that should be added to the request. :type extra_environ: dictionary :param status: The HTTP status code you expect in response (if not 200 or 3xx). You can also use a wildcard, like ``'3*'`` or ``'*'``. :type status: integer or string :param expect_errors: If this is False, then if anything is written to environ ``wsgi.errors`` it will be an error. If it is True, then non-200/3xx responses are also okay. :type expect_errors: boolean :param xhr: If this is true, then marks response as ajax. The same as headers={'X-REQUESTED-WITH': 'XMLHttpRequest', } :type xhr: boolean :returns: :class:`webtest.TestResponse` instance. """ environ = self._make_environ(extra_environ) url = str(url) url = self._remove_fragment(url) if params: if not isinstance(params, string_types): params = urlencode(params, doseq=True) if str('?') in url: url += str('&') else: url += str('?') url += params if str('?') in url: url, environ['QUERY_STRING'] = url.split(str('?'), 1) else: environ['QUERY_STRING'] = str('') req = self.RequestClass.blank(url, environ) if xhr: headers = self._add_xhr_header(headers) if headers: req.headers.update(headers) return self.do_request(req, status=status, expect_errors=expect_errors)
def build_params(url, params): if not isinstance(params, str): params = urlencode(params, doseq=True) if '?' in url: url += '&' else: url += '?' url += params return url
def build_params(url, params): if not isinstance(params, six.string_types): params = urlencode(params, doseq=True) if str('?') in url: url += str('&') else: url += str('?') url += params return url
def get(self, url, params=None, headers=None, extra_environ=None, status=None, expect_errors=False): """ Get the given url (well, actually a path like ``'/page.html'``). ``params``: A query string, or a dictionary that will be encoded into a query string. You may also include a query string on the ``url``. ``headers``: A dictionary of extra headers to send. ``extra_environ``: A dictionary of environmental variables that should be added to the request. ``status``: The integer status code you expect (if not 200 or 3xx). If you expect a 404 response, for instance, you must give ``status=404`` or it will be an error. You can also give a wildcard, like ``'3*'`` or ``'*'``. ``expect_errors``: If this is not true, then if anything is written to ``wsgi.errors`` it will be an error. If it is true, then non-200/3xx responses are also okay. Returns a :class:`webtest.TestResponse` object. """ environ = self._make_environ(extra_environ) # Hide from py.test: __tracebackhide__ = True # NOQA url = str(url) url = self._remove_fragment(url) if params: if not isinstance(params, string_types): params = urlencode(params, doseq=True) if str('?') in url: url += str('&') else: url += str('?') url += params if str('?') in url: url, environ['QUERY_STRING'] = url.split(str('?'), 1) else: environ['QUERY_STRING'] = str('') req = self.RequestClass.blank(url, environ) if headers: req.headers.update(headers) return self.do_request(req, status=status, expect_errors=expect_errors)
def encode_params(params, content_type): if params is NoDefault: return '' if isinstance(params, dict) or hasattr(params, 'items'): params = list(params.items()) if isinstance(params, (list, tuple)): if content_type: content_type = content_type.lower() if 'charset=' in content_type: charset = content_type.split('charset=')[1] charset = charset.strip('; ').lower() encoded_params = [] for k, v in params: if isinstance(v, str): v = v.encode(charset) encoded_params.append((k, v)) params = encoded_params params = urlencode(params, doseq=True) return params
def encode_params(params, content_type): if params is NoDefault: return '' if isinstance(params, dict) or hasattr(params, 'items'): params = list(params.items()) if isinstance(params, (list, tuple)): if content_type: content_type = content_type.lower() if 'charset=' in content_type: charset = content_type.split('charset=')[1] charset = charset.strip('; ').lower() encoded_params = [] for k, v in params: if isinstance(v, six.text_type): v = v.encode(charset) encoded_params.append((k, v)) params = encoded_params params = urlencode(params, doseq=True) return params