1414# limitations under the License.
1515
1616import gc
17- import webtest
18- import pytest
1917import time
18+
19+ import pytest
20+ import webtest
21+ from testing_support .fixtures import validate_attributes , validate_transaction_metrics
22+ from testing_support .sample_applications import simple_app , simple_app_raw
23+
24+ import newrelic .packages .six as six
2025from newrelic .api .application import application_instance
2126from newrelic .api .web_transaction import WebTransaction
22- from testing_support .fixtures import (validate_transaction_metrics ,
23- validate_attributes )
24- from testing_support .sample_applications import simple_app
25- import newrelic .packages .six as six
27+ from newrelic .api .wsgi_application import wsgi_application
28+
2629application = webtest .TestApp (simple_app )
2730
2831
2932# TODO: WSGI metrics must not be generated for a WebTransaction
3033METRICS = (
31- (' Python/WSGI/Input/Bytes' , None ),
32- (' Python/WSGI/Input/Time' , None ),
33- (' Python/WSGI/Input/Calls/read' , None ),
34- (' Python/WSGI/Input/Calls/readline' , None ),
35- (' Python/WSGI/Input/Calls/readlines' , None ),
36- (' Python/WSGI/Output/Bytes' , None ),
37- (' Python/WSGI/Output/Time' , None ),
38- (' Python/WSGI/Output/Calls/yield' , None ),
39- (' Python/WSGI/Output/Calls/write' , None ),
34+ (" Python/WSGI/Input/Bytes" , None ),
35+ (" Python/WSGI/Input/Time" , None ),
36+ (" Python/WSGI/Input/Calls/read" , None ),
37+ (" Python/WSGI/Input/Calls/readline" , None ),
38+ (" Python/WSGI/Input/Calls/readlines" , None ),
39+ (" Python/WSGI/Output/Bytes" , None ),
40+ (" Python/WSGI/Output/Time" , None ),
41+ (" Python/WSGI/Output/Calls/yield" , None ),
42+ (" Python/WSGI/Output/Calls/write" , None ),
4043)
4144
4245
43- # TODO: Add rollup_metrics=METRICS
46+ # Test for presence of framework and dispatcher info based on whether framework is specified
47+ @validate_transaction_metrics (
48+ name = "test" , custom_metrics = [("Python/Framework/framework/v1" , 1 ), ("Python/Dispatcher/dispatcher/v1.0.0" , 1 )]
49+ )
50+ def test_dispatcher_and_framework_metrics ():
51+ inner_wsgi_decorator = wsgi_application (
52+ name = "test" , framework = ("framework" , "v1" ), dispatcher = ("dispatcher" , "v1.0.0" )
53+ )
54+ decorated_application = inner_wsgi_decorator (simple_app_raw )
55+
56+ application = webtest .TestApp (decorated_application )
57+ application .get ("/" )
58+
59+
60+ # Test for presence of framework and dispatcher info under existing transaction
4461@validate_transaction_metrics (
45- 'test_base_web_transaction' ,
46- group = 'Test' )
47- @validate_attributes ('agent' ,
48- [
49- 'request.headers.accept' , 'request.headers.contentLength' ,
50- 'request.headers.contentType' , 'request.headers.host' ,
51- 'request.headers.referer' , 'request.headers.userAgent' , 'request.method' ,
52- 'request.uri' , 'response.status' , 'response.headers.contentLength' ,
53- 'response.headers.contentType' , 'request.parameters.foo' ,
54- 'request.parameters.boo' , 'webfrontend.queue.seconds' ,
55- ])
56- @pytest .mark .parametrize ('use_bytes' , (True , False ))
62+ name = "test" , custom_metrics = [("Python/Framework/framework/v1" , 1 ), ("Python/Dispatcher/dispatcher/v1.0.0" , 1 )]
63+ )
64+ def test_double_wrapped_dispatcher_and_framework_metrics ():
65+ inner_wsgi_decorator = wsgi_application (
66+ name = "test" , framework = ("framework" , "v1" ), dispatcher = ("dispatcher" , "v1.0.0" )
67+ )
68+ decorated_application = inner_wsgi_decorator (simple_app_raw )
69+
70+ outer_wsgi_decorator = wsgi_application (name = "double_wrapped" )
71+ double_decorated_application = outer_wsgi_decorator (decorated_application )
72+
73+ application = webtest .TestApp (double_decorated_application )
74+ application .get ("/" )
75+
76+
77+ # TODO: Add rollup_metrics=METRICS
78+ @validate_transaction_metrics ("test_base_web_transaction" , group = "Test" )
79+ @validate_attributes (
80+ "agent" ,
81+ [
82+ "request.headers.accept" ,
83+ "request.headers.contentLength" ,
84+ "request.headers.contentType" ,
85+ "request.headers.host" ,
86+ "request.headers.referer" ,
87+ "request.headers.userAgent" ,
88+ "request.method" ,
89+ "request.uri" ,
90+ "response.status" ,
91+ "response.headers.contentLength" ,
92+ "response.headers.contentType" ,
93+ "request.parameters.foo" ,
94+ "request.parameters.boo" ,
95+ "webfrontend.queue.seconds" ,
96+ ],
97+ )
98+ @pytest .mark .parametrize ("use_bytes" , (True , False ))
5799def test_base_web_transaction (use_bytes ):
58100 application = application_instance ()
59101
60102 request_headers = {
61- ' Accept' : ' text/plain' ,
62- ' Content-Length' : '0' ,
63- ' Content-Type' : ' text/plain' ,
64- ' Host' : ' localhost' ,
65- ' Referer' : ' http://example.com?q=1&boat=⛵' ,
66- ' User-Agent' : ' potato' ,
67- ' X-Request-Start' : str (time .time () - 0.2 ),
68- ' newRelic' : ' invalid' ,
103+ " Accept" : " text/plain" ,
104+ " Content-Length" : "0" ,
105+ " Content-Type" : " text/plain" ,
106+ " Host" : " localhost" ,
107+ " Referer" : " http://example.com?q=1&boat=⛵" ,
108+ " User-Agent" : " potato" ,
109+ " X-Request-Start" : str (time .time () - 0.2 ),
110+ " newRelic" : " invalid" ,
69111 }
70112
71113 if use_bytes :
72114 byte_headers = {}
73115
74116 for name , value in request_headers .items ():
75- name = name .encode (' utf-8' )
117+ name = name .encode (" utf-8" )
76118 try :
77- value = value .encode (' utf-8' )
119+ value = value .encode (" utf-8" )
78120 except UnicodeDecodeError :
79121 assert six .PY2
80122 byte_headers [name ] = value
81123
82124 request_headers = byte_headers
83125
84126 transaction = WebTransaction (
85- application ,
86- ' test_base_web_transaction' ,
87- group = ' Test' ,
88- scheme = ' http' ,
89- host = ' localhost' ,
90- port = 8000 ,
91- request_method = ' HEAD' ,
92- request_path = ' /foobar' ,
93- query_string = ' foo=bar&boo=baz' ,
94- headers = request_headers .items (),
127+ application ,
128+ " test_base_web_transaction" ,
129+ group = " Test" ,
130+ scheme = " http" ,
131+ host = " localhost" ,
132+ port = 8000 ,
133+ request_method = " HEAD" ,
134+ request_path = " /foobar" ,
135+ query_string = " foo=bar&boo=baz" ,
136+ headers = request_headers .items (),
95137 )
96138
97139 if use_bytes :
98- response_headers = ((b'Content-Length' , b'0' ),
99- (b'Content-Type' , b'text/plain' ))
140+ response_headers = ((b"Content-Length" , b"0" ), (b"Content-Type" , b"text/plain" ))
100141 else :
101- response_headers = (('Content-Length' , '0' ),
102- ('Content-Type' , 'text/plain' ))
142+ response_headers = (("Content-Length" , "0" ), ("Content-Type" , "text/plain" ))
103143
104144 with transaction :
105145 transaction .process_response (200 , response_headers )
@@ -117,8 +157,8 @@ def validate_no_garbage():
117157
118158
119159@validate_transaction_metrics (
120- name = '' ,
121- group = ' Uri' ,
160+ name = "" ,
161+ group = " Uri" ,
122162)
123163def test_wsgi_app_memory (validate_no_garbage ):
124- application .get ('/' )
164+ application .get ("/" )
0 commit comments