Chromium Code Reviews| Index: tools/perf/metrics/chrome_proxy_unittest.py |
| diff --git a/tools/perf/metrics/chrome_proxy_unittest.py b/tools/perf/metrics/chrome_proxy_unittest.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..096b5e7c1b0dfe65170e71dfbd1b88179e3e01a6 |
| --- /dev/null |
| +++ b/tools/perf/metrics/chrome_proxy_unittest.py |
| @@ -0,0 +1,161 @@ |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import base64 |
| +import unittest |
| + |
| +from metrics import chrome_proxy |
| +from metrics import network_unittest |
| +from metrics import test_page_measurement_results |
| + |
| + |
| +_EVENTS = [ |
| + # An HTML not via proxy. |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='http://test.html1', |
| + response_headers={ |
| + 'Content-Type': 'text/html', |
| + 'Content-Length': str(len(network_unittest.HTML_BODY)), |
| + }, |
| + body=network_unittest.HTML_BODY), |
| + |
| + # An HTML via proxy with old Via header. |
| + 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': chrome_proxy.CHROME_PROXY_VIA_HEADER_OLD + ',other-via', |
| + }, |
| + body=network_unittest.HTML_BODY), |
| + |
| + # An image via proxy with Via header and it is cached. |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='http://test.image', |
| + response_headers={ |
| + 'Content-Type': 'image/jpeg', |
| + 'Content-Encoding': 'gzip', |
| + 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), |
| + 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| + }, |
| + body=base64.b64encode(network_unittest.IMAGE_BODY), |
| + base64_encoded_body=True, |
| + served_from_cache=True), |
| + |
| + # An image fetched directly. |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='http://test.image', |
| + response_headers={ |
| + 'Content-Type': 'image/jpeg', |
| + 'Content-Encoding': 'gzip', |
| + }, |
| + body=base64.b64encode(network_unittest.IMAGE_BODY), |
| + base64_encoded_body=True), |
| + |
| + # A safe-browsing malware response. |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='http://test.malware', |
| + response_headers={ |
| + 'X-Malware-Url': '1', |
| + 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| + 'Location': 'http://test.malware', |
| + }, |
| + status=307), |
| + ] |
| + |
| + |
| +class ChromeProxyMetricTest(unittest.TestCase): |
| + def testChromeProxyResponse(self): |
| + # An https non-proxy response. |
| + resp = chrome_proxy.ChromeProxyResponse( |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='https://test.url', |
| + response_headers={ |
| + 'Content-Type': 'text/html', |
| + 'Content-Length': str(len(network_unittest.HTML_BODY)), |
| + 'Via': 'some other via', |
| + }, |
| + body=network_unittest.HTML_BODY)) |
| + self.assertFalse(resp.ShouldHaveChromeProxyViaHeader()) |
| + self.assertFalse(resp.HasChromeProxyViaHeader()) |
| + self.assertTrue(resp.IsValidByViaHeader()) |
| + |
| + # A proxied JPEG image response |
| + resp = chrome_proxy.ChromeProxyResponse( |
| + network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent( |
| + url='http://test.image', |
| + response_headers={ |
| + 'Content-Type': 'image/jpeg', |
| + 'Content-Encoding': 'gzip', |
| + 'Via': '1.1 ' + chrome_proxy.CHROME_PROXY_VIA_HEADER, |
| + 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL), |
| + }, |
| + body=base64.b64encode(network_unittest.IMAGE_BODY), |
| + base64_encoded_body=True)) |
| + self.assertTrue(resp.ShouldHaveChromeProxyViaHeader()) |
| + self.assertTrue(resp.HasChromeProxyViaHeader()) |
| + self.assertTrue(resp.IsValidByViaHeader()) |
| + |
| + def testChromePrxoyMetricForDataSaving(self): |
|
bengr
2014/03/26 22:45:55
Proxy
bolian
2014/03/27 00:43:32
Done.
|
| + metric = chrome_proxy.ChromeProxyMetric() |
| + metric._events = _EVENTS[:4] |
| + |
| + self.assertTrue(len(_EVENTS[:4]), len(list(metric.IterResponses(None)))) |
| + results = test_page_measurement_results.TestPageMeasurementResults(self) |
| + |
| + metric.AddResultsForDataSaving(None, results) |
| + results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2) |
| + results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1) |
| + results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2) |
| + |
| + def testChromePrxoyMetricForHeaderValidation(self): |
|
bengr
2014/03/26 22:45:55
Proxy
bolian
2014/03/27 00:43:32
Done.
|
| + metric = chrome_proxy.ChromeProxyMetric() |
| + metric._events = _EVENTS[:4] |
| + results = test_page_measurement_results.TestPageMeasurementResults(self) |
| + |
| + missing_via_exception = False |
| + try: |
| + metric.AddResultsForHeaderValidation(None, results) |
| + except chrome_proxy.ChromeProxyMetricException: |
| + missing_via_exception = True |
| + # Only the HTTP image response does not have a valid Via header. |
| + self.assertTrue(missing_via_exception) |
| + |
| + # Two events with valid Via headers. |
| + metric._events = _EVENTS[1:3] |
| + metric.AddResultsForHeaderValidation(None, results) |
| + results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2) |
| + |
| + def testChromePrxoyMetricForBypass(self): |
|
bengr
2014/03/26 22:45:55
Proxy
bolian
2014/03/27 00:43:32
Done.
|
| + metric = chrome_proxy.ChromeProxyMetric() |
| + metric._events = _EVENTS[:4] |
| + results = test_page_measurement_results.TestPageMeasurementResults(self) |
| + |
| + bypass_exception = False |
| + try: |
| + metric.AddResultsForBypass(None, results) |
| + except chrome_proxy.ChromeProxyMetricException: |
| + bypass_exception = True |
| + # Two of the first three events have Via headers. |
| + self.assertTrue(bypass_exception) |
| + |
| + # Use directly fetched image only. It is treated as bypassed. |
| + metric._events = _EVENTS[3:4] |
| + metric.AddResultsForBypass(None, results) |
| + results.AssertHasPageSpecificScalarValue('bypass', 'count', 1) |
| + |
| + def testChromePrxoyMetricForSafebrowsing(self): |
|
bengr
2014/03/26 22:45:55
Proxy
bolian
2014/03/27 00:43:32
Done.
|
| + metric = chrome_proxy.ChromeProxyMetric() |
| + metric._events = _EVENTS[4:] |
| + results = test_page_measurement_results.TestPageMeasurementResults(self) |
| + |
| + metric.AddResultsForSafebrowsing(None, results) |
| + results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) |
| + |
| + # Clear results and metrics to test no response for safebrowsing |
| + results = test_page_measurement_results.TestPageMeasurementResults(self) |
| + metric._events = [] |
| + metric.AddResultsForSafebrowsing(None, results) |
| + results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True) |