/
test__helpers.py
292 lines (228 loc) · 10.5 KB
/
test__helpers.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
# Copyright 2017 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
_FLASK_TRACE_ID = "flask-id"
_FLASK_HTTP_REQUEST = {"requestUrl": "https://flask.palletsprojects.com/en/1.1.x/"}
_DJANGO_TRACE_ID = "django-id"
_DJANGO_HTTP_REQUEST = {"requestUrl": "https://www.djangoproject.com/"}
class Test_get_request_data_from_flask(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.logging_v2.handlers import _helpers
return _helpers.get_request_data_from_flask()
@staticmethod
def create_app():
import flask
app = flask.Flask(__name__)
@app.route("/")
def index():
return "test flask trace" # pragma: NO COVER
return app
def test_no_context_header(self):
app = self.create_app()
with app.test_request_context(path="/", headers={}):
http_request, trace_id = self._call_fut()
self.assertIsNone(trace_id)
self.assertEqual(http_request["requestMethod"], "GET")
def test_valid_context_header(self):
flask_trace_header = "X_CLOUD_TRACE_CONTEXT"
expected_trace_id = _FLASK_TRACE_ID
flask_trace_id = expected_trace_id + "/testspanid"
app = self.create_app()
context = app.test_request_context(
path="/", headers={flask_trace_header: flask_trace_id}
)
with context:
http_request, trace_id = self._call_fut()
self.assertEqual(trace_id, expected_trace_id)
self.assertEqual(http_request["requestMethod"], "GET")
def test_http_request_populated(self):
expected_path = "http://testserver/123"
expected_agent = "Mozilla/5.0"
expected_referrer = "self"
expected_ip = "10.1.2.3"
body_content = "test"
headers = {
"User-Agent": expected_agent,
"Referer": expected_referrer,
}
app = self.create_app()
with app.test_client() as c:
c.put(
path=expected_path,
data=body_content,
environ_base={"REMOTE_ADDR": expected_ip},
headers=headers,
)
http_request, trace_id = self._call_fut()
self.assertEqual(http_request["requestMethod"], "PUT")
self.assertEqual(http_request["requestUrl"], expected_path)
self.assertEqual(http_request["userAgent"], expected_agent)
self.assertEqual(http_request["referer"], expected_referrer)
self.assertEqual(http_request["remoteIp"], expected_ip)
self.assertEqual(http_request["requestSize"], len(body_content))
self.assertEqual(http_request["protocol"], "HTTP/1.1")
def test_http_request_sparse(self):
expected_path = "http://testserver/123"
app = self.create_app()
with app.test_client() as c:
c.put(path=expected_path)
http_request, trace_id = self._call_fut()
self.assertEqual(http_request["requestMethod"], "PUT")
self.assertEqual(http_request["requestUrl"], expected_path)
self.assertEqual(http_request["protocol"], "HTTP/1.1")
class Test_get_request_data_from_django(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.logging_v2.handlers import _helpers
return _helpers.get_request_data_from_django()
def setUp(self):
from django.conf import settings
from django.test.utils import setup_test_environment
if not settings.configured:
settings.configure()
setup_test_environment()
def tearDown(self):
from django.test.utils import teardown_test_environment
from google.cloud.logging_v2.handlers.middleware import request
teardown_test_environment()
request._thread_locals.__dict__.clear()
def test_no_context_header(self):
from django.test import RequestFactory
from google.cloud.logging_v2.handlers.middleware import request
django_request = RequestFactory().get("/")
middleware = request.RequestMiddleware(None)
middleware.process_request(django_request)
http_request, trace_id = self._call_fut()
self.assertEqual(http_request["requestMethod"], "GET")
self.assertIsNone(trace_id)
def test_valid_context_header(self):
from django.test import RequestFactory
from google.cloud.logging_v2.handlers.middleware import request
django_trace_header = "HTTP_X_CLOUD_TRACE_CONTEXT"
expected_trace_id = "testtraceiddjango"
django_trace_id = expected_trace_id + "/testspanid"
django_request = RequestFactory().get(
"/", **{django_trace_header: django_trace_id}
)
middleware = request.RequestMiddleware(None)
middleware.process_request(django_request)
http_request, trace_id = self._call_fut()
self.assertEqual(trace_id, expected_trace_id)
self.assertEqual(http_request["requestMethod"], "GET")
def test_http_request_populated(self):
from django.test import RequestFactory
from google.cloud.logging_v2.handlers.middleware import request
expected_path = "http://testserver/123"
expected_agent = "Mozilla/5.0"
expected_referrer = "self"
body_content = "test"
django_request = RequestFactory().put(
expected_path,
data=body_content,
HTTP_USER_AGENT=expected_agent,
HTTP_REFERER=expected_referrer,
)
# ensure test passes even after request has been read
# context: https://github.com/googleapis/python-logging/issues/159
django_request.read()
middleware = request.RequestMiddleware(None)
middleware.process_request(django_request)
http_request, trace_id = self._call_fut()
self.assertEqual(http_request["requestMethod"], "PUT")
self.assertEqual(http_request["requestUrl"], expected_path)
self.assertEqual(http_request["userAgent"], expected_agent)
self.assertEqual(http_request["referer"], expected_referrer)
self.assertEqual(http_request["remoteIp"], "127.0.0.1")
self.assertEqual(http_request["requestSize"], len(body_content))
self.assertEqual(http_request["protocol"], "HTTP/1.1")
def test_http_request_sparse(self):
from django.test import RequestFactory
from google.cloud.logging_v2.handlers.middleware import request
expected_path = "http://testserver/123"
django_request = RequestFactory().put(expected_path)
middleware = request.RequestMiddleware(None)
middleware.process_request(django_request)
http_request, trace_id = self._call_fut()
self.assertEqual(http_request["requestMethod"], "PUT")
self.assertEqual(http_request["requestUrl"], expected_path)
self.assertEqual(http_request["remoteIp"], "127.0.0.1")
self.assertEqual(http_request["protocol"], "HTTP/1.1")
class Test_get_request_data(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.logging_v2.handlers import _helpers
return _helpers.get_request_data()
def _helper(self, django_return, flask_return):
django_patch = mock.patch(
"google.cloud.logging_v2.handlers._helpers.get_request_data_from_django",
return_value=django_return,
)
flask_patch = mock.patch(
"google.cloud.logging_v2.handlers._helpers.get_request_data_from_flask",
return_value=flask_return,
)
with django_patch as django_mock:
with flask_patch as flask_mock:
result = self._call_fut()
return django_mock, flask_mock, result
def test_from_django(self):
django_expected = (_DJANGO_HTTP_REQUEST, _DJANGO_TRACE_ID)
flask_expected = (None, None)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
self.assertEqual(output, django_expected)
django_mock.assert_called_once_with()
flask_mock.assert_not_called()
def test_from_flask(self):
django_expected = (None, None)
flask_expected = (_FLASK_HTTP_REQUEST, _FLASK_TRACE_ID)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
self.assertEqual(output, flask_expected)
django_mock.assert_called_once_with()
flask_mock.assert_called_once_with()
def test_from_django_and_flask(self):
django_expected = (_DJANGO_HTTP_REQUEST, _DJANGO_TRACE_ID)
flask_expected = (_FLASK_HTTP_REQUEST, _FLASK_TRACE_ID)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
# Django wins.
self.assertEqual(output, django_expected)
django_mock.assert_called_once_with()
flask_mock.assert_not_called()
def test_missing_http_request(self):
flask_expected = (None, _FLASK_TRACE_ID)
django_expected = (None, _DJANGO_TRACE_ID)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
# function only returns trace if http_request data is present
self.assertEqual(output, (None, None))
django_mock.assert_called_once_with()
flask_mock.assert_called_once_with()
def test_missing_trace_id(self):
flask_expected = (_FLASK_HTTP_REQUEST, None)
django_expected = (None, _DJANGO_TRACE_ID)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
# trace_id is optional
self.assertEqual(output, flask_expected)
django_mock.assert_called_once_with()
flask_mock.assert_called_once_with()
def test_missing_both(self):
flask_expected = (None, None)
django_expected = (None, None)
django_mock, flask_mock, output = self._helper(django_expected, flask_expected)
self.assertEqual(output, (None, None))
django_mock.assert_called_once_with()
flask_mock.assert_called_once_with()
def test_wo_libraries(self):
output = self._call_fut()
self.assertEqual(output, (None, None))