@@ -32,11 +32,24 @@ async def echo_put(self, request):
3232
3333 async def status_code (self , request ):
3434 status_code = int (request .match_info ['status_code' ])
35+ return await self ._handle_status_code_request (request , status_code , 'get' )
3536
37+ async def status_code_post (self , request ):
38+ request_json = await request .json ()
39+ status_code = request_json ['status_code' ]
40+ return await self ._handle_status_code_request (request , status_code , 'post' )
41+
42+ async def status_code_put (self , request ):
43+ request_json = await request .json ()
44+ status_code = request_json ['status_code' ]
45+ return await self ._handle_status_code_request (request , status_code , 'put' )
46+
47+ async def _handle_status_code_request (self , request , status_code , action ):
3648 response_json = {
3749 'code' : 'SomeCode' ,
3850 'message' : 'some message' ,
39- 'status' : status_code
51+ 'status' : status_code ,
52+ 'action' : action
4053 }
4154
4255 if self .omit_error_description :
@@ -69,6 +82,8 @@ async def _process(self, request, response_body, status_code=200):
6982 app .router .add_post ('/echo/post' , mock_server .echo_post )
7083 app .router .add_put ('/echo/put' , mock_server .echo_put )
7184 app .router .add_get (r'/status/{status_code}' , mock_server .status_code )
85+ app .router .add_post (r'/status' , mock_server .status_code_post )
86+ app .router .add_put (r'/status' , mock_server .status_code_put )
7287 server = await aiohttp_client (app ) # pylint: disable=redefined-outer-name
7388
7489 server .mock = mock_server
@@ -250,7 +265,7 @@ async def test_can_handle_stopped_node_put():
250265# endregion
251266
252267
253- # region error handling - HTTP error code
268+ # region error handling - HTTP error code (general)
254269
255270async def _assert_can_propagate_status_code_result (server , status_code ): # pylint: disable=redefined-outer-name
256271 # Arrange:
@@ -260,7 +275,7 @@ async def _assert_can_propagate_status_code_result(server, status_code): # pyli
260275 response_json = await connector .get (f'status/{ status_code } ' )
261276
262277 # Assert:
263- assert {'code' : 'SomeCode' , 'message' : 'some message' , 'status' : status_code } == response_json
278+ assert {'code' : 'SomeCode' , 'message' : 'some message' , 'status' : status_code , 'action' : 'get' } == response_json
264279
265280
266281async def _assert_can_propagate_status_code_failure_result (server , status_code ): # pylint: disable=redefined-outer-name
@@ -284,12 +299,12 @@ async def _assert_can_propagate_status_code_failure_result_with_message(server,
284299
285300
286301async def test_can_propagate_http_success_results (server ): # pylint: disable=redefined-outer-name
287- for status_code in (200 , 202 , 300 , 404 ):
302+ for status_code in (200 , 202 , 300 ):
288303 await _assert_can_propagate_status_code_result (server , status_code )
289304
290305
291306async def test_can_propagate_http_failure_results (server ): # pylint: disable=redefined-outer-name
292- for status_code in (400 , 401 , 500 , 501 ):
307+ for status_code in (400 , 401 , 404 , 500 , 501 ):
293308 await _assert_can_propagate_status_code_failure_result (server , status_code )
294309
295310
@@ -298,3 +313,74 @@ async def test_can_propagate_http_failure_results_with_message(server): # pylin
298313 await _assert_can_propagate_status_code_failure_result_with_message (server , status_code )
299314
300315# endregion
316+
317+
318+ # region error handling - HTTP error code (404)
319+
320+ async def _assert_can_handle_http_failure_404_as_error_by_default (server , action , url_path , ** kwargs ):
321+ # pylint: disable=redefined-outer-name
322+ # Arrange:
323+ connector = BasicConnector (server .make_url ('' ))
324+
325+ # Act + Assert:
326+ with pytest .raises (NodeException , match = 'HTTP request failed with code 404\n SomeCode\n some message' ):
327+ await getattr (connector , action )(url_path , ** kwargs )
328+
329+
330+ async def _assert_can_handle_http_failure_404_as_explicit_error (server , action , url_path , ** kwargs ): # pylint: disable=redefined-outer-name
331+ # Arrange:
332+ connector = BasicConnector (server .make_url ('' ))
333+
334+ # Act + Assert:
335+ with pytest .raises (NodeException , match = 'HTTP request failed with code 404\n SomeCode\n some message' ):
336+ await getattr (connector , action )(url_path , not_found_as_error = True , ** kwargs )
337+
338+
339+ async def _assert_can_handle_http_failure_404_as_explicit_non_error (server , action , url_path , ** kwargs ):
340+ # pylint: disable=redefined-outer-name
341+ # Arrange:
342+ connector = BasicConnector (server .make_url ('' ))
343+
344+ # Act:
345+ response_json = await getattr (connector , action )(url_path , not_found_as_error = False , ** kwargs )
346+
347+ # Assert:
348+ assert {'code' : 'SomeCode' , 'message' : 'some message' , 'status' : 404 , 'action' : action } == response_json
349+
350+
351+ async def test_can_handle_http_failure_404_as_error_by_default_get (server ): # pylint: disable=redefined-outer-name
352+ await _assert_can_handle_http_failure_404_as_error_by_default (server , 'get' , 'status/404' )
353+
354+
355+ async def test_can_handle_http_failure_404_as_error_by_default_post (server ): # pylint: disable=redefined-outer-name
356+ await _assert_can_handle_http_failure_404_as_error_by_default (server , 'post' , 'status' , request_payload = {'status_code' : 404 })
357+
358+
359+ async def test_can_handle_http_failure_404_as_error_by_default_put (server ): # pylint: disable=redefined-outer-name
360+ await _assert_can_handle_http_failure_404_as_error_by_default (server , 'put' , 'status' , request_payload = {'status_code' : 404 })
361+
362+
363+ async def test_can_handle_http_failure_404_as_explicit_error_get (server ): # pylint: disable=redefined-outer-name
364+ await _assert_can_handle_http_failure_404_as_explicit_error (server , 'get' , 'status/404' )
365+
366+
367+ async def test_can_handle_http_failure_404_as_explicit_error_post (server ): # pylint: disable=redefined-outer-name
368+ await _assert_can_handle_http_failure_404_as_explicit_error (server , 'post' , 'status' , request_payload = {'status_code' : 404 })
369+
370+
371+ async def test_can_handle_http_failure_404_as_explicit_error_put (server ): # pylint: disable=redefined-outer-name
372+ await _assert_can_handle_http_failure_404_as_explicit_error (server , 'put' , 'status' , request_payload = {'status_code' : 404 })
373+
374+
375+ async def test_can_handle_http_failure_404_as_explicit_non_error_get (server ): # pylint: disable=redefined-outer-name
376+ await _assert_can_handle_http_failure_404_as_explicit_non_error (server , 'get' , 'status/404' )
377+
378+
379+ async def test_can_handle_http_failure_404_as_explicit_non_error_post (server ): # pylint: disable=redefined-outer-name
380+ await _assert_can_handle_http_failure_404_as_explicit_non_error (server , 'post' , 'status' , request_payload = {'status_code' : 404 })
381+
382+
383+ async def test_can_handle_http_failure_404_as_explicit_non_error_put (server ): # pylint: disable=redefined-outer-name
384+ await _assert_can_handle_http_failure_404_as_explicit_non_error (server , 'put' , 'status' , request_payload = {'status_code' : 404 })
385+
386+ # endregion
0 commit comments