4
4
import unittest
5
5
import mock
6
6
7
- from flask import Flask
7
+ import flask
8
8
from flask_zipkin import Zipkin
9
9
10
10
@@ -15,27 +15,48 @@ def handle_transport(*kargs, **kwargs):
15
15
class FlaskZipkinTestCase (unittest .TestCase ):
16
16
17
17
def setUp (self ):
18
- app = Flask (__name__ )
18
+ app = flask . Flask (__name__ )
19
19
app .testing = True
20
20
app .config ['ZIPKIN_DISABLE' ] = False
21
21
app .config ['ZIPKIN_DSN' ] = 'whatever'
22
22
23
+ bp = flask .Blueprint ('bp' , __name__ , url_prefix = '/bp' )
24
+
25
+ @app .route ('/foo' )
26
+ def foo ():
27
+ return 'bar'
28
+
29
+ @bp .route ('/bar' )
30
+ def bar ():
31
+ return 'foo'
32
+
23
33
z = Zipkin ()
24
- z .init_app (app )
25
34
z ._transport_handler = handle_transport
26
35
27
36
self .z = z
28
37
self .app = app
29
-
30
- @app .route ('/foo' )
31
- def foo ():
32
- return 'bar'
38
+ self .app .register_blueprint (bp )
39
+ z .init_app (self .app )
33
40
34
41
@mock .patch ('py_zipkin.zipkin.create_endpoint' )
35
42
def test_normal_get (self , create_endpoint_mock ):
36
43
rv = self .app .test_client ().get ('/foo' )
37
44
assert rv .status_code == 200
38
45
46
+ @mock .patch ('py_zipkin.zipkin.create_endpoint' )
47
+ def test_flask_request_context_with_app (self , create_endpoint_mock ):
48
+ with self .app .test_request_context ('/foo' ):
49
+ self .app .preprocess_request ()
50
+ assert flask .g ._zipkin_span
51
+ assert flask .g ._zipkin_span .span_name == 'foo.GET'
52
+
53
+ @mock .patch ('py_zipkin.zipkin.create_endpoint' )
54
+ def test_flask_request_context_with_bp (self , create_endpoint_mock ):
55
+ with self .app .test_request_context ('/bp/bar' ):
56
+ self .app .preprocess_request ()
57
+ assert flask .g ._zipkin_span
58
+ assert flask .g ._zipkin_span .span_name == 'bp.bar.GET'
59
+
39
60
40
61
def suite ():
41
62
suite = unittest .TestSuite ()
0 commit comments