Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot change request content by using pytest_tavern_beta_before_every_request #942

Open
william-best opened this issue Jul 19, 2024 · 0 comments

Comments

@william-best
Copy link

william-best commented Jul 19, 2024

tavern 2.11.0

def pytest_tavern_beta_before_every_request(request_args):
    # Add or modify request headers
    logging.info("------before req: {}".format(request_args))
    request_args["headers"] = {'bar': 'bbbbb'}
    request_args['params']['a'] = 8
    logging.info("------req: {}".format(request_args))
    return request_args
def pytest_tavern_beta_after_every_response(expected, response):
    logging.info("------response: {}".format(response.json()))

flask:

@app.route('/get-info', methods=['GET'])
def get_info():
    request_headers = dict(request.headers)
    request_remote_addr = request.remote_addr
    request_args = request.args.to_dict()
    info = {
        'headers': request_headers,
        'remote_addr': request_remote_addr,
        'args': request_args
    }
    return jsonify(info)

Log:

    2024-07-19 11:13:23 [INFO] Running stage : change header (run.py:351)
2024-07-19 11:13:23 [INFO] ------before req: {'method': 'GET', 'url': 'http://127.0.0.1:5005/get-info', 'params': {'a': 1, 'b': 2, 'c': 3}, 'headers': {'foo1': 'aaa bbb'}, 'allow_redirects': False} (conftest.py:6)
2024-07-19 11:13:23 [INFO] ------req: {'method': 'GET', 'url': 'http://127.0.0.1:5005/get-info', 'params': {'a': 8, 'b': 2, 'c': 3}, 'headers': {'bar': 'bbbbb'}, 'allow_redirects': False} (conftest.py:9)
2024-07-19 11:13:23 [DEBUG] Starting new HTTP connection (1): 127.0.0.1:5005 (connectionpool.py:243)
2024-07-19 11:13:23 [DEBUG] http://127.0.0.1:5005 "GET /get-info?a=1&b=2&c=3 HTTP/11" 200 303 (connectionpool.py:546)
2024-07-19 11:13:23 [INFO] Response: '<Response [200]>' (response.py:53)
2024-07-19 11:13:23 [INFO] ------response: {'args': {'a': '1', 'b': '2', 'c': '3'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Foo1': 'aaa bbb', 'Host': '127.0.0.1:5005', 'User-Agent': 'python-requests/2.32.3'}, 'remote_addr': '192.168.65.1'} (conftest.py:13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant