Chromecast: extracts Linux window creation code to a common place.
[chromium-blink-merge.git] / tools / chrome_proxy / integration_tests / chrome_proxy_metrics_unittest.py
blob37fdb5f9f3babec82c226bd275cb9cb06a5dff7d
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
3 # found in the LICENSE file.
5 import base64
6 import unittest
8 from integration_tests import chrome_proxy_metrics as metrics
9 from integration_tests import network_metrics_unittest as network_unittest
10 from metrics import test_page_test_results
13 # Timeline events used in tests.
14 # An HTML not via proxy.
15 EVENT_HTML_PROXY = network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
16 url='http://test.html1',
17 response_headers={
18 'Content-Type': 'text/html',
19 'Content-Length': str(len(network_unittest.HTML_BODY)),
21 body=network_unittest.HTML_BODY)
23 # An HTML via proxy with the deprecated Via header.
24 EVENT_HTML_PROXY_DEPRECATED_VIA = (
25 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
26 url='http://test.html2',
27 response_headers={
28 'Content-Type': 'text/html',
29 'Content-Encoding': 'gzip',
30 'X-Original-Content-Length': str(len(network_unittest.HTML_BODY)),
31 'Via': (metrics.CHROME_PROXY_VIA_HEADER_DEPRECATED +
32 ',other-via'),
34 body=network_unittest.HTML_BODY))
36 # An image via proxy with Via header and it is cached.
37 EVENT_IMAGE_PROXY_CACHED = (
38 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
39 url='http://test.image',
40 response_headers={
41 'Content-Type': 'image/jpeg',
42 'Content-Encoding': 'gzip',
43 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
44 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
46 body=base64.b64encode(network_unittest.IMAGE_BODY),
47 base64_encoded_body=True,
48 served_from_cache=True))
50 # An image fetched directly.
51 EVENT_IMAGE_DIRECT = (
52 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
53 url='http://test.image',
54 response_headers={
55 'Content-Type': 'image/jpeg',
56 'Content-Encoding': 'gzip',
58 body=base64.b64encode(network_unittest.IMAGE_BODY),
59 base64_encoded_body=True))
61 # A safe-browsing malware response.
62 EVENT_MALWARE_PROXY = (
63 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
64 url='http://test.malware',
65 response_headers={
66 'X-Malware-Url': '1',
67 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
68 'Location': 'http://test.malware',
70 status=307))
72 # An HTML via proxy with the deprecated Via header.
73 EVENT_IMAGE_BYPASS = (
74 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
75 url='http://test.image',
76 response_headers={
77 'Chrome-Proxy': 'bypass=1',
78 'Content-Type': 'text/html',
79 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
81 status=502))
83 # An image fetched directly.
84 EVENT_IMAGE_DIRECT = (
85 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
86 url='http://test.image',
87 response_headers={
88 'Content-Type': 'image/jpeg',
89 'Content-Encoding': 'gzip',
91 body=base64.b64encode(network_unittest.IMAGE_BODY),
92 base64_encoded_body=True))
95 class ChromeProxyMetricTest(unittest.TestCase):
97 _test_proxy_info = {}
99 def _StubGetProxyInfo(self, info):
100 def stub(unused_tab, unused_url=''): # pylint: disable=W0613
101 return ChromeProxyMetricTest._test_proxy_info
102 metrics.GetProxyInfoFromNetworkInternals = stub
103 ChromeProxyMetricTest._test_proxy_info = info
105 def testChromeProxyResponse(self):
106 # An https non-proxy response.
107 resp = metrics.ChromeProxyResponse(
108 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
109 url='https://test.url',
110 response_headers={
111 'Content-Type': 'text/html',
112 'Content-Length': str(len(network_unittest.HTML_BODY)),
113 'Via': 'some other via',
115 body=network_unittest.HTML_BODY))
116 self.assertFalse(resp.ShouldHaveChromeProxyViaHeader())
117 self.assertFalse(resp.HasChromeProxyViaHeader())
118 self.assertTrue(resp.IsValidByViaHeader())
120 # A proxied JPEG image response
121 resp = metrics.ChromeProxyResponse(
122 network_unittest.NetworkMetricTest.MakeNetworkTimelineEvent(
123 url='http://test.image',
124 response_headers={
125 'Content-Type': 'image/jpeg',
126 'Content-Encoding': 'gzip',
127 'Via': '1.1 ' + metrics.CHROME_PROXY_VIA_HEADER,
128 'X-Original-Content-Length': str(network_unittest.IMAGE_OCL),
130 body=base64.b64encode(network_unittest.IMAGE_BODY),
131 base64_encoded_body=True))
132 self.assertTrue(resp.ShouldHaveChromeProxyViaHeader())
133 self.assertTrue(resp.HasChromeProxyViaHeader())
134 self.assertTrue(resp.IsValidByViaHeader())
136 def testChromeProxyMetricForDataSaving(self):
137 metric = metrics.ChromeProxyMetric()
138 events = [
139 EVENT_HTML_PROXY,
140 EVENT_HTML_PROXY_DEPRECATED_VIA,
141 EVENT_IMAGE_PROXY_CACHED,
142 EVENT_IMAGE_DIRECT]
143 metric.SetEvents(events)
145 self.assertTrue(len(events), len(list(metric.IterResponses(None))))
146 results = test_page_test_results.TestPageTestResults(self)
148 metric.AddResultsForDataSaving(None, results)
149 results.AssertHasPageSpecificScalarValue('resources_via_proxy', 'count', 2)
150 results.AssertHasPageSpecificScalarValue('resources_from_cache', 'count', 1)
151 results.AssertHasPageSpecificScalarValue('resources_direct', 'count', 2)
153 def testChromeProxyMetricForHeaderValidation(self):
154 metric = metrics.ChromeProxyMetric()
155 metric.SetEvents([
156 EVENT_HTML_PROXY,
157 EVENT_HTML_PROXY_DEPRECATED_VIA,
158 EVENT_IMAGE_PROXY_CACHED,
159 EVENT_IMAGE_DIRECT])
161 results = test_page_test_results.TestPageTestResults(self)
163 missing_via_exception = False
164 try:
165 metric.AddResultsForHeaderValidation(None, results)
166 except metrics.ChromeProxyMetricException:
167 missing_via_exception = True
168 # Only the HTTP image response does not have a valid Via header.
169 self.assertTrue(missing_via_exception)
171 # Two events with valid Via headers.
172 metric.SetEvents([
173 EVENT_HTML_PROXY_DEPRECATED_VIA,
174 EVENT_IMAGE_PROXY_CACHED])
175 metric.AddResultsForHeaderValidation(None, results)
176 results.AssertHasPageSpecificScalarValue('checked_via_header', 'count', 2)
178 def testChromeProxyMetricForBypass(self):
179 metric = metrics.ChromeProxyMetric()
180 metric.SetEvents([
181 EVENT_HTML_PROXY,
182 EVENT_HTML_PROXY_DEPRECATED_VIA,
183 EVENT_IMAGE_PROXY_CACHED,
184 EVENT_IMAGE_DIRECT])
185 results = test_page_test_results.TestPageTestResults(self)
187 bypass_exception = False
188 try:
189 metric.AddResultsForBypass(None, results)
190 except metrics.ChromeProxyMetricException:
191 bypass_exception = True
192 # Two of the first three events have Via headers.
193 self.assertTrue(bypass_exception)
195 # Use directly fetched image only. It is treated as bypassed.
196 metric.SetEvents([EVENT_IMAGE_DIRECT])
197 metric.AddResultsForBypass(None, results)
198 results.AssertHasPageSpecificScalarValue('bypass', 'count', 1)
200 def testChromeProxyMetricForCorsBypass(self):
201 metric = metrics.ChromeProxyMetric()
202 metric.SetEvents([EVENT_HTML_PROXY_DEPRECATED_VIA,
203 EVENT_IMAGE_BYPASS,
204 EVENT_IMAGE_DIRECT])
205 results = test_page_test_results.TestPageTestResults(self)
206 metric.AddResultsForCorsBypass(None, results)
207 results.AssertHasPageSpecificScalarValue('cors_bypass', 'count', 1)
210 def testChromeProxyMetricForHTTPFallback(self):
211 metric = metrics.ChromeProxyMetric()
212 metric.SetEvents([
213 EVENT_HTML_PROXY,
214 EVENT_HTML_PROXY_DEPRECATED_VIA])
215 results = test_page_test_results.TestPageTestResults(self)
217 fallback_exception = False
218 info = {}
219 info['enabled'] = False
220 self._StubGetProxyInfo(info)
221 try:
222 metric.AddResultsForBypass(None, results)
223 except metrics.ChromeProxyMetricException:
224 fallback_exception = True
225 self.assertTrue(fallback_exception)
227 fallback_exception = False
228 info['enabled'] = True
229 info['proxies'] = [
230 'something.else.com:80',
231 metrics.PROXY_SETTING_DIRECT
233 self._StubGetProxyInfo(info)
234 try:
235 metric.AddResultsForBypass(None, results)
236 except metrics.ChromeProxyMetricException:
237 fallback_exception = True
238 self.assertTrue(fallback_exception)
240 info['enabled'] = True
241 info['proxies'] = [
242 metrics.PROXY_SETTING_HTTP,
243 metrics.PROXY_SETTING_DIRECT
245 self._StubGetProxyInfo(info)
246 metric.AddResultsForHTTPFallback(None, results)
248 def testChromeProxyMetricForSafebrowsing(self):
249 metric = metrics.ChromeProxyMetric()
250 metric.SetEvents([EVENT_MALWARE_PROXY])
251 results = test_page_test_results.TestPageTestResults(self)
253 metric.AddResultsForSafebrowsing(None, results)
254 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)
256 # Clear results and metrics to test no response for safebrowsing
257 results = test_page_test_results.TestPageTestResults(self)
258 metric.SetEvents([])
259 metric.AddResultsForSafebrowsing(None, results)
260 results.AssertHasPageSpecificScalarValue('safebrowsing', 'boolean', True)