1919from unittest import mock
2020
2121from trace_client import MAX_TAG_LENGTH , MAX_TAGS , TraceClient , __version__ , environment_opts_out
22+ from trace_client import trace_client as trace_client_module
2223
2324_ENV_VARS = ("TRACE_USAGE_REPORTING" , "DO_NOT_TRACK" )
2425
@@ -27,6 +28,7 @@ class _Capture:
2728 def __init__ (self ):
2829 self .requests = []
2930 self .reply_status = 201
31+ self .location = None # sent as a Location header when set
3032 self .arrived = threading .Event ()
3133 self .release = threading .Event ()
3234 self .release .set () # by default answer at once
@@ -39,17 +41,22 @@ def do_POST(self):
3941 body = self .rfile .read (length )
4042 capture .release .wait (10 )
4143 capture .requests .append ({
44+ "method" : self .command ,
4245 "path" : self .path ,
4346 "authorization" : self .headers .get ("Authorization" ),
4447 "content_type" : self .headers .get ("Content-Type" ),
4548 "user_agent" : self .headers .get ("User-Agent" ),
4649 "body" : body .decode ("utf-8" ),
4750 })
4851 self .send_response (capture .reply_status )
52+ if capture .location is not None :
53+ self .send_header ("Location" , capture .location )
4954 self .send_header ("Content-Length" , "0" )
5055 self .end_headers ()
5156 capture .arrived .set ()
5257
58+ do_GET = do_POST # a followed 301/302/303 arrives as a GET
59+
5360 def log_message (self , * args ): # keep test output quiet
5461 pass
5562
@@ -198,6 +205,25 @@ def test_a_success_status_other_than_201_is_logged_as_not_delivered(self):
198205 "only a 201 is success: %s" % [r .getMessage () for r in self .log ])
199206 self .assertTrue (all (r .levelno == logging .DEBUG for r in self .log ))
200207
208+ def test_a_redirect_is_logged_and_dropped_never_followed_with_the_key (self ):
209+ target = _Capture ()
210+ target_server = _server (target )
211+ self .addCleanup (target_server .shutdown )
212+ # Another port and another host name: what a redirect elsewhere looks like.
213+ self .capture .location = "http://localhost:%d/elsewhere" % target_server .server_address [1 ]
214+ for status in (301 , 302 , 303 , 307 , 308 ):
215+ self .log .clear ()
216+ self .capture .reply_status = status
217+ client = TraceClient (self .base_url , "MyGame" , "1.2.3" , key = "secret-key" )
218+ client .report ("startup" )
219+ client .close () # joins the sender, so its log line is written by now
220+ self .assertEqual ([], target .requests ,
221+ "a %d must not carry the key to the host it names" % status )
222+ self .assertTrue (any ("answered %d" % status in r .getMessage () for r in self .log ),
223+ "the log names the redirect that lost the event: %s"
224+ % [r .getMessage () for r in self .log ])
225+ self .assertTrue (all (r .levelno == logging .DEBUG for r in self .log ))
226+
201227 def test_an_error_answer_whose_body_cannot_be_read_still_logs_its_status (self ):
202228 class Unreadable (urllib .error .HTTPError ):
203229 def read (self , * args ):
@@ -206,7 +232,7 @@ def read(self, *args):
206232 answer = Unreadable (self .base_url + "/api/metrics" , 503 , "Service Unavailable" , {}, None )
207233 crashes = []
208234 with mock .patch .object (threading , "excepthook" , crashes .append ), \
209- mock .patch .object (urllib . request , "urlopen " , side_effect = answer ):
235+ mock .patch .object (trace_client_module . _OPENER , "open " , side_effect = answer ):
210236 client = TraceClient (self .base_url , "MyGame" , "1.2.3" , key = "k" )
211237 client .report ("startup" ) # must not raise
212238 client .close ()
0 commit comments