繁体   English   中英

如何检查响应的状态码更改为 json?

[英]How to check status code of response changed to json?

我有个问题。 我正在做 rtsp 客户端测试。 这个客户是我们的,工作得很好。 问题是,如果返回 json,是否存在检查响应状态的方法?

客户端.py

def get_paths(self):
   return requests.get(f"{self.API_URL}/v1/paths/list").json()

测试.py

def test_get_path(self):
    response = self.rtsp_client.get_paths()
    self.assertEqual(response, 200)

在这个例子中,我得到:

Traceback (most recent call last):
  File "/home/oliwer/Documents/repozytoriumGit/device-channels/project/backend/rtsp_simple_server_api/tests.py", line 99, in test_get_path
    self.assertEqual(response, 200)
AssertionError: {'items': {'none': {'confName': 'none', '[3105 chars][]}}} != 200

我明白为什么我得到这个 output。我只是感兴趣是否存在一种方法来测试此响应的状态代码。

附言。 关键是,我无法更改请求功能,它必须返回 json,我想得到类似的东西:

测试.py

    def test_get_path(self):
       response = self.rtsp_client.get_path().command_that_can_reverse_json()_command
       self.assertEqual(response, 200)

我想,当我有 .json() 方法时,这是可能的。 在我看来,第一个响应是 object,但编码为 json 文件。 所以我的想法是我可以逆转这个操作,但我不知道怎么做。 在你看来,我做不到。

我建议您像这样更改代码

def get_paths():
   return requests.get('https://jsonplaceholder.typicode.com/todos/1')

比您可以访问响应的状态代码:

r = get_paths()
print(r.status_code)
>>> 200

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM