/
hello.py
executable file
·381 lines (227 loc) · 6.75 KB
/
hello.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
from flask import redirect
from connexion import NoContent, ProblemException, problem, request
class DummyClass(object):
@classmethod
def test_classmethod(cls):
return cls.__name__
def test_method(self):
return self.__class__.__name__
class_instance = DummyClass() # noqa
def get():
return ''
def search():
return ''
def api_list():
return 'a'
def post():
return ''
def post_greeting(name):
data = {'greeting': 'Hello {name}'.format(name=name)}
return data
def post_goodday(name):
data = {'greeting': 'Hello {name}'.format(name=name)}
headers = {"Location": "/my/uri"}
return data, 201, headers
def post_goodday_no_header():
return {'greeting': 'Hello.'}, 201
def post_goodevening(name):
data = 'Good evening {name}'.format(name=name)
headers = {"Location": "/my/uri"}
return data, 201, headers
def get_list(name):
data = ['hello', name]
return data
def get_bye(name):
return 'Goodbye {name}'.format(name=name), 200
def get_bye_secure(name):
return 'Goodbye {name} (Secure: {user})'.format(name=name, user=request.user)
def with_problem():
return problem(type='http://www.example.com/error',
title='Some Error',
detail='Something went wrong somewhere',
status=418,
instance='instance1',
headers={'x-Test-Header': 'In Test'})
def with_problem_txt():
return problem(title='Some Error',
detail='Something went wrong somewhere',
status=418,
instance='instance1')
def internal_error():
return 42 / 0
def get_greetings(name):
"""
Used to test custom mimetypes
"""
data = {'greetings': 'Hello {name}'.format(name=name)}
return data
def multimime():
return 'Goodbye'
def empty():
return None, 204
def schema(new_stack):
return new_stack
def schema_response_object(valid):
if valid == "invalid_requirements":
return {"docker_version": 1.0}
elif valid == "invalid_type":
return {"image_version": 1.0}
else:
return {"image_version": "1.0"} # valid
def schema_response_string(valid):
if valid == "valid":
return "Image version 2.0"
else:
return 2.0
def schema_response_integer(valid):
if valid == "valid":
return 3
else:
return 3.0
def schema_response_number(valid):
if valid == "valid":
return 4.0
else:
return "Four"
def schema_response_boolean(valid):
if valid == "valid":
return True
else:
return "yes"
def schema_response_array(valid):
if valid == "invalid_dict":
return {
{"image_version": "1.0"}:
{"image_version": "2.0"}
}
elif valid == "invalid_string":
return "Not an array."
else:
return [
{"image_version": "1.0"},
{"image_version": "2.0"}
]
def schema_query(image_version=None):
return {'image_version': image_version}
def schema_list():
return ''
def schema_map():
return ''
def schema_recursive():
return ''
def schema_format():
return ''
def test_parameter_validation():
return ''
def test_required_query_param():
return ''
def test_array_csv_query_param(items):
return items
def test_array_pipes_query_param(items):
return items
def test_array_unsupported_query_param(items):
return items
def test_no_content_response():
return NoContent, 204
def test_schema_array(test_array):
return test_array
def test_schema_int(test_int):
return test_int
def test_get_someint(someint):
return type(someint).__name__
def test_get_somefloat(somefloat):
return type(somefloat).__name__
def test_default_param(name):
return {"app_name": name}
def test_default_object_body(stack):
return {"stack": stack}
def test_default_integer_body(stack_version):
return stack_version
def test_falsy_param(falsy):
return falsy
def test_formdata_param(formData):
return formData
def test_formdata_missing_param():
return ''
def test_formdata_file_upload(formData):
filename = formData.filename
contents = formData.read().decode('utf-8')
return {filename: contents}
def test_formdata_file_upload_missing_param():
return ''
def test_bool_default_param(thruthiness):
return thruthiness
def test_bool_array_param(thruthiness=None):
if thruthiness is None:
thruthiness = []
return all(thruthiness)
def test_required_param(simple):
return simple
def test_redirect_endpoint():
headers = {'Location': 'http://www.google.com/'}
return '', 302, headers
def test_redirect_response_endpoint():
return redirect('http://www.google.com/')
def test_204_with_headers():
headers = {'X-Something': 'test'}
return '', 204, headers
def test_nocontent_obj_with_headers():
headers = {'X-Something': 'test'}
return NoContent, 204, headers
def path_parameters_in_get_method(title):
return [title], 200, {}
def test_default_missmatch_definition(age):
return 'OK'
def test_array_in_path(names):
return names, 200
def test_global_response_definition():
return ['general', 'list'], 200
def test_nullable_parameters(time_start):
if time_start is None:
return 'it was None'
return time_start
def test_nullable_param_post(post_param):
if post_param is None:
return 'it was None'
return post_param
def test_nullable_param_put(contents):
if contents is None:
return 'it was None'
return contents
def test_custom_json_response():
return {'theResult': DummyClass()}, 200
def get_blob_data():
return b'cool\x00\x08'
def get_data_as_binary():
return get_blob_data(), 200, {'Content-Type': 'application/octet-stream'}
def get_data_as_text(post_param):
return ''
def get_invalid_response():
return {"simple": object()}
def get_empty_dict():
return {}
def get_custom_problem_response():
return problem(403, "You need to pay", "Missing amount",
ext={'amount': 23.0})
def throw_problem_exception():
raise ProblemException(
title="As Exception",
detail="Something wrong or not!",
ext={'age': 30}
)
def unordered_params_response(first, path_param, second):
return dict(first=int(first), path_param=str(path_param), second=int(second))
def more_than_one_scope_defined():
return "OK"
def test_args_kwargs(*args, **kwargs):
return kwargs
def test_param_sanitization(query=None, form=None):
result = {}
if query:
result['query'] = query
if form:
result['form'] = form
return result
def test_body_sanitization(body=None):
return body