Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Side by Side Diff: tools/chrome_proxy/integration_tests/chrome_proxy_metrics.py

Issue 905823003: Fix unittests for Data Saver integration tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import logging 5 import logging
6 import time 6 import time
7 7
8 from integration_tests import network_metrics 8 from integration_tests import network_metrics
9 from telemetry.page import page_test 9 from telemetry.page import page_test
10 from telemetry.value import scalar 10 from telemetry.value import scalar
11 11
12 12
13 class ChromeProxyMetricException(page_test.MeasurementFailure): 13 class ChromeProxyMetricException(page_test.MeasurementFailure):
14 pass 14 pass
15 15
16 16
17 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' 17 CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy'
18 CHROME_PROXY_VIA_HEADER_DEPRECATED = '1.1 Chrome Compression Proxy'
19 18
20 19
21 class ChromeProxyResponse(network_metrics.HTTPResponse): 20 class ChromeProxyResponse(network_metrics.HTTPResponse):
22 """ Represents an HTTP response from a timeleine event.""" 21 """ Represents an HTTP response from a timeleine event."""
23 def __init__(self, event): 22 def __init__(self, event):
24 super(ChromeProxyResponse, self).__init__(event) 23 super(ChromeProxyResponse, self).__init__(event)
25 24
26 def ShouldHaveChromeProxyViaHeader(self): 25 def ShouldHaveChromeProxyViaHeader(self):
27 resp = self.response 26 resp = self.response
28 # Ignore https and data url 27 # Ignore https and data url
29 if resp.url.startswith('https') or resp.url.startswith('data:'): 28 if resp.url.startswith('https') or resp.url.startswith('data:'):
30 return False 29 return False
31 # Ignore 304 Not Modified and cache hit. 30 # Ignore 304 Not Modified and cache hit.
32 if resp.status == 304 or resp.served_from_cache: 31 if resp.status == 304 or resp.served_from_cache:
33 return False 32 return False
34 # Ignore invalid responses that don't have any header. Log a warning. 33 # Ignore invalid responses that don't have any header. Log a warning.
35 if not resp.headers: 34 if not resp.headers:
36 logging.warning('response for %s does not any have header ' 35 logging.warning('response for %s does not any have header '
37 '(refer=%s, status=%s)', 36 '(refer=%s, status=%s)',
38 resp.url, resp.GetHeader('Referer'), resp.status) 37 resp.url, resp.GetHeader('Referer'), resp.status)
39 return False 38 return False
40 return True 39 return True
41 40
42 def HasChromeProxyViaHeader(self): 41 def HasChromeProxyViaHeader(self):
43 via_header = self.response.GetHeader('Via') 42 via_header = self.response.GetHeader('Via')
44 if not via_header: 43 if not via_header:
45 return False 44 return False
46 vias = [v.strip(' ') for v in via_header.split(',')] 45 vias = [v.strip(' ') for v in via_header.split(',')]
47 # The Via header is valid if it is the old format or the new format 46 # The Via header is valid if it is the old format or the new format
sclittle 2015/02/06 20:44:38 Update comment - there is no "old format" now
bengr 2015/02/06 20:55:14 Done.
48 # with 4-character version prefix, for example, 47 # with 4-character version prefix, for example,
49 # "1.1 Chrome-Compression-Proxy". 48 # "1.1 Chrome-Compression-Proxy".
50 return (CHROME_PROXY_VIA_HEADER_DEPRECATED in vias or 49 return (any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias))
sclittle 2015/02/06 20:44:38 nit: I think you can remove the outermost parenthe
bengr 2015/02/06 20:55:14 Done.
51 any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias))
52 50
53 def IsValidByViaHeader(self): 51 def IsValidByViaHeader(self):
54 return (not self.ShouldHaveChromeProxyViaHeader() or 52 return (not self.ShouldHaveChromeProxyViaHeader() or
55 self.HasChromeProxyViaHeader()) 53 self.HasChromeProxyViaHeader())
56 54
57 def GetChromeProxyClientType(self): 55 def GetChromeProxyClientType(self):
58 """Get the client type directive from the Chrome-Proxy request header. 56 """Get the client type directive from the Chrome-Proxy request header.
59 57
60 Returns: 58 Returns:
61 The client type directive from the Chrome-Proxy request header for the 59 The client type directive from the Chrome-Proxy request header for the
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 # The first response(s) coming from fallback_response_host should be 314 # The first response(s) coming from fallback_response_host should be
317 # through the HTTP fallback proxy. 315 # through the HTTP fallback proxy.
318 resp = next(responses, None) 316 resp = next(responses, None)
319 while resp and fallback_response_host in resp.response.url: 317 while resp and fallback_response_host in resp.response.url:
320 if fallback_response_host in resp.response.url: 318 if fallback_response_host in resp.response.url:
321 if (not resp.HasChromeProxyViaHeader() or resp.remote_port != 80): 319 if (not resp.HasChromeProxyViaHeader() or resp.remote_port != 80):
322 r = resp.response 320 r = resp.response
323 raise ChromeProxyMetricException, ( 321 raise ChromeProxyMetricException, (
324 'Response for %s should have come through the fallback proxy.\n' 322 'Response for %s should have come through the fallback proxy.\n'
325 'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % ( 323 'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % (
326 r.url, str(fallback_resp.remote_port), r.status, r.status_text, 324 r.url, str(resp.remote_port), r.status, r.status_text,
327 r.headers)) 325 r.headers))
328 else: 326 else:
329 via_fallback_count += 1 327 via_fallback_count += 1
330 resp = next(responses, None) 328 resp = next(responses, None)
331 329
332 # All other responses should be bypassed. 330 # All other responses should be bypassed.
333 while resp: 331 while resp:
334 if resp.HasChromeProxyViaHeader(): 332 if resp.HasChromeProxyViaHeader():
335 r = resp.response 333 r = resp.response
336 raise ChromeProxyMetricException, ( 334 raise ChromeProxyMetricException, (
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 'Response for %s should have via header; proxy should no longer ' 414 'Response for %s should have via header; proxy should no longer '
417 'be bypassed.\nReponse: status=(%d, %s)\nHeaders:\n %s' % ( 415 'be bypassed.\nReponse: status=(%d, %s)\nHeaders:\n %s' % (
418 r.url, r.status, r.status_text, r.headers)) 416 r.url, r.status, r.status_text, r.headers))
419 else: 417 else:
420 via_count += 1 418 via_count += 1
421 419
422 results.AddValue(scalar.ScalarValue( 420 results.AddValue(scalar.ScalarValue(
423 results.current_page, 'bypass', 'count', bypass_count)) 421 results.current_page, 'bypass', 'count', bypass_count))
424 results.AddValue(scalar.ScalarValue( 422 results.AddValue(scalar.ScalarValue(
425 results.current_page, 'via', 'count', via_count)) 423 results.current_page, 'via', 'count', via_count))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698