-
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 11, 2020
1 parent
46af9a8
commit 99ccb22
Showing
8 changed files
with
104 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from typing import Type | ||
|
||
from restit.resource import Resource, PathParameter | ||
|
||
|
||
# noinspection PyShadowingBuiltins | ||
def path_parameter(name: str, type=str, doc: str = None): | ||
def decorator(clazz: Type[Resource]): | ||
clazz.add_path_parameter(PathParameter(name, type, doc)) | ||
return clazz | ||
|
||
return decorator |
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,36 @@ | ||
import unittest | ||
|
||
from restit import request_mapping, Request, Response, path_parameter, RestitApp, RestitTestApp | ||
from restit.resource import Resource | ||
|
||
|
||
@request_mapping("/path/:id1/and/:id2/and/:id3") | ||
@path_parameter("id1", type=int, doc="First path parameter") | ||
@path_parameter("id2", type=float, doc="Second path parameter") | ||
@path_parameter("id3") | ||
class Resource1(Resource): | ||
def get(self, request: Request, **path_params) -> Response: | ||
return Response(path_params) | ||
|
||
|
||
class PathParameterTestCase(unittest.TestCase): | ||
def setUp(self) -> None: | ||
restit_app = RestitApp(resources=[ | ||
Resource1() | ||
]) | ||
self.restit_test_app = RestitTestApp(restit_app) | ||
|
||
def test_path_parameters(self): | ||
response = self.restit_test_app.get("/path/1/and/10/and/20") | ||
self.assertEqual(200, response.get_status_code()) | ||
self.assertEqual({'id1': 1, 'id2': 10.0, 'id3': '20'}, response.json()) | ||
|
||
def test_conversion_exception(self): | ||
response = self.restit_test_app.get("/path/1/and/hans/and/20") | ||
self.assertEqual(400, response.get_status_code()) | ||
self.assertEqual( | ||
"<title>400 Bad Request</title>\n" | ||
"<h1>Bad Request</h1>\n" | ||
"<p>Path parameter value 'hans' is not matching 'PathParameter(name='id2', type=<class 'float'>, " | ||
"doc='Second path parameter')' (could not convert string to float: 'hans')</p>\n", response.text | ||
) |
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