def test_file_and_json_fails(self, req, includes): """Can't send json and files at once""" req["files"] = ["abc"] req["json"] = {"key": "value"} with pytest.raises(exceptions.BadSchemaError): get_request_args(req, includes)
def test_default_method_raises_with_body(self, req, includes, body_key): del req["method"] del req["data"] req[body_key] = {"a": "b"} with pytest.warns(RuntimeWarning): get_request_args(req, includes)
def test_no_override_content_type(self, req, includes): req["headers"]["Content-Type"] = "application/x-www-form-urlencoded" args = get_request_args(req, includes) assert args["headers"][ "Content-Type"] == "application/x-www-form-urlencoded"
def test_cert_with_valid_values(self, req, includes, cert_value): req["cert"] = cert_value args = get_request_args(req, includes) if isinstance(cert_value, list): assert args["cert"] == (cert_value[0], cert_value[1]) else: assert args["cert"] == cert_value
def test_no_set_content_type(self, req, includes): del req["headers"]["Content-Type"] args = get_request_args(req, includes) with pytest.raises(KeyError): assert args["headers"]["content-type"]
def test_default_method(self, req, includes): del req["method"] del req["data"] args = get_request_args(req, includes) assert args["method"] == "GET"
def test_nested_params_encoded(self, req, includes): req["params"] = {"a": {"b": {"c": "d"}}} args = get_request_args(req, includes) assert args["params"][ "a"] == "%7B%22b%22%3A+%7B%22c%22%3A+%22d%22%7D%7D"
def test_passthrough_verify(self, req, includes, verify): """Should be able to pass 'verify' through to requests.request""" req["verify"] = verify args = get_request_args(req, includes) assert args["verify"] == verify
def test_headers_no_content_type_change(self, req, includes, extra_headers): """Sending a file doesn't set the content type as json""" del req["data"] req["files"] = ["abc"] args = get_request_args(req, includes) assert "content-type" not in [i.lower() for i in args["headers"].keys()]
def test_file_body(self, req, includes): """Test getting file body""" req.pop("data") req["file_body"] = "{callback_url}" includes["abcdef"] = "Hello" args = get_request_args(req, includes) assert args["file_body"] == includes["variables"]["callback_url"]
def test_no_default_content_type(self, req, includes, extra): del req["headers"]["Content-Type"] req.pop("json", {}) req.pop("data", {}) req.update(**extra) args = get_request_args(req, includes) # Requests will automatically set content type headers for json/form encoded data so we don't need to with pytest.raises(KeyError): assert args["headers"]["content-type"]
def test_get_from_function(self, req, includes): """Make sure ext functions work in request This is a bit of a silly example because we're passing a dictionary instead of a string like it would be from the test, but it saves us having to define another external function just for this test """ to_copy = {"thing": "value"} req["data"] = {"$ext": {"function": "copy:copy", "extra_args": [to_copy]}} args = get_request_args(req, includes) assert args["data"] == to_copy
def test_file_body_format(self, req, includes): """Test getting file body""" req.pop("data") with tempfile.NamedTemporaryFile(encoding="utf8", mode="w") as tmpin: tmpin.write("OK") includes["variables"]["tmpfile_loc"] = tmpin.name req["file_body"] = "{tmpfile_loc}" args = get_request_args(req, includes) assert args["file_body"] == tmpin.name
def __init__(self, session, rspec, test_block_config): """Prepare request Args: rspec (dict): test spec test_block_config (dict): Any configuration for this the block of tests Raises: UnexpectedKeysError: If some unexpected keys were used in the test spec. Only valid keyword args to requests can be passed """ if 'meta' in rspec: meta = rspec.pop('meta') if meta and 'clear_session_cookies' in meta: session.cookies.clear_session_cookies() expected = { "method", "url", "headers", "data", "params", "auth", "json", "verify", # "files", # "cookies", # "hooks", } check_expected_keys(expected, rspec) request_args = get_request_args(rspec, test_block_config) logger.debug("Request args: %s", request_args) self._request_args = request_args # There is no way using requests to make a prepared request that will # not follow redicrects, so instead we have to do this. This also means # that we can't have the 'pre-request' hook any more because we don't # create a prepared request. self._prepared = functools.partial(session.make_request, **request_args)
def test_file_body_content_type(self, req, includes): """Test inferring content type etc. works""" req.pop("data") req.pop("headers") with tempfile.NamedTemporaryFile(encoding="utf8", mode="w", suffix=".json") as tmpin: tmpin.write("OK") req["file_body"] = tmpin.name args = get_request_args(req, includes) print(args) assert args["file_body"] == tmpin.name assert args["headers"]["content-type"] == "application/json"
def __init__(self, session, rspec, test_block_config): """Prepare request Args: rspec (dict): test spec test_block_config (dict): Any configuration for this the block of tests Raises: UnexpectedKeysError: If some unexpected keys were used in the test spec. Only valid keyword args to requests can be passed """ if 'meta' in rspec: meta = rspec.pop('meta') if meta and 'clear_session_cookies' in meta: # TODO: how to clear cookies? #session.cookies.clear_session_cookies() pass expected = { "method", "url", "headers", "data", "params", "auth", "json", "verify", # "files", # "cookies", # "hooks", } check_expected_keys(expected, rspec) request_args = get_request_args(rspec, test_block_config) logger.debug("Request args: %s", request_args) self._request_args = request_args # TODO: Follow redirects options? self._prepared = functools.partial(session.make_request, **request_args)
def test_default_content_type(self, req, includes): del req["headers"]["Content-Type"] args = get_request_args(req, includes) assert args["headers"]["content-type"] == "application/json"
def test_no_override_method(self, req, includes): req["method"] = "POST" args = get_request_args(req, includes) assert args["method"] == "POST"
def test_array_substitution(self, req, includes): args = get_request_args(req, includes) assert args['data']['array'] == ['def456', 'def456']
def test_file_and_data_fails(self, req, includes): """Can't send json/form data and files at once""" req["files"] = ["abc"] with pytest.raises(exceptions.BadSchemaError): get_request_args(req, includes)
def test_verity_with_valid_values(self, req, includes, verify_values): req["verify"] = verify_values args = get_request_args(req, includes) assert args["verify"] == verify_values
def test_cannot_send_data_and_json(self, req, includes): req["json"] = [1, 2, 3] req["data"] = [1, 2, 3] with pytest.raises(exceptions.BadSchemaError): get_request_args(req, includes)
def test_file_and_data_succeeds(self, req, includes): """Can send form data and files at once""" req["files"] = ["abc"] get_request_args(req, includes)
def test_array_substitution(self, req, includes): args = get_request_args(req, includes) assert args["data"]["array"] == ["def456", "def456"]