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. 4 5 import os 6 7 from telemetry.core import util 8 from telemetry.testing import tab_test_case 9 10 11 class MemoryCacheHTTPServerTest(tab_test_case.TabTestCase): 12 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 18 19 def testBasicHostingAndRangeRequests(self): 20 self.Navigate('blank.html') 21 x = self._tab.EvaluateJavaScript('document.body.innerHTML') 22 x = x.strip() 23 24 # Test basic html hosting. 25 self.assertEquals(x, 'Hello world') 26 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) 31 32 # Test byte range request: greater than zero start byte. 33 self.CheckContentHeaders('100-', '100-%d' % last_byte, file_size - 100) 34 35 # Test byte range request: explicit byte range. 36 self.CheckContentHeaders('2-500', '2-500', '499') 37 38 # Test byte range request: no start byte. 39 self.CheckContentHeaders('-228', '%d-%d' % (file_size - 228, last_byte), 40 '228') 41 42 # Test byte range request: end byte less than start byte. 43 self.CheckContentHeaders('100-5', '100-%d' % last_byte, file_size - 100) 44 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; 52 }; 53 // Avoid cached content by appending unique URL param. 54 xmlhttp.open('GET', "%s?t=" + Date.now(), true); 55 xmlhttp.setRequestHeader('Range', 'bytes=%s'); 56 xmlhttp.send(); 57 """ % (self.UrlOfUnittestFile(self._test_filename), content_range_request)) 58 self._tab.WaitForJavaScriptExpression('loaded', 5) 59 content_range = self._tab.EvaluateJavaScript( 60 'xmlhttp.getResponseHeader("Content-Range");') 61 content_range_response = 'bytes %s/%d' % (content_range_response, 62 self._test_file_size) 63 self.assertEquals(content_range, content_range_response) 64 content_length = self._tab.EvaluateJavaScript( 65 'xmlhttp.getResponseHeader("Content-Length");') 66 self.assertEquals(content_length, str(content_length_response)) 67