|
@@ -0,0 +1,231 @@
|
|
1
|
+# coding: utf-8
|
|
2
|
+from aiohttp import web
|
|
3
|
+import marshmallow
|
|
4
|
+
|
|
5
|
+from hapic import Hapic
|
|
6
|
+from hapic import HapicData
|
|
7
|
+from hapic.ext.aiohttp.context import AiohttpContext
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+class TestAiohttpExt(object):
|
|
11
|
+ async def test_aiohttp_only__ok__nominal_case(
|
|
12
|
+ self,
|
|
13
|
+ aiohttp_client,
|
|
14
|
+ loop,
|
|
15
|
+ ):
|
|
16
|
+ async def hello(request):
|
|
17
|
+ return web.Response(text='Hello, world')
|
|
18
|
+
|
|
19
|
+ app = web.Application(debug=True)
|
|
20
|
+ app.router.add_get('/', hello)
|
|
21
|
+ client = await aiohttp_client(app)
|
|
22
|
+ resp = await client.get('/')
|
|
23
|
+ assert resp.status == 200
|
|
24
|
+ text = await resp.text()
|
|
25
|
+ assert 'Hello, world' in text
|
|
26
|
+
|
|
27
|
+ async def test_aiohttp_input_path__ok__nominal_case(
|
|
28
|
+ self,
|
|
29
|
+ aiohttp_client,
|
|
30
|
+ loop,
|
|
31
|
+ ):
|
|
32
|
+ hapic = Hapic(async_=True)
|
|
33
|
+
|
|
34
|
+ class InputPathSchema(marshmallow.Schema):
|
|
35
|
+ name = marshmallow.fields.String()
|
|
36
|
+
|
|
37
|
+ @hapic.input_path(InputPathSchema())
|
|
38
|
+ async def hello(request, hapic_data: HapicData):
|
|
39
|
+ name = hapic_data.path.get('name')
|
|
40
|
+ return web.Response(text='Hello, {}'.format(name))
|
|
41
|
+
|
|
42
|
+ app = web.Application(debug=True)
|
|
43
|
+ app.router.add_get('/{name}', hello)
|
|
44
|
+ hapic.set_context(AiohttpContext(app))
|
|
45
|
+ client = await aiohttp_client(app)
|
|
46
|
+
|
|
47
|
+ resp = await client.get('/bob')
|
|
48
|
+ assert resp.status == 200
|
|
49
|
+
|
|
50
|
+ text = await resp.text()
|
|
51
|
+ assert 'Hello, bob' in text
|
|
52
|
+
|
|
53
|
+ async def test_aiohttp_input_path__error_wrong_input_parameter(
|
|
54
|
+ self,
|
|
55
|
+ aiohttp_client,
|
|
56
|
+ loop,
|
|
57
|
+ ):
|
|
58
|
+ hapic = Hapic(async_=True)
|
|
59
|
+
|
|
60
|
+ class InputPathSchema(marshmallow.Schema):
|
|
61
|
+ i = marshmallow.fields.Integer()
|
|
62
|
+
|
|
63
|
+ @hapic.input_path(InputPathSchema())
|
|
64
|
+ async def hello(request, hapic_data: HapicData):
|
|
65
|
+ i = hapic_data.path.get('i')
|
|
66
|
+ return web.Response(text='integer: {}'.format(str(i)))
|
|
67
|
+
|
|
68
|
+ app = web.Application(debug=True)
|
|
69
|
+ app.router.add_get('/{i}', hello)
|
|
70
|
+ hapic.set_context(AiohttpContext(app))
|
|
71
|
+ client = await aiohttp_client(app)
|
|
72
|
+
|
|
73
|
+ resp = await client.get('/bob') # NOTE: should be integer here
|
|
74
|
+ assert resp.status == 400
|
|
75
|
+
|
|
76
|
+ error = await resp.json()
|
|
77
|
+ assert 'Validation error of input data' in error.get('message')
|
|
78
|
+ assert {'i': ['Not a valid integer.']} == error.get('details')
|
|
79
|
+
|
|
80
|
+ async def test_aiohttp_input_body__ok_nominal_case(
|
|
81
|
+ self,
|
|
82
|
+ aiohttp_client,
|
|
83
|
+ loop,
|
|
84
|
+ ):
|
|
85
|
+ hapic = Hapic(async_=True)
|
|
86
|
+
|
|
87
|
+ class InputBodySchema(marshmallow.Schema):
|
|
88
|
+ name = marshmallow.fields.String()
|
|
89
|
+
|
|
90
|
+ @hapic.input_body(InputBodySchema())
|
|
91
|
+ async def hello(request, hapic_data: HapicData):
|
|
92
|
+ name = hapic_data.body.get('name')
|
|
93
|
+ return web.Response(text='Hello, {}'.format(name))
|
|
94
|
+
|
|
95
|
+ app = web.Application(debug=True)
|
|
96
|
+ app.router.add_post('/', hello)
|
|
97
|
+ hapic.set_context(AiohttpContext(app))
|
|
98
|
+ client = await aiohttp_client(app)
|
|
99
|
+
|
|
100
|
+ resp = await client.post('/', data={'name': 'bob'})
|
|
101
|
+ assert resp.status == 200
|
|
102
|
+
|
|
103
|
+ text = await resp.text()
|
|
104
|
+ assert 'Hello, bob' in text
|
|
105
|
+
|
|
106
|
+ async def test_aiohttp_input_body__error__incorrect_input_body(
|
|
107
|
+ self,
|
|
108
|
+ aiohttp_client,
|
|
109
|
+ loop,
|
|
110
|
+ ):
|
|
111
|
+ hapic = Hapic(async_=True)
|
|
112
|
+
|
|
113
|
+ class InputBodySchema(marshmallow.Schema):
|
|
114
|
+ i = marshmallow.fields.Integer()
|
|
115
|
+
|
|
116
|
+ @hapic.input_body(InputBodySchema())
|
|
117
|
+ async def hello(request, hapic_data: HapicData):
|
|
118
|
+ i = hapic_data.body.get('i')
|
|
119
|
+ return web.Response(text='integer, {}'.format(i))
|
|
120
|
+
|
|
121
|
+ app = web.Application(debug=True)
|
|
122
|
+ app.router.add_post('/', hello)
|
|
123
|
+ hapic.set_context(AiohttpContext(app))
|
|
124
|
+ client = await aiohttp_client(app)
|
|
125
|
+
|
|
126
|
+ resp = await client.post('/', data={'i': 'bob'}) # NOTE: should be int
|
|
127
|
+ assert resp.status == 400
|
|
128
|
+
|
|
129
|
+ error = await resp.json()
|
|
130
|
+ assert 'Validation error of input data' in error.get('message')
|
|
131
|
+ assert {'i': ['Not a valid integer.']} == error.get('details')
|
|
132
|
+
|
|
133
|
+ async def test_aiohttp_output_body__ok__nominal_case(
|
|
134
|
+ self,
|
|
135
|
+ aiohttp_client,
|
|
136
|
+ loop,
|
|
137
|
+ ):
|
|
138
|
+ hapic = Hapic(async_=True)
|
|
139
|
+
|
|
140
|
+ class OuputBodySchema(marshmallow.Schema):
|
|
141
|
+ name = marshmallow.fields.String()
|
|
142
|
+
|
|
143
|
+ @hapic.output_body(OuputBodySchema())
|
|
144
|
+ async def hello(request):
|
|
145
|
+ return {
|
|
146
|
+ 'name': 'bob',
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ app = web.Application(debug=True)
|
|
150
|
+ app.router.add_get('/', hello)
|
|
151
|
+ hapic.set_context(AiohttpContext(app))
|
|
152
|
+ client = await aiohttp_client(app)
|
|
153
|
+
|
|
154
|
+ resp = await client.get('/')
|
|
155
|
+ assert resp.status == 200
|
|
156
|
+
|
|
157
|
+ data = await resp.json()
|
|
158
|
+ assert 'bob' == data.get('name')
|
|
159
|
+
|
|
160
|
+ async def test_aiohttp_output_body__error__incorrect_output_body(
|
|
161
|
+ self,
|
|
162
|
+ aiohttp_client,
|
|
163
|
+ loop,
|
|
164
|
+ ):
|
|
165
|
+ hapic = Hapic(async_=True)
|
|
166
|
+
|
|
167
|
+ class OuputBodySchema(marshmallow.Schema):
|
|
168
|
+ i = marshmallow.fields.Integer(required=True)
|
|
169
|
+
|
|
170
|
+ @hapic.output_body(OuputBodySchema())
|
|
171
|
+ async def hello(request):
|
|
172
|
+ return {
|
|
173
|
+ 'i': 'bob', # NOTE: should be integer
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ app = web.Application(debug=True)
|
|
177
|
+ app.router.add_get('/', hello)
|
|
178
|
+ hapic.set_context(AiohttpContext(app))
|
|
179
|
+ client = await aiohttp_client(app)
|
|
180
|
+
|
|
181
|
+ resp = await client.get('/')
|
|
182
|
+ assert resp.status == 500
|
|
183
|
+
|
|
184
|
+ data = await resp.json()
|
|
185
|
+ assert 'Validation error of output data' == data.get('message')
|
|
186
|
+ assert {
|
|
187
|
+ 'i': ['Missing data for required field.'],
|
|
188
|
+ } == data.get('details')
|
|
189
|
+
|
|
190
|
+ async def test_aiohttp_output_stream__ok__nominal_case(
|
|
191
|
+ self,
|
|
192
|
+ aiohttp_client,
|
|
193
|
+ loop,
|
|
194
|
+ ):
|
|
195
|
+ hapic = Hapic(async_=True)
|
|
196
|
+
|
|
197
|
+ class AsyncGenerator:
|
|
198
|
+ def __init__(self):
|
|
199
|
+ self._iterator = iter([
|
|
200
|
+ {'name': 'Hello, bob'},
|
|
201
|
+ {'name': 'Hello, franck'},
|
|
202
|
+ ])
|
|
203
|
+
|
|
204
|
+ async def __aiter__(self):
|
|
205
|
+ return self
|
|
206
|
+
|
|
207
|
+ async def __anext__(self):
|
|
208
|
+ return next(self._iterator)
|
|
209
|
+
|
|
210
|
+ class OuputStreamItemSchema(marshmallow.Schema):
|
|
211
|
+ name = marshmallow.fields.String()
|
|
212
|
+
|
|
213
|
+ @hapic.output_stream(OuputStreamItemSchema())
|
|
214
|
+ async def hello(request):
|
|
215
|
+ return AsyncGenerator()
|
|
216
|
+
|
|
217
|
+ app = web.Application(debug=True)
|
|
218
|
+ app.router.add_get('/', hello)
|
|
219
|
+ hapic.set_context(AiohttpContext(app))
|
|
220
|
+ client = await aiohttp_client(app)
|
|
221
|
+
|
|
222
|
+ resp = await client.get('/')
|
|
223
|
+ assert resp.status == 200
|
|
224
|
+
|
|
225
|
+ line = await resp.content.readline()
|
|
226
|
+ assert b'{"name": "Hello, bob"}\n' == line
|
|
227
|
+
|
|
228
|
+ line = await resp.content.readline()
|
|
229
|
+ assert b'{"name": "Hello, franck"}\n' == line
|
|
230
|
+
|
|
231
|
+ # TODO BS 2018-07-26: How to ensure we are at end of response ?
|