3737from azure .core .pipeline .transport import RequestsTransportResponse
3838from azure .core .pipeline .transport ._base import _HttpResponseBase as PipelineTransportHttpResponseBase
3939from azure .core .rest ._http_response_impl import _HttpResponseBaseImpl as RestHttpResponseBase
40+ from utils import HTTP_REQUESTS
4041
4142class PipelineTransportMockResponse (PipelineTransportHttpResponseBase ):
4243 def __init__ (self , json_body ):
@@ -152,6 +153,8 @@ def test_deserialized_httpresponse_error_code(self, mock_response):
152153 assert error .error .error .code == "FakeErrorOne"
153154 assert error .error .error .message == "A fake error"
154155
156+ assert str (error ) == "(FakeErrorOne) A fake error\n Code: FakeErrorOne\n Message: A fake error"
157+
155158
156159 @pytest .mark .parametrize ("mock_response" , MOCK_RESPONSES )
157160 def test_deserialized_httpresponse_error_message (self , mock_response ):
@@ -284,4 +287,36 @@ def test_null_odata_details(self, mock_response):
284287 }
285288 }
286289 exp = HttpResponseError (response = mock_response (json .dumps (message ).encode ("utf-8" )))
287- assert exp .error .code == "501"
290+ assert exp .error .code == "501"
291+
292+ @pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
293+ def test_non_odatav4_error_body (self , client , http_request ):
294+ request = http_request ("GET" , "/errors/non-odatav4-body" )
295+ response = client .send_request (request )
296+ with pytest .raises (HttpResponseError ) as ex :
297+ response .raise_for_status ()
298+ assert str (ex .value ) == "Operation returned an invalid status 'BAD REQUEST'\n Content: {\" code\" : 400, \" error\" : {\" global\" : [\" MY-ERROR-MESSAGE-THAT-IS-COMING-FROM-THE-API\" ]}}"
299+
300+ @pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
301+ def test_malformed_json (self , client , http_request ):
302+ request = http_request ("GET" , "/errors/malformed-json" )
303+ response = client .send_request (request )
304+ with pytest .raises (HttpResponseError ) as ex :
305+ response .raise_for_status ()
306+ assert str (ex .value ) == "Operation returned an invalid status 'BAD REQUEST'\n Content: {\" code\" : 400, \" error\" : {\" global\" : [\" MY-ERROR-MESSAGE-THAT-IS-COMING-FROM-THE-API\" ]"
307+
308+ @pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
309+ def test_text (self , client , http_request ):
310+ request = http_request ("GET" , "/errors/text" )
311+ response = client .send_request (request )
312+ with pytest .raises (HttpResponseError ) as ex :
313+ response .raise_for_status ()
314+ assert str (ex .value ) == "Operation returned an invalid status 'BAD REQUEST'\n Content: I am throwing an error"
315+
316+ @pytest .mark .parametrize ("http_request" , HTTP_REQUESTS )
317+ def test_datav4_error (self , client , http_request ):
318+ request = http_request ("GET" , "/errors/odatav4" )
319+ response = client .send_request (request )
320+ with pytest .raises (HttpResponseError ) as ex :
321+ response .raise_for_status ()
322+ assert "Content: {\" " not in str (ex .value )
0 commit comments