Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / telemetry / telemetry / core / memory_cache_http_server_unittest.py
blob9a74a1bc1e85aeff8d6697886b05475cfed6ce1e
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 os
7 from telemetry.core import util
8 from telemetry.testing import tab_test_case
11 class MemoryCacheHTTPServerTest(tab_test_case.TabTestCase):
13 def setUp(self):
14 super(MemoryCacheHTTPServerTest, self).setUp()
15 self._test_filename = 'bear.webm'
16 _test_file = os.path.join(util.GetUnittestDataDir(), 'bear.webm')
17 self._test_file_size = os.stat(_test_file).st_size
19 def testBasicHostingAndRangeRequests(self):
20 self.Navigate('blank.html')
21 x = self._tab.EvaluateJavaScript('document.body.innerHTML')
22 x = x.strip()
24 # Test basic html hosting.
25 self.assertEquals(x, 'Hello world')
27 file_size = self._test_file_size
28 last_byte = file_size - 1
29 # Test byte range request: no end byte.
30 self.CheckContentHeaders('0-', '0-%d' % last_byte, file_size)
32 # Test byte range request: greater than zero start byte.
33 self.CheckContentHeaders('100-', '100-%d' % last_byte, file_size - 100)
35 # Test byte range request: explicit byte range.
36 self.CheckContentHeaders('2-500', '2-500', '499')
38 # Test byte range request: no start byte.
39 self.CheckContentHeaders('-228', '%d-%d' % (file_size - 228, last_byte),
40 '228')
42 # Test byte range request: end byte less than start byte.
43 self.CheckContentHeaders('100-5', '100-%d' % last_byte, file_size - 100)
45 def CheckContentHeaders(self, content_range_request, content_range_response,
46 content_length_response):
47 self._tab.ExecuteJavaScript("""
48 var loaded = false;
49 var xmlhttp = new XMLHttpRequest();
50 xmlhttp.onload = function(e) {
51 loaded = true;
53 // Avoid cached content by appending unique URL param.
54 xmlhttp.open('GET', {{ url }} + "?t=" + Date.now(), true);
55 xmlhttp.setRequestHeader('Range', {{ range }});
56 xmlhttp.send();
57 """,
58 url=self.UrlOfUnittestFile(self._test_filename),
59 range='bytes=%s' % content_range_request)
60 self._tab.WaitForJavaScriptCondition('loaded', timeout=5)
61 content_range = self._tab.EvaluateJavaScript(
62 'xmlhttp.getResponseHeader("Content-Range");')
63 content_range_response = 'bytes %s/%d' % (content_range_response,
64 self._test_file_size)
65 self.assertEquals(content_range, content_range_response)
66 content_length = self._tab.EvaluateJavaScript(
67 'xmlhttp.getResponseHeader("Content-Length");')
68 self.assertEquals(content_length, str(content_length_response))
70 def testAbsoluteAndRelativePathsYieldSameURL(self):
71 test_file_rel_path = 'green_rect.html'
72 test_file_abs_path = os.path.abspath(os.path.join(util.GetUnittestDataDir(),
73 test_file_rel_path))
74 # It's necessary to bypass self.UrlOfUnittestFile since that
75 # concatenates the unittest directory on to the incoming path,
76 # causing the same code path to be taken in both cases.
77 self._platform.SetHTTPServerDirectories(util.GetUnittestDataDir())
78 self.assertEquals(
79 self._platform.http_server.UrlOf(test_file_rel_path),
80 self._platform.http_server.UrlOf(test_file_abs_path))