2121from io import StringIO
2222
2323import pytest
24- from conftest import FAILING_ON_WINDOWS
2524from testing_support .certs import CERT_PATH
2625from testing_support .mock_external_http_server import MockExternalHTTPServer
2726
4342
4443def echo_full_request (self ):
4544 self .server .connections .append (self .connection )
46- request_line = str (self .requestline ).encode ("utf-8" )
47- headers = "\n " .join (f"{ k .lower ()} : { v } " for k , v in self .headers .items ())
48- self .send_response (200 )
49- self .end_headers ()
50- self .wfile .write (request_line )
51- self .wfile .write (b"\n " )
52- self .wfile .write (headers .strip ().encode ("utf-8" ))
53- self .wfile .write (b"\n " )
45+ request_line = str (self .requestline )
46+ headers = list (self .headers .items ())
47+
5448 content_length = int (self .headers .get ("Content-Length" , 0 ))
5549 if content_length :
56- data = self .rfile .read (content_length )
57- self .wfile .write (data )
50+ body = self .rfile .read (content_length ).hex ()
51+ else :
52+ body = ""
53+
54+ payload = [request_line , headers , body ]
55+ payload = json .dumps (payload ).encode ("utf-8" )
56+
57+ self .send_response (200 )
58+ self .end_headers ()
59+ self .wfile .write (payload )
60+
61+
62+ def decode_payload (data ):
63+ if isinstance (data , bytes ):
64+ data = data .decode ("utf-8" )
65+ payload = json .loads (data )
66+ payload [2 ] = bytes .fromhex (payload [2 ]) # Convert body back to bytes
67+ return payload
5868
5969
6070class InsecureServer (MockExternalHTTPServer ):
@@ -158,28 +168,23 @@ def test_http_no_payload(server, method):
158168 status , data = client .send_request (method = method , headers = {"foo" : "bar" })
159169
160170 assert status == 200
161- data = ensure_str (data )
162- data = data .split ("\n " )
171+ request_line , headers , _payload = decode_payload (data )
163172
164173 # Verify connection has been closed
165174 assert client ._connection_attr is None
166175 assert connection .pool is None
167176
168177 # Verify request line
169- assert data [ 0 ] .startswith (f"{ method } /agent_listener/invoke_raw_method " )
178+ assert request_line .startswith (f"{ method } /agent_listener/invoke_raw_method " )
170179
171180 # Verify headers
172181 user_agent_header = ""
173182 foo_header = ""
174183
175- for header in data [1 :- 1 ]:
176- if header .lower ().startswith ("user-agent:" ):
177- _ , value = header .split (":" , 1 )
178- value = value .strip ()
184+ for key , value in headers :
185+ if key .lower () == "user-agent" :
179186 user_agent_header = value
180- elif header .startswith ("foo:" ):
181- _ , value = header .split (":" , 1 )
182- value = value .strip ()
187+ if key .lower () == "foo" :
183188 foo_header = value
184189
185190 assert user_agent_header .startswith ("NewRelic-PythonAgent/" )
@@ -228,7 +233,6 @@ def test_http_close_connection_in_context_manager():
228233 client .close_connection ()
229234
230235
231- @FAILING_ON_WINDOWS
232236@pytest .mark .parametrize (
233237 "client_cls,method,threshold" ,
234238 (
@@ -267,8 +271,7 @@ def test_http_payload_compression(server, client_cls, method, threshold):
267271 status , data = client .send_request (payload = payload , params = {"method" : "method2" })
268272
269273 assert status == 200
270- data = data .split (b"\n " )
271- sent_payload = data [- 1 ]
274+ _request_line , headers , sent_payload = decode_payload (data )
272275 payload_byte_len = len (sent_payload )
273276 internal_metrics = dict (internal_metrics .metrics ())
274277 if client_cls is ApplicationModeClient :
@@ -292,7 +295,7 @@ def test_http_payload_compression(server, client_cls, method, threshold):
292295 assert not internal_metrics
293296
294297 if threshold < 20 :
295- expected_content_encoding = method . encode ( "utf-8" )
298+ expected_content_encoding = method
296299 assert sent_payload != payload
297300 if method == "deflate" :
298301 sent_payload = zlib .decompress (sent_payload )
@@ -301,12 +304,11 @@ def test_http_payload_compression(server, client_cls, method, threshold):
301304 sent_payload = decompressor .decompress (sent_payload )
302305 sent_payload += decompressor .flush ()
303306 else :
304- expected_content_encoding = b "Identity"
307+ expected_content_encoding = "Identity"
305308
306- for header in data [1 :- 1 ]:
307- if header .lower ().startswith (b"content-encoding" ):
308- _ , content_encoding = header .split (b":" , 1 )
309- content_encoding = content_encoding .strip ()
309+ for key , value in headers :
310+ if key .lower () == "content-encoding" :
311+ content_encoding = value
310312 break
311313 else :
312314 raise AssertionError ("Missing content-encoding header" )
@@ -375,16 +377,13 @@ def test_ssl_via_ssl_proxy(server, auth):
375377 status , data = client .send_request ()
376378
377379 assert status == 200
378- data = data .decode ("utf-8" )
379- data = data .split ("\n " )
380- assert data [0 ].startswith ("POST https://localhost:1/agent_listener/invoke_raw_method " )
380+ request_line , headers , _payload = decode_payload (data )
381+ assert request_line .startswith ("POST https://localhost:1/agent_listener/invoke_raw_method " )
381382
382383 proxy_auth = None
383- for header in data [1 :- 1 ]:
384- if header .lower ().startswith ("proxy-authorization" ):
385- _ , proxy_auth = header .split (":" , 1 )
386- proxy_auth = proxy_auth .strip ()
387- break
384+ for key , value in headers :
385+ if key .lower () == "proxy-authorization" :
386+ proxy_auth = value
388387
389388 if proxy_user :
390389 auth_expected = proxy_user
@@ -410,9 +409,8 @@ def test_non_ssl_via_ssl_proxy(server):
410409 status , data = client .send_request ()
411410
412411 assert status == 200
413- data = data .decode ("utf-8" )
414- data = data .split ("\n " )
415- assert data [0 ].startswith ("POST http://localhost:1/agent_listener/invoke_raw_method " )
412+ request_line , _headers , _payload = decode_payload (data )
413+ assert request_line .startswith ("POST http://localhost:1/agent_listener/invoke_raw_method " )
416414
417415 assert server .httpd .connect_host is None
418416
@@ -424,9 +422,8 @@ def test_non_ssl_via_non_ssl_proxy(insecure_server):
424422 status , data = client .send_request ()
425423
426424 assert status == 200
427- data = data .decode ("utf-8" )
428- data = data .split ("\n " )
429- assert data [0 ].startswith ("POST http://localhost:1/agent_listener/invoke_raw_method " )
425+ request_line , _headers , _payload = decode_payload (data )
426+ assert request_line .startswith ("POST http://localhost:1/agent_listener/invoke_raw_method " )
430427
431428 assert insecure_server .httpd .connect_host is None
432429
0 commit comments