55import typing
66from unittest import mock
77
8- import httpretty
9- import httpretty .core
10- import httpretty .http
118import urllib3
129import urllib3 .exceptions
10+ from mocket import Mocket , Mocketizer
11+ from mocket .mocks .mockhttp import Entry
1312
1413from opentelemetry import trace
1514from opentelemetry .instrumentation ._semconv import (
@@ -68,18 +67,20 @@ def setUp(self):
6867
6968 URLLib3Instrumentor ().instrument ()
7069
71- httpretty .enable (allow_net_connect = False )
72- httpretty .register_uri (httpretty .GET , self .HTTP_URL , body = "Hello!" )
73- httpretty .register_uri (httpretty .GET , self .HTTPS_URL , body = "Hello!" )
74- httpretty .register_uri (httpretty .POST , self .HTTP_URL , body = "Hello!" )
70+ self .mocketizer = Mocketizer (strict_mode = True )
71+ self .mocketizer .enter ()
72+ Entry .single_register (
73+ Entry .GET , self .HTTP_URL , body = "Hello!" , match_querystring = False
74+ )
75+ Entry .single_register (Entry .GET , self .HTTPS_URL , body = "Hello!" )
76+ Entry .single_register (Entry .POST , self .HTTP_URL , body = "Hello!" )
7577
7678 def tearDown (self ):
7779 super ().tearDown ()
7880 self .env_patch .stop ()
7981 URLLib3Instrumentor ().uninstrument ()
8082
81- httpretty .disable ()
82- httpretty .reset ()
83+ self .mocketizer .exit ()
8384
8485 def assert_span (self , exporter = None , num_spans = 1 ):
8586 if exporter is None :
@@ -252,7 +253,7 @@ def test_schema_url_both_semconv(self):
252253
253254 def test_basic_not_found (self ):
254255 url_404 = "http://mock/status/404"
255- httpretty . register_uri ( httpretty .GET , url_404 , status = 404 )
256+ Entry . single_register ( Entry .GET , url_404 , status = 404 )
256257
257258 response = self .perform_request (url_404 )
258259 self .assertEqual (404 , response .status )
@@ -263,7 +264,7 @@ def test_basic_not_found(self):
263264
264265 def test_basic_not_found_new_semconv (self ):
265266 url_404 = "http://mock/status/404"
266- httpretty . register_uri ( httpretty .GET , url_404 , status = 404 )
267+ Entry . single_register ( Entry .GET , url_404 , status = 404 )
267268
268269 response = self .perform_request (url_404 )
269270 self .assertEqual (404 , response .status )
@@ -274,7 +275,7 @@ def test_basic_not_found_new_semconv(self):
274275
275276 def test_basic_not_found_both_semconv (self ):
276277 url_404 = "http://mock/status/404"
277- httpretty . register_uri ( httpretty .GET , url_404 , status = 404 )
278+ Entry . single_register ( Entry .GET , url_404 , status = 404 )
278279
279280 response = self .perform_request (url_404 )
280281 self .assertEqual (404 , response .status )
@@ -284,9 +285,12 @@ def test_basic_not_found_both_semconv(self):
284285 self .assertEqual (404 , span .attributes .get ("http.status_code" ))
285286 self .assertIs (trace .status .StatusCode .ERROR , span .status .status_code )
286287
287- @mock .patch ("httpretty.http.HttpBaseClass.METHODS" , ("NONSTANDARD" ,))
288+ @mock .patch (
289+ "mocket.mocks.mockhttp.Entry.METHODS" ,
290+ Entry .METHODS + ("NONSTANDARD" ,),
291+ )
288292 def test_nonstandard_http_method (self ):
289- httpretty . register_uri (
293+ Entry . single_register (
290294 "NONSTANDARD" , self .HTTP_URL , body = "Hello!" , status = 405
291295 )
292296 self .perform_request (self .HTTP_URL , method = "NONSTANDARD" )
@@ -295,9 +299,12 @@ def test_nonstandard_http_method(self):
295299 self .assertEqual (span .attributes .get ("http.method" ), "_OTHER" )
296300 self .assertEqual (span .attributes .get ("http.status_code" ), 405 )
297301
298- @mock .patch ("httpretty.http.HttpBaseClass.METHODS" , ("NONSTANDARD" ,))
302+ @mock .patch (
303+ "mocket.mocks.mockhttp.Entry.METHODS" ,
304+ Entry .METHODS + ("NONSTANDARD" ,),
305+ )
299306 def test_nonstandard_http_method_new_semconv (self ):
300- httpretty . register_uri (
307+ Entry . single_register (
301308 "NONSTANDARD" , self .HTTP_URL , body = "Hello!" , status = 405
302309 )
303310 self .perform_request (self .HTTP_URL , method = "NONSTANDARD" )
@@ -309,9 +316,12 @@ def test_nonstandard_http_method_new_semconv(self):
309316 )
310317 self .assertEqual (span .attributes .get ("http.response.status_code" ), 405 )
311318
312- @mock .patch ("httpretty.http.HttpBaseClass.METHODS" , ("NONSTANDARD" ,))
319+ @mock .patch (
320+ "mocket.mocks.mockhttp.Entry.METHODS" ,
321+ Entry .METHODS + ("NONSTANDARD" ,),
322+ )
313323 def test_nonstandard_http_method_both_semconv (self ):
314- httpretty . register_uri (
324+ Entry . single_register (
315325 "NONSTANDARD" , self .HTTP_URL , body = "Hello!" , status = 405
316326 )
317327 self .perform_request (self .HTTP_URL , method = "NONSTANDARD" )
@@ -327,31 +337,31 @@ def test_nonstandard_http_method_both_semconv(self):
327337
328338 def test_basic_http_non_default_port (self ):
329339 url = "http://mock:666/status/200"
330- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
340+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
331341
332342 response = self .perform_request (url )
333343 self .assert_success_span (response , url )
334344
335345 def test_basic_http_absolute_url (self ):
336346 url = "http://mock:666/status/200"
337- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
347+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
338348 pool = urllib3 .HTTPConnectionPool ("mock" , port = 666 )
339349 response = pool .request ("GET" , url )
340350
341351 self .assert_success_span (response , url )
342352
343353 def test_url_open_explicit_arg_parameters (self ):
344354 url = "http://mock:666/status/200"
345- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
355+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
346356 pool = urllib3 .HTTPConnectionPool ("mock" , port = 666 )
347357 response = pool .urlopen (method = "GET" , url = "/status/200" )
348358
349359 self .assert_success_span (response , url )
350360
351361 def test_excluded_urls_explicit (self ):
352362 url_201 = "http://mock/status/201"
353- httpretty . register_uri (
354- httpretty .GET ,
363+ Entry . single_register (
364+ Entry .GET ,
355365 url_201 ,
356366 status = 201 ,
357367 )
@@ -365,8 +375,8 @@ def test_excluded_urls_explicit(self):
365375
366376 def test_excluded_urls_from_env (self ):
367377 url = "http://localhost/env_excluded_arg/123"
368- httpretty . register_uri (
369- httpretty .GET ,
378+ Entry . single_register (
379+ Entry .GET ,
370380 url ,
371381 status = 200 ,
372382 )
@@ -410,7 +420,7 @@ def test_context_propagation(self):
410420 self .assertEqual (b"Hello!" , response .data )
411421
412422 span = self .assert_span ()
413- headers = dict (httpretty .last_request ().headers )
423+ headers = dict (Mocket .last_request ().headers )
414424
415425 self .assertIn (MockTextMapPropagator .TRACE_ID_KEY , headers )
416426 self .assertEqual (
@@ -610,9 +620,12 @@ def test_custom_response_headers_captured(self):
610620 "X-Custom-Header" : "custom-value" ,
611621 "X-Another-Header" : "another-value" ,
612622 }
613- url = "http://mock//capture_headers"
614- httpretty .register_uri (
615- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
623+ url = "http://mock/capture_headers"
624+ Entry .single_register (
625+ Entry .GET ,
626+ url ,
627+ body = "Hello!" ,
628+ headers = response_headers ,
616629 )
617630 self .perform_request (url )
618631
@@ -665,9 +678,12 @@ def test_sensitive_headers_sanitized(self):
665678 "Set-Cookie" : "session=abc123" ,
666679 "X-Secret" : "secret" ,
667680 }
668- url = "http://mock//capture_headers"
669- httpretty .register_uri (
670- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
681+ url = "http://mock/capture_headers"
682+ Entry .single_register (
683+ Entry .GET ,
684+ url ,
685+ body = "Hello!" ,
686+ headers = response_headers ,
671687 )
672688 self .perform_request (
673689 url ,
@@ -708,9 +724,12 @@ def test_custom_headers_with_regex(self):
708724 "X-Custom-Response-B" : "value-B" ,
709725 "X-Other-Response-Header" : "other-value" ,
710726 }
711- url = "http://mock//capture_headers"
712- httpretty .register_uri (
713- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
727+ url = "http://mock/capture_headers"
728+ Entry .single_register (
729+ Entry .GET ,
730+ url ,
731+ body = "Hello!" ,
732+ headers = response_headers ,
714733 )
715734 self .perform_request (
716735 url ,
@@ -754,9 +773,12 @@ def test_custom_headers_case_insensitive(self):
754773 )
755774
756775 response_headers = {"X-ReSPoNse-HeaDER" : "custom-value" }
757- url = "http://mock//capture_headers"
758- httpretty .register_uri (
759- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
776+ url = "http://mock/capture_headers"
777+ Entry .single_register (
778+ Entry .GET ,
779+ url ,
780+ body = "Hello!" ,
781+ headers = response_headers ,
760782 )
761783 self .perform_request (
762784 url ,
@@ -785,9 +807,12 @@ def test_standard_http_headers_captured(self):
785807 "Content-Type" : "text/plain" ,
786808 "Server" : "TestServer/1.0" ,
787809 }
788- url = "http://mock//capture_headers"
789- httpretty .register_uri (
790- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
810+ url = "http://mock/capture_headers"
811+ Entry .single_register (
812+ Entry .GET ,
813+ url ,
814+ body = "Hello!" ,
815+ headers = response_headers ,
791816 )
792817 self .perform_request (
793818 url ,
@@ -853,9 +878,12 @@ def test_capture_all_response_headers(self):
853878 "X-Response-Two" : "value2" ,
854879 "X-Response-Three" : "value3" ,
855880 }
856- url = "http://mock//capture_headers"
857- httpretty .register_uri (
858- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
881+ url = "http://mock/capture_headers"
882+ Entry .single_register (
883+ Entry .GET ,
884+ url ,
885+ body = "Hello!" ,
886+ headers = response_headers ,
859887 )
860888 self .perform_request (url )
861889
@@ -915,9 +943,12 @@ def test_capture_and_sanitize_environment_variables(self):
915943 "X-Response-One" : "value1" ,
916944 "X-Response-Two" : "value2" ,
917945 }
918- url = "http://mock//capture_headers"
919- httpretty .register_uri (
920- httpretty .GET , url , body = "Hello!" , adding_headers = response_headers
946+ url = "http://mock/capture_headers"
947+ Entry .single_register (
948+ Entry .GET ,
949+ url ,
950+ body = "Hello!" ,
951+ headers = response_headers ,
921952 )
922953 self .perform_request (
923954 url , headers = {"x-request-one" : "one" , "x-request-two" : "two" }
@@ -945,7 +976,7 @@ def test_urlopen_positional_headers(self):
945976 URLLib3Instrumentor ().uninstrument ()
946977 URLLib3Instrumentor ().instrument (captured_request_headers = ["X-Test" ])
947978 url = "http://mock/status/200"
948- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
979+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
949980 pool = urllib3 .HTTPConnectionPool ("mock" )
950981 headers = {"X-Test" : "Value" }
951982 response = pool .urlopen ("GET" , "/status/200" , None , headers )
@@ -959,7 +990,7 @@ def test_urlopen_all_positional(self):
959990 URLLib3Instrumentor ().uninstrument ()
960991 URLLib3Instrumentor ().instrument (captured_request_headers = ["X-Test" ])
961992 url = "http://mock/status/200"
962- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
993+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
963994 pool = urllib3 .HTTPConnectionPool ("mock" )
964995 response = pool .urlopen (
965996 "GET" ,
@@ -981,7 +1012,7 @@ def test_urlopen_mixed_args(self):
9811012 URLLib3Instrumentor ().uninstrument ()
9821013 URLLib3Instrumentor ().instrument (captured_request_headers = ["X-Test" ])
9831014 url = "http://mock/status/200"
984- httpretty . register_uri ( httpretty .GET , url , body = "Hello!" )
1015+ Entry . single_register ( Entry .GET , url , body = "Hello!" )
9851016 pool = urllib3 .HTTPConnectionPool ("mock" )
9861017 response = pool .urlopen (
9871018 "GET" , "/status/200" , headers = {"X-Test" : "Value" }, body = None
0 commit comments