-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtest_https_fn.py
244 lines (195 loc) · 7.51 KB
/
test_https_fn.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
"""
Tests for the https module.
"""
import unittest
from flask import Flask, Request
from werkzeug.test import EnvironBuilder
from firebase_functions import core, https_fn
class TestHttps(unittest.TestCase):
"""
Tests for the http module.
"""
def test_on_request_calls_init_function(self):
app = Flask(__name__)
hello = None
@core.init
def init():
nonlocal hello
hello = "world"
@https_fn.on_request()
def func(_):
pass
with app.test_request_context("/"):
environ = EnvironBuilder(
method="POST",
json={
"data": {
"test": "value"
},
},
).get_environ()
request = Request(environ)
func(request)
self.assertEqual(hello, "world")
def test_on_call_calls_init_function(self):
app = Flask(__name__)
hello = None
@core.init
def init():
nonlocal hello
hello = "world"
@https_fn.on_call()
def func(_):
pass
with app.test_request_context("/"):
environ = EnvironBuilder(
method="POST",
json={
"data": {
"test": "value"
},
},
).get_environ()
request = Request(environ)
func(request)
self.assertEqual("world", hello)
def test_callable_encoding(self):
app = Flask(__name__)
@https_fn.on_call()
def add(req: https_fn.CallableRequest[int]):
return req.data + 1
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST", json={
"data": 1
}).get_environ()
request = Request(environ)
response = add(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json(), {"result": 2})
def test_callable_errors(self):
app = Flask(__name__)
@https_fn.on_call()
def throw_generic_error(req):
# pylint: disable=broad-exception-raised
raise Exception("Invalid type")
@https_fn.on_call()
def throw_access_denied(req):
raise https_fn.HttpsError(
https_fn.FunctionsErrorCode.PERMISSION_DENIED,
"Permission is denied")
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST", json={
"data": None
}).get_environ()
request = Request(environ)
response = throw_generic_error(request)
self.assertEqual(response.status_code, 500)
self.assertEqual(
response.get_json(),
{"error": {
"message": "INTERNAL",
"status": "INTERNAL"
}})
response = throw_access_denied(request)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.get_json(), {
"error": {
"message": "Permission is denied",
"status": "PERMISSION_DENIED"
}
})
def test_yielding_without_streaming(self):
app = Flask(__name__)
@https_fn.on_call()
def yielder(req: https_fn.CallableRequest[int]):
yield from range(req.data)
return "OK"
@https_fn.on_call()
def yield_thrower(req: https_fn.CallableRequest[int]):
yield from range(req.data)
raise https_fn.HttpsError(
https_fn.FunctionsErrorCode.PERMISSION_DENIED,
"Can't read anymore")
@https_fn.on_call()
def legacy_yielder(req: https_fn.CallableRequest[int]):
yield from range(req.data)
# Prior to Python 3.3, this was the way "return" was handled
# Python 3.5 made this messy however because it converts
# raised StopIteration into a RuntimeError
# pylint: disable=stop-iteration-return
raise StopIteration("OK")
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST", json={
"data": 5
}).get_environ()
request = Request(environ)
response = yielder(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json(), {"result": "OK"})
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST", json={
"data": 5
}).get_environ()
request = Request(environ)
response = legacy_yielder(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.get_json(), {"result": "OK"})
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST", json={
"data": 3
}).get_environ()
request = Request(environ)
response = yield_thrower(request)
self.assertEqual(response.status_code, 403)
self.assertEqual(
response.get_json(), {
"error": {
"message": "Can't read anymore",
"status": "PERMISSION_DENIED"
}
})
def test_yielding_with_streaming(self):
app = Flask(__name__)
@https_fn.on_call()
def yielder(req: https_fn.CallableRequest[int]):
yield from range(req.data)
return "OK"
@https_fn.on_call()
def yield_thrower(req: https_fn.CallableRequest[int]):
yield from range(req.data)
raise https_fn.HttpsError(https_fn.FunctionsErrorCode.INTERNAL,
"Throwing")
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST",
json={
"data": 2
},
headers={
"accept": "text/event-stream"
}).get_environ()
request = Request(environ)
response = yielder(request)
self.assertEqual(response.status_code, 200)
chunks = list(response.response)
self.assertEqual(chunks, [
'data: {"message": 0}\n\n', 'data: {"message": 1}\n\n',
'data: {"result": "OK"}\n\n', "END"
])
with app.test_request_context("/"):
environ = EnvironBuilder(method="POST",
json={
"data": 2
},
headers={
"accept": "text/event-stream"
}).get_environ()
request = Request(environ)
response = yield_thrower(request)
self.assertEqual(response.status_code, 200)
chunks = list(response.response)
self.assertEqual(chunks, [
'data: {"message": 0}\n\n', 'data: {"message": 1}\n\n',
'error: {"error": {"status": "INTERNAL", "message": "Throwing"}}\n\n',
"END"
])