@@ -801,34 +801,70 @@ def test_base_response_get_response():
801
801
resp .get_response (requests .PreparedRequest ())
802
802
803
803
804
- def test_custom_adapter ():
805
- @responses .activate
806
- def run ():
807
- url = "http://example.com"
808
- responses .add (responses .GET , url , body = b"test" )
804
+ class TestAdapters :
805
+ class CustomAdapter (requests .adapters .HTTPAdapter ):
806
+ """Classic custom adapter."""
809
807
810
- calls = [0 ]
808
+ def send (self , * a , ** k ):
809
+ return super ().send (* a , ** k )
811
810
812
- class DummyAdapter (requests .adapters .HTTPAdapter ):
813
- def send ( self , * a , ** k ):
814
- calls [ 0 ] += 1
815
- return super (). send ( * a , ** k )
811
+ class PositionalArgsAdapter (requests .adapters .HTTPAdapter ):
812
+ """Custom adapter that sends only positional args.
813
+ See https://github.com/getsentry/responses/issues/642 for more into.
814
+ """
816
815
817
- # Test that the adapter is actually used
818
- session = requests .Session ()
819
- session .mount ("http://" , DummyAdapter ())
816
+ def send (
817
+ self ,
818
+ request ,
819
+ stream = False ,
820
+ timeout = None ,
821
+ verify = True ,
822
+ cert = None ,
823
+ proxies = None ,
824
+ ):
825
+ return super ().send (request , stream , timeout , verify , cert , proxies )
826
+
827
+ class PositionalArgsIncompleteAdapter (requests .adapters .HTTPAdapter ):
828
+ """Custom adapter that sends only positional args.
829
+ Not all arguments are forwarded to the send method.
830
+ See https://github.com/getsentry/responses/issues/642 for more into.
831
+ """
820
832
821
- session .get (url , allow_redirects = False )
822
- assert calls [0 ] == 1
833
+ def send (
834
+ self ,
835
+ request ,
836
+ stream = False ,
837
+ timeout = None ,
838
+ verify = True ,
839
+ # following args are intentionally not forwarded
840
+ cert = None ,
841
+ proxies = None ,
842
+ ):
843
+ return super ().send (request , stream , timeout , verify )
844
+
845
+ @pytest .mark .parametrize (
846
+ "adapter_class" ,
847
+ (CustomAdapter , PositionalArgsAdapter , PositionalArgsIncompleteAdapter ),
848
+ )
849
+ def test_custom_adapter (self , adapter_class ):
850
+ """Test basic adapter implementation and that responses can patch them properly."""
823
851
824
- # Test that the response is still correctly emulated
825
- session = requests .Session ()
826
- session .mount ("http://" , DummyAdapter ())
852
+ @responses .activate
853
+ def run ():
854
+ url = "http://example.com"
855
+ responses .add (responses .GET , url , body = b"test adapter" )
827
856
828
- resp = session .get (url )
829
- assert_response (resp , "test" )
857
+ # Test that the adapter is actually used
858
+ session = requests .Session ()
859
+ adapter = adapter_class ()
860
+ session .mount ("http://" , adapter )
861
+ with patch .object (adapter , "send" , side_effect = adapter .send ) as mock_send :
862
+ resp = session .get (url , allow_redirects = False )
830
863
831
- run ()
864
+ assert mock_send .call_count == 1
865
+ assert_response (resp , "test adapter" )
866
+
867
+ run ()
832
868
833
869
834
870
def test_responses_as_context_manager ():
0 commit comments