簡體   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