|
@@ -2,12 +2,15 @@
|
2
|
2
|
import typing
|
3
|
3
|
from http import HTTPStatus
|
4
|
4
|
|
|
5
|
+import marshmallow
|
|
6
|
+
|
5
|
7
|
from hapic.context import ContextInterface
|
6
|
8
|
from hapic.data import HapicData
|
7
|
9
|
from hapic.decorator import ControllerWrapper
|
8
|
10
|
from hapic.decorator import InputControllerWrapper
|
9
|
11
|
from hapic.decorator import OutputControllerWrapper
|
10
|
12
|
from hapic.processor import RequestParameters
|
|
13
|
+from hapic.processor import MarshmallowOutputProcessor
|
11
|
14
|
from hapic.processor import ProcessValidationError
|
12
|
15
|
from hapic.processor import ProcessorInterface
|
13
|
16
|
from tests.base import Base
|
|
@@ -92,6 +95,10 @@ class MyInputControllerWrapper(InputControllerWrapper):
|
92
|
95
|
hapic_data.query = processed_data
|
93
|
96
|
|
94
|
97
|
|
|
98
|
+class MySchema(marshmallow.Schema):
|
|
99
|
+ name = marshmallow.fields.String(required=True)
|
|
100
|
+
|
|
101
|
+
|
95
|
102
|
class TestControllerWrapper(Base):
|
96
|
103
|
def test_unit__base_controller_wrapper__ok__no_behaviour(self):
|
97
|
104
|
context = MyContext()
|
|
@@ -171,3 +178,23 @@ class TestOutputControllerWrapper(Base):
|
171
|
178
|
'http_code': HTTPStatus.OK,
|
172
|
179
|
'original_response': 43,
|
173
|
180
|
} == result
|
|
181
|
+
|
|
182
|
+ def test_unit__output_data_wrapping__fail__error_response(self):
|
|
183
|
+ context = MyContext()
|
|
184
|
+ processor = MarshmallowOutputProcessor()
|
|
185
|
+ processor.schema = MySchema()
|
|
186
|
+ wrapper = OutputControllerWrapper(context, processor)
|
|
187
|
+
|
|
188
|
+ @wrapper.get_wrapper
|
|
189
|
+ def func(foo):
|
|
190
|
+ return 'wrong result format'
|
|
191
|
+
|
|
192
|
+ result = func(42)
|
|
193
|
+ # see MyProcessor#process
|
|
194
|
+ assert isinstance(result, dict)
|
|
195
|
+ assert 'http_code' in result
|
|
196
|
+ assert result['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR
|
|
197
|
+ assert 'original_error' in result
|
|
198
|
+ assert result['original_error'].error_details == {
|
|
199
|
+ 'name': ['Missing data for required field.']
|
|
200
|
+ }
|