def __init__(self, client, rspec, test_block_config): expected = {"topic", "payload", "json", "qos", "retain"} check_expected_keys(expected, rspec) publish_args = get_publish_args(rspec, test_block_config) update_from_ext(publish_args, ["json"], test_block_config) self._prepared = functools.partial(client.publish, **publish_args) # Need to do this here because get_publish_args will modify the original # input, which we might want to use to format. No error handling because # all the error handling is done in the previous call self._original_publish_args = format_keys( rspec, test_block_config["variables"])
def test_get_from_function(self, req, merge_values): """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"} original_json = {"test": "test"} req["json"] = { "$ext": { "function": "copy:copy", "extra_args": [to_copy] }, **original_json, } update_from_ext(req, ["json"], {"merge_ext_values": merge_values}) if merge_values: assert req["json"] == dict(**to_copy, **original_json) else: assert req["json"] == to_copy
def __init__(self, session, rspec, test_block_config): """Prepare request Args: session (requests.Session): existing session 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", "file_body", "stream", "timeout", "cookies", "cert", # "hooks", "follow_redirects", } check_expected_keys(expected, rspec) request_args = get_request_args(rspec, test_block_config) update_from_ext( request_args, RestRequest.optional_in_file, test_block_config, ) # Used further down, but pop it asap to avoid unwanted side effects file_body = request_args.pop("file_body", None) # If there was a 'cookies' key, set it in the request expected_cookies = _read_expected_cookies(session, rspec, test_block_config) if expected_cookies is not None: logger.debug("Sending cookies %s in request", expected_cookies.keys()) request_args.update(cookies=expected_cookies) # Check for redirects request_args.update( allow_redirects=_check_allow_redirects(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 redirects, 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. def prepared_request(): # If there are open files, create a context manager around each so # they will be closed at the end of the request. with ExitStack() as stack: stack.enter_context(_set_cookies_for_request(session, request_args)) # These are mutually exclusive if file_body: file = stack.enter_context(open(file_body, "rb")) request_args.update(data=file) else: self._request_args.update( _get_file_arguments(request_args, stack, test_block_config) ) return session.request(**self._request_args) self._prepared = prepared_request