简体   繁体   English

如何检查预期数据是通过 post 还是 put 请求发送的?

[英]How to check if the expected data is sent by a post or put request?

I am using requests-mock and I am trying to figure out how to assert that the put request has been called correctly:我正在使用requests-mock并试图弄清楚如何断言已正确调用put请求:


def funtion_to_be_tested():
    requests.put(
        headers={'X-API-Key': 'api_key'},
        url=url,
        params={'param1': 'foo'},
        data='This is the data'
    )


def test_upload(requests_mock):
    url = 'http://example.com/upload'
    requests_mock.put(url, text='ok')
    funtion_to_be_tested()
    # how to check that `data` was `This is the data` and that `headers` contained the `X-API-Key`?

Edit: I refactored out the code to be tested to a function called funtion_to_be_tested编辑:我将要测试的代码重构为一个名为funtion_to_be_tested的函数

If you just want check the method being used, you can check that by verifying the sent request.如果您只想检查正在使用的方法,您可以通过验证发送的请求来检查。

def test_upload(requests_mock: Any):
    url = 'http://example.com/upload'
    requests_mock.put(url, text='ok')
    r = requests.put(
        headers={'X-API-Key': 'api_key'},
        url=url,
        params={'param1': 'foo'},
        data='This is the data'
    )
    print(r.request.method)  # Prints PUT/POST 
    print(r.request.headers) # Prints your headers sent
    print(r.request.body)    # Prints your data sent

I have included the request headers/body in the code above in case you want to check the other parameters as well.我在上面的代码中包含了请求标头/正文,以防您还想检查其他参数。

The standard way to do this is run through your function and then make assertions on the request_history :执行此操作的标准方法是运行您的函数,然后对request_history进行断言:

Altering the example because it always takes me a while to make pytest work correctly, but it will work basically the same way:更改示例,因为我总是需要一段时间才能使 pytest 正常工作,但它的工作方式基本相同:

import requests
import requests_mock

def funtion_to_be_tested():
    requests.put(
        headers={'X-API-Key': 'api_key'},
        url=url,
        params={'param1': 'foo'},
        data='This is the data'
    )

with requests_mock.mock() as m:
    url = 'http://example.com/upload'
    m.put(url, text='ok')
    funtion_to_be_tested()

    assert m.last_request.headers['X-API-Key'] == 'api_key'
    assert m.last_request.text == 'This is the data'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查看Flask中POST/PUT api方法的数据 - How to check data of POST/PUT api method in Flask 如何检查Pyserial发送的数据 - How to check the data sent by Pyserial Django:如何在发布请求后将数据保存到数据库之前如何检查数据是否正确? - Django: How to check if data is correct before saving it to a database on a post request? 如何读取 python 请求发送的 post 请求的标头数据? - How can i read the data of headers of a post request sent by python requests? 如何在Django Rest Framework序列化程序中访问通过POST请求发送的额外数据 - How to access extra data sent with POST request in Django Rest Framework serializer 如何查看post请求的进度? - How to check the progress of the post request? 如何使用Celery和Flask请求POST PUT方法? - How to request POST PUT methods with Celery and Flask? 带有已发送表单数据的 POST 请求未返回正确的响应 - POST request with sent form data does not return correct response 在Django中增加通过HTTP POST请求发送的数据大小 - Increase the size of data sent through HTTP POST request in Django 通过 POST 方法发送数据以显示在 AJAX 请求中 - Sent data through POST method to show in AJAX request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM