|
1 | 1 | import base64
|
| 2 | +import builtins |
2 | 3 | import json
|
3 | 4 | import zlib
|
4 | 5 | from decimal import Decimal
|
5 | 6 | from pathlib import Path
|
6 | 7 | from typing import Dict
|
| 8 | +from unittest.mock import MagicMock |
7 | 9 |
|
8 | 10 | import pytest
|
9 | 11 |
|
@@ -502,73 +504,6 @@ def custom_method():
|
502 | 504 | assert headers["Access-Control-Allow-Methods"] == "CUSTOM"
|
503 | 505 |
|
504 | 506 |
|
505 |
| -def test_unhandled_exceptions_debug_on(): |
506 |
| - # GIVEN debug is enabled |
507 |
| - # AND an unhandled exception is raised |
508 |
| - app = ApiGatewayResolver(debug=True) |
509 |
| - assert app._debug |
510 |
| - |
511 |
| - @app.get("/raises-error") |
512 |
| - def raises_error(): |
513 |
| - raise RuntimeError("Foo") |
514 |
| - |
515 |
| - # WHEN calling the handler |
516 |
| - result = app({"path": "/raises-error", "httpMethod": "GET"}, None) |
517 |
| - |
518 |
| - # THEN return a 500 |
519 |
| - # AND Content-Type is set to text/plain |
520 |
| - # AND include the exception traceback in the response |
521 |
| - assert result["statusCode"] == 500 |
522 |
| - assert "Traceback (most recent call last)" in result["body"] |
523 |
| - headers = result["headers"] |
524 |
| - assert headers["Content-Type"] == content_types.TEXT_PLAIN |
525 |
| - |
526 |
| - |
527 |
| -def test_unhandled_exceptions_debug_off(): |
528 |
| - # GIVEN debug is disabled |
529 |
| - # AND an unhandled exception is raised |
530 |
| - app = ApiGatewayResolver(debug=False) |
531 |
| - assert not app._debug |
532 |
| - |
533 |
| - @app.get("/raises-error") |
534 |
| - def raises_error(): |
535 |
| - raise RuntimeError("Foo") |
536 |
| - |
537 |
| - # WHEN calling the handler |
538 |
| - # THEN raise the original exception |
539 |
| - with pytest.raises(RuntimeError) as e: |
540 |
| - app({"path": "/raises-error", "httpMethod": "GET"}, None) |
541 |
| - |
542 |
| - # AND include the original error |
543 |
| - assert e.value.args == ("Foo",) |
544 |
| - |
545 |
| - |
546 |
| -def test_debug_mode_environment_variable(monkeypatch): |
547 |
| - # GIVEN a debug mode environment variable is set |
548 |
| - monkeypatch.setenv(constants.EVENT_HANDLER_DEBUG_ENV, "true") |
549 |
| - app = ApiGatewayResolver() |
550 |
| - |
551 |
| - # WHEN calling app._debug |
552 |
| - # THEN the debug mode is enabled |
553 |
| - assert app._debug |
554 |
| - |
555 |
| - |
556 |
| -def test_debug_json_formatting(): |
557 |
| - # GIVEN debug is True |
558 |
| - app = ApiGatewayResolver(debug=True) |
559 |
| - response = {"message": "Foo"} |
560 |
| - |
561 |
| - @app.get("/foo") |
562 |
| - def foo(): |
563 |
| - return response |
564 |
| - |
565 |
| - # WHEN calling the handler |
566 |
| - result = app({"path": "/foo", "httpMethod": "GET"}, None) |
567 |
| - |
568 |
| - # THEN return a pretty print json in the body |
569 |
| - assert result["body"] == json.dumps(response, indent=4) |
570 |
| - |
571 |
| - |
572 | 507 | def test_service_error_responses():
|
573 | 508 | # SCENARIO handling different kind of service errors being raised
|
574 | 509 | app = ApiGatewayResolver(cors=CORSConfig())
|
@@ -651,3 +586,85 @@ def service_error():
|
651 | 586 | assert "Access-Control-Allow-Origin" in result["headers"]
|
652 | 587 | expected = {"statusCode": 502, "message": "Something went wrong!"}
|
653 | 588 | assert result["body"] == json_dump(expected)
|
| 589 | + |
| 590 | + |
| 591 | +def test_debug_unhandled_exceptions_debug_on(): |
| 592 | + # GIVEN debug is enabled |
| 593 | + # AND an unhandled exception is raised |
| 594 | + app = ApiGatewayResolver(debug=True) |
| 595 | + assert app._debug |
| 596 | + |
| 597 | + @app.get("/raises-error") |
| 598 | + def raises_error(): |
| 599 | + raise RuntimeError("Foo") |
| 600 | + |
| 601 | + # WHEN calling the handler |
| 602 | + result = app({"path": "/raises-error", "httpMethod": "GET"}, None) |
| 603 | + |
| 604 | + # THEN return a 500 |
| 605 | + # AND Content-Type is set to text/plain |
| 606 | + # AND include the exception traceback in the response |
| 607 | + assert result["statusCode"] == 500 |
| 608 | + assert "Traceback (most recent call last)" in result["body"] |
| 609 | + headers = result["headers"] |
| 610 | + assert headers["Content-Type"] == content_types.TEXT_PLAIN |
| 611 | + |
| 612 | + |
| 613 | +def test_debug_unhandled_exceptions_debug_off(): |
| 614 | + # GIVEN debug is disabled |
| 615 | + # AND an unhandled exception is raised |
| 616 | + app = ApiGatewayResolver(debug=False) |
| 617 | + assert not app._debug |
| 618 | + |
| 619 | + @app.get("/raises-error") |
| 620 | + def raises_error(): |
| 621 | + raise RuntimeError("Foo") |
| 622 | + |
| 623 | + # WHEN calling the handler |
| 624 | + # THEN raise the original exception |
| 625 | + with pytest.raises(RuntimeError) as e: |
| 626 | + app({"path": "/raises-error", "httpMethod": "GET"}, None) |
| 627 | + |
| 628 | + # AND include the original error |
| 629 | + assert e.value.args == ("Foo",) |
| 630 | + |
| 631 | + |
| 632 | +def test_debug_mode_environment_variable(monkeypatch): |
| 633 | + # GIVEN a debug mode environment variable is set |
| 634 | + monkeypatch.setenv(constants.EVENT_HANDLER_DEBUG_ENV, "true") |
| 635 | + app = ApiGatewayResolver() |
| 636 | + |
| 637 | + # WHEN calling app._debug |
| 638 | + # THEN the debug mode is enabled |
| 639 | + assert app._debug |
| 640 | + |
| 641 | + |
| 642 | +def test_debug_json_formatting(): |
| 643 | + # GIVEN debug is True |
| 644 | + app = ApiGatewayResolver(debug=True) |
| 645 | + response = {"message": "Foo"} |
| 646 | + |
| 647 | + @app.get("/foo") |
| 648 | + def foo(): |
| 649 | + return response |
| 650 | + |
| 651 | + # WHEN calling the handler |
| 652 | + result = app({"path": "/foo", "httpMethod": "GET"}, None) |
| 653 | + |
| 654 | + # THEN return a pretty print json in the body |
| 655 | + assert result["body"] == json.dumps(response, indent=4) |
| 656 | + |
| 657 | + |
| 658 | +def test_debug_print_event(monkeypatch): |
| 659 | + # GIVE debug is True |
| 660 | + app = ApiGatewayResolver(debug=True) |
| 661 | + mocked_print = MagicMock() |
| 662 | + monkeypatch.setattr(builtins, "print", mocked_print) |
| 663 | + |
| 664 | + # WHEN calling resolve |
| 665 | + event = {"path": "/foo", "httpMethod": "GET"} |
| 666 | + app(event, None) |
| 667 | + |
| 668 | + # THEN print the event |
| 669 | + # NOTE: other calls might have happened outside of this mock |
| 670 | + mocked_print.assert_any_call(json.dumps(event, indent=4)) |
0 commit comments