Commit d9ae5cad authored by bengr's avatar bengr Committed by Commit bot

Fix unittests for Data Saver integration tests

The unittests were failing due to various bugs.
These have been fixed.

BUG=456240

Review URL: https://codereview.chromium.org/905823003

Cr-Commit-Position: refs/heads/master@{#315124}
parent b9a6b787
...@@ -15,7 +15,6 @@ class ChromeProxyMetricException(page_test.MeasurementFailure): ...@@ -15,7 +15,6 @@ class ChromeProxyMetricException(page_test.MeasurementFailure):
CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy' CHROME_PROXY_VIA_HEADER = 'Chrome-Compression-Proxy'
CHROME_PROXY_VIA_HEADER_DEPRECATED = '1.1 Chrome Compression Proxy'
class ChromeProxyResponse(network_metrics.HTTPResponse): class ChromeProxyResponse(network_metrics.HTTPResponse):
...@@ -44,11 +43,9 @@ class ChromeProxyResponse(network_metrics.HTTPResponse): ...@@ -44,11 +43,9 @@ class ChromeProxyResponse(network_metrics.HTTPResponse):
if not via_header: if not via_header:
return False return False
vias = [v.strip(' ') for v in via_header.split(',')] vias = [v.strip(' ') for v in via_header.split(',')]
# The Via header is valid if it is the old format or the new format # The Via header is valid if it has a 4-character version prefix followed by
# with 4-character version prefix, for example, # the proxy name, for example, "1.1 Chrome-Compression-Proxy".
# "1.1 Chrome-Compression-Proxy". return any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias)
return (CHROME_PROXY_VIA_HEADER_DEPRECATED in vias or
any(v[4:] == CHROME_PROXY_VIA_HEADER for v in vias))
def IsValidByViaHeader(self): def IsValidByViaHeader(self):
return (not self.ShouldHaveChromeProxyViaHeader() or return (not self.ShouldHaveChromeProxyViaHeader() or
...@@ -323,7 +320,7 @@ class ChromeProxyMetric(network_metrics.NetworkMetric): ...@@ -323,7 +320,7 @@ class ChromeProxyMetric(network_metrics.NetworkMetric):
raise ChromeProxyMetricException, ( raise ChromeProxyMetricException, (
'Response for %s should have come through the fallback proxy.\n' 'Response for %s should have come through the fallback proxy.\n'
'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % ( 'Response: remote_port=%s status=(%d, %s)\nHeaders:\n %s' % (
r.url, str(fallback_resp.remote_port), r.status, r.status_text, r.url, str(resp.remote_port), r.status, r.status_text,
r.headers)) r.headers))
else: else:
via_fallback_count += 1 via_fallback_count += 1
......
...@@ -46,19 +46,6 @@ EVENT_HTML_PROXY_VIA_HTTP_FALLBACK = ( ...@@ -46,19 +46,6 @@ EVENT_HTML_PROXY_VIA_HTTP_FALLBACK = (
body=network_unittest.HTML_BODY, body=network_unittest.HTML_BODY,
remote_port=80)) remote_port=80))
# An HTML via proxy with the deprecated Via header.
EVENT_HTML_PROXY_DEPRECATED_VIA = (
network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
url='http://test.html2',
response_headers={
'Content-Type': 'text/html',
'Content-Encoding': 'gzip',
'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)),
'Via': (metrics.CHROME_PROXY_VIA_HEADER_DEPRECATED +
',other-via'),
},
body=network_unittest.HTML_BODY))
# An image via proxy with Via header. # An image via proxy with Via header.
EVENT_IMAGE_PROXY_VIA = ( EVENT_IMAGE_PROXY_VIA = (
network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
...@@ -123,7 +110,7 @@ EVENT_MALWARE_PROXY = ( ...@@ -123,7 +110,7 @@ EVENT_MALWARE_PROXY = (
}, },
status=307)) status=307))
# An HTML via proxy with the deprecated Via header. # An HTML via proxy with the Via header.
EVENT_IMAGE_BYPASS = ( EVENT_IMAGE_BYPASS = (
network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
url='http://test.image', url='http://test.image',
...@@ -191,7 +178,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -191,7 +178,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
events = [ events = [
EVENT_HTML_DIRECT, EVENT_HTML_DIRECT,
EVENT_HTML_PROXY_DEPRECATED_VIA, EVENT_HTML_PROXY_VIA,
EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_PROXY_CACHED,
EVENT_IMAGE_DIRECT] EVENT_IMAGE_DIRECT]
metric.SetEvents(events) metric.SetEvents(events)
...@@ -208,7 +195,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -208,7 +195,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
metric.SetEvents([ metric.SetEvents([
EVENT_HTML_DIRECT, EVENT_HTML_DIRECT,
EVENT_HTML_PROXY_DEPRECATED_VIA, EVENT_HTML_PROXY_VIA,
EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_PROXY_CACHED,
EVENT_IMAGE_DIRECT]) EVENT_IMAGE_DIRECT])
...@@ -224,7 +211,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -224,7 +211,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
# Two events with valid Via headers. # Two events with valid Via headers.
metric.SetEvents([ metric.SetEvents([
EVENT_HTML_PROXY_DEPRECATED_VIA, EVENT_HTML_PROXY_VIA,
EVENT_IMAGE_PROXY_CACHED]) EVENT_IMAGE_PROXY_CACHED])
metric.AddResultsForHeaderValidation(None, results) metric.AddResultsForHeaderValidation(None, results)
results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2)
...@@ -233,7 +220,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -233,7 +220,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
metric.SetEvents([ metric.SetEvents([
EVENT_HTML_DIRECT, EVENT_HTML_DIRECT,
EVENT_HTML_PROXY_DEPRECATED_VIA, EVENT_HTML_PROXY_VIA,
EVENT_IMAGE_PROXY_CACHED, EVENT_IMAGE_PROXY_CACHED,
EVENT_IMAGE_DIRECT]) EVENT_IMAGE_DIRECT])
results = test_page_test_results.TestPageTestResults(self) results = test_page_test_results.TestPageTestResults(self)
...@@ -253,7 +240,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -253,7 +240,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
def testChromeProxyMetricForCorsBypass(self): def testChromeProxyMetricForCorsBypass(self):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
metric.SetEvents([EVENT_HTML_PROXY_DEPRECATED_VIA, metric.SetEvents([EVENT_HTML_PROXY_VIA,
EVENT_IMAGE_BYPASS, EVENT_IMAGE_BYPASS,
EVENT_IMAGE_DIRECT]) EVENT_IMAGE_DIRECT])
results = test_page_test_results.TestPageTestResults(self) results = test_page_test_results.TestPageTestResults(self)
...@@ -279,19 +266,21 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -279,19 +266,21 @@ class ChromeProxyMetricTest(unittest.TestCase):
# The second response was over direct, but was expected via proxy. # The second response was over direct, but was expected via proxy.
self.assertTrue(exception_occurred) self.assertTrue(exception_occurred)
def testChromeProxyMetricForSafebrowsing(self): def testChromeProxyMetricForSafebrowsingOn(self):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
metric.SetEvents([EVENT_MALWARE_PROXY]) metric.SetEvents([EVENT_MALWARE_PROXY])
results = test_page_test_results.TestPageTestResults(self) results = test_page_test_results.TestPageTestResults(self)
metric.AddResultsForSafebrowsing(None, results) metric.AddResultsForSafebrowsingOn(None, results)
results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) results.AssertHasPageSpecificScalarValue(
'safebrowsing', 'timeout responses', 1)
# Clear results and metrics to test no response for safebrowsing # Clear results and metrics to test no response for safebrowsing
results = test_page_test_results.TestPageTestResults(self) results = test_page_test_results.TestPageTestResults(self)
metric.SetEvents([]) metric.SetEvents([])
metric.AddResultsForSafebrowsing(None, results) metric.AddResultsForSafebrowsingOn(None, results)
results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) results.AssertHasPageSpecificScalarValue(
'safebrowsing', 'timeout responses', 1)
def testChromeProxyMetricForHTTPFallback(self): def testChromeProxyMetricForHTTPFallback(self):
metric = metrics.ChromeProxyMetric() metric = metrics.ChromeProxyMetric()
...@@ -318,7 +307,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -318,7 +307,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
EVENT_HTML_DIRECT, EVENT_HTML_DIRECT,
EVENT_IMAGE_DIRECT]) EVENT_IMAGE_DIRECT])
results = test_page_test_results.TestPageTestResults(self) results = test_page_test_results.TestPageTestResults(self)
metric.AddResultsForHTTPToDirectFallback(None, results) metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2')
results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1) results.AssertHasPageSpecificScalarValue('via_fallback', 'count', 1)
results.AssertHasPageSpecificScalarValue('bypass', 'count', 2) results.AssertHasPageSpecificScalarValue('bypass', 'count', 2)
...@@ -326,7 +315,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -326,7 +315,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
EVENT_HTML_DIRECT]) EVENT_HTML_DIRECT])
exception_occurred = False exception_occurred = False
try: try:
metric.AddResultsForHTTPToDirectFallback(None, results) metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2')
except metrics.ChromeProxyMetricException: except metrics.ChromeProxyMetricException:
exception_occurred = True exception_occurred = True
# The first response was expected through the HTTP fallback proxy. # The first response was expected through the HTTP fallback proxy.
...@@ -337,7 +326,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -337,7 +326,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK]) EVENT_IMAGE_PROXY_VIA_HTTP_FALLBACK])
exception_occurred = False exception_occurred = False
try: try:
metric.AddResultsForHTTPToDirectFallback(None, results) metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2')
except metrics.ChromeProxyMetricException: except metrics.ChromeProxyMetricException:
exception_occurred = True exception_occurred = True
# All but the first response were expected to be over direct. # All but the first response were expected to be over direct.
...@@ -348,7 +337,7 @@ class ChromeProxyMetricTest(unittest.TestCase): ...@@ -348,7 +337,7 @@ class ChromeProxyMetricTest(unittest.TestCase):
EVENT_IMAGE_DIRECT]) EVENT_IMAGE_DIRECT])
exception_occurred = False exception_occurred = False
try: try:
metric.AddResultsForHTTPToDirectFallback(None, results) metric.AddResultsForHTTPToDirectFallback(None, results, 'test.html2')
except metrics.ChromeProxyMetricException: except metrics.ChromeProxyMetricException:
exception_occurred = True exception_occurred = True
# The first response was expected through the HTTP fallback proxy. # The first response was expected through the HTTP fallback proxy.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment