|
19 | 19 |
|
20 | 20 | from google.rpc import code_pb2 |
21 | 21 | import flask |
| 22 | +from werkzeug.exceptions import NotFound |
22 | 23 | import mock |
23 | 24 |
|
24 | 25 | from opencensus.ext.flask import flask_middleware |
@@ -311,6 +312,48 @@ def test__after_request_sampled(self): |
311 | 312 | self.assertEqual(span.attributes, expected_attributes) |
312 | 313 | assert isinstance(span.parent_span, base.NullContextManager) |
313 | 314 |
|
| 315 | + def test__after_request_invalid_url(self): |
| 316 | + flask_trace_header = 'traceparent' |
| 317 | + trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' |
| 318 | + span_id = '6e0c63257de34c92' |
| 319 | + flask_trace_id = '00-{}-{}-00'.format(trace_id, span_id) |
| 320 | + |
| 321 | + app = self.create_app() |
| 322 | + flask_middleware.FlaskMiddleware( |
| 323 | + app=app, |
| 324 | + sampler=samplers.AlwaysOnSampler() |
| 325 | + ) |
| 326 | + |
| 327 | + context = app.test_request_context( |
| 328 | + path='/this-url-does-not-exist', |
| 329 | + headers={flask_trace_header: flask_trace_id} |
| 330 | + ) |
| 331 | + |
| 332 | + with context: |
| 333 | + app.preprocess_request() |
| 334 | + tracer = execution_context.get_opencensus_tracer() |
| 335 | + self.assertIsNotNone(tracer) |
| 336 | + |
| 337 | + span = tracer.current_span() |
| 338 | + |
| 339 | + try: |
| 340 | + rv = app.dispatch_request() |
| 341 | + except NotFound as e: |
| 342 | + rv = app.handle_user_exception(e) |
| 343 | + app.finalize_request(rv) |
| 344 | + |
| 345 | + # http.route should not be set |
| 346 | + expected_attributes = { |
| 347 | + 'http.host': u'localhost', |
| 348 | + 'http.method': u'GET', |
| 349 | + 'http.path': u'/this-url-does-not-exist', |
| 350 | + 'http.url': u'http://localhost/this-url-does-not-exist', |
| 351 | + 'http.status_code': 404 |
| 352 | + } |
| 353 | + |
| 354 | + self.assertEqual(span.attributes, expected_attributes) |
| 355 | + assert isinstance(span.parent_span, base.NullContextManager) |
| 356 | + |
314 | 357 | def test__after_request_blacklist(self): |
315 | 358 | flask_trace_header = 'traceparent' |
316 | 359 | trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05' |
|
0 commit comments