-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tuerke Erik
committed
Mar 24, 2020
1 parent
19c6d10
commit 7b5ed84
Showing
4 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import unittest | ||
|
||
from restit import Resource, Request, Response, RestItTestApp, RestItApp | ||
from restit.decorator import path, exception_mapping | ||
from restit.exception import BadRequest, NotFound | ||
|
||
|
||
class MyException1(Exception): | ||
pass | ||
|
||
|
||
@path("/") | ||
@exception_mapping({MyException1: BadRequest}) | ||
class MyResource(Resource): | ||
def get(self, request: Request) -> Response: | ||
raise MyException1("Hello") | ||
|
||
@exception_mapping({MyException1: (NotFound, "Miau")}) | ||
def post(self, request: Request) -> Response: | ||
raise MyException1() | ||
|
||
|
||
class ExceptionMappingTestTestCase(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.restit_test_app = RestItTestApp(RestItApp(resources=[MyResource()], raise_exceptions=True)) | ||
|
||
def test_map_exception(self): | ||
with self.assertRaises(BadRequest) as exception: | ||
self.restit_test_app.get("/") | ||
|
||
self.assertEqual("Hello", str(exception.exception)) | ||
|
||
def test_override(self): | ||
with self.assertRaises(NotFound) as exception: | ||
self.restit_test_app.post("/") | ||
|
||
self.assertEqual("Miau", str(exception.exception)) |