MCPcopy
hub / github.com/encode/httpx / test_raise_for_status

Function test_raise_for_status

tests/models/test_responses.py:91–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89
90
91def test_raise_for_status():
92 request = httpx.Request("GET", "https://example.org")
93
94 # 2xx status codes are not an error.
95 response = httpx.Response(200, request=request)
96 response.raise_for_status()
97
98 # 1xx status codes are informational responses.
99 response = httpx.Response(101, request=request)
100 assert response.is_informational
101 with pytest.raises(httpx.HTTPStatusError) as exc_info:
102 response.raise_for_status()
103 assert str(exc_info.value) == (
104 "Informational response '101 Switching Protocols' for url 'https://example.org'\n"
105 "For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/101"
106 )
107
108 # 3xx status codes are redirections.
109 headers = {"location": "https://other.org"}
110 response = httpx.Response(303, headers=headers, request=request)
111 assert response.is_redirect
112 with pytest.raises(httpx.HTTPStatusError) as exc_info:
113 response.raise_for_status()
114 assert str(exc_info.value) == (
115 "Redirect response '303 See Other' for url 'https://example.org'\n"
116 "Redirect location: 'https://other.org'\n"
117 "For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303"
118 )
119
120 # 4xx status codes are a client error.
121 response = httpx.Response(403, request=request)
122 assert response.is_client_error
123 assert response.is_error
124 with pytest.raises(httpx.HTTPStatusError) as exc_info:
125 response.raise_for_status()
126 assert str(exc_info.value) == (
127 "Client error '403 Forbidden' for url 'https://example.org'\n"
128 "For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403"
129 )
130
131 # 5xx status codes are a server error.
132 response = httpx.Response(500, request=request)
133 assert response.is_server_error
134 assert response.is_error
135 with pytest.raises(httpx.HTTPStatusError) as exc_info:
136 response.raise_for_status()
137 assert str(exc_info.value) == (
138 "Server error '500 Internal Server Error' for url 'https://example.org'\n"
139 "For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500"
140 )
141
142 # Calling .raise_for_status without setting a request instance is
143 # not valid. Should raise a runtime error.
144 response = httpx.Response(200)
145 with pytest.raises(RuntimeError):
146 response.raise_for_status()
147
148

Callers

nothing calls this directly

Calls 1

raise_for_statusMethod · 0.95

Tested by

no test coverage detected