| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2012 Google Inc. All Rights Reserved. | 2 # Copyright 2012 Google Inc. All Rights Reserved. |
| 3 # | 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 7 # | 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # | 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 14 # limitations under the License. | 14 # limitations under the License. |
| 15 | 15 |
| 16 import mock | 16 import mock |
| 17 import unittest | 17 import unittest |
| 18 | 18 |
| 19 import datetime | 19 import datetime |
| 20 import dnsproxy | 20 import dnsproxy |
| 21 import httparchive | 21 import httparchive |
| 22 import httpclient | 22 import httpclient |
| 23 import platformsettings | 23 import platformsettings |
| 24 import script_injector |
| 24 import test_utils | 25 import test_utils |
| 25 | 26 |
| 26 | 27 |
| 27 class RealHttpFetchTest(unittest.TestCase): | 28 class RealHttpFetchTest(unittest.TestCase): |
| 28 | 29 |
| 29 # Initialize test data | 30 # Initialize test data |
| 30 CONTENT_TYPE = 'content-type: image/x-icon' | 31 CONTENT_TYPE = 'content-type: image/x-icon' |
| 31 COOKIE_1 = ('Set-Cookie: GMAIL_IMP=EXPIRED; ' | 32 COOKIE_1 = ('Set-Cookie: GMAIL_IMP=EXPIRED; ' |
| 32 'Expires=Thu, 12-Jul-2012 22:41:22 GMT; ' | 33 'Expires=Thu, 12-Jul-2012 22:41:22 GMT; ' |
| 33 'Path=/mail; Secure') | 34 'Path=/mail; Secure') |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 fetch = httpclient.RealHttpFetch(real_dns_lookup) | 205 fetch = httpclient.RealHttpFetch(real_dns_lookup) |
| 205 request = httparchive.ArchivedHttpRequest( | 206 request = httparchive.ArchivedHttpRequest( |
| 206 command='GET', host='google.com', full_path='/search?q=dogs', | 207 command='GET', host='google.com', full_path='/search?q=dogs', |
| 207 request_body=None, headers={}, is_ssl=True) | 208 request_body=None, headers={}, is_ssl=True) |
| 208 response = fetch(request) | 209 response = fetch(request) |
| 209 self.assertIsNotNone(response) | 210 self.assertIsNotNone(response) |
| 210 | 211 |
| 211 | 212 |
| 212 class HttpArchiveFetchTest(unittest.TestCase): | 213 class HttpArchiveFetchTest(unittest.TestCase): |
| 213 | 214 |
| 215 TEST_REQUEST_TIME = datetime.datetime(2016, 11, 17, 1, 2, 3, 456) |
| 216 |
| 214 def createTestResponse(self): | 217 def createTestResponse(self): |
| 215 return httparchive.ArchivedHttpResponse( | 218 return httparchive.ArchivedHttpResponse( |
| 216 11, 200, 'OK', [('content-type', 'text/html')], | 219 11, 200, 'OK', [('content-type', 'text/html')], |
| 217 '<body>test</body>', request_time=datetime.datetime(2016, 11, 17)) | 220 ['<body>test</body>'], |
| 221 request_time=HttpArchiveFetchTest.TEST_REQUEST_TIME) |
| 218 | 222 |
| 219 def checkTestResponse(self, actual_response, archive, request): | 223 def checkTestResponse(self, actual_response, archive, request): |
| 220 self.assertEqual(actual_response, archive[request]) | 224 self.assertEqual(actual_response, archive[request]) |
| 221 self.assertEqual('<body>test</body>', actual_response.response_data) | 225 self.assertEqual(['<body>test</body>'], actual_response.response_data) |
| 222 self.assertEqual(datetime.datetime(2016, 11, 17), | 226 self.assertEqual(HttpArchiveFetchTest.TEST_REQUEST_TIME, |
| 223 actual_response.request_time) | 227 actual_response.request_time) |
| 224 | 228 |
| 225 @staticmethod | 229 @staticmethod |
| 226 def dummy_injector(_): | 230 def dummy_injector(_): |
| 227 return '<body>test</body>' | 231 return '<body>test</body>' |
| 228 | 232 |
| 229 | 233 |
| 230 class RecordHttpArchiveFetchTest(HttpArchiveFetchTest): | 234 class RecordHttpArchiveFetchTest(HttpArchiveFetchTest): |
| 231 | 235 |
| 232 @mock.patch('httpclient.RealHttpFetch') | 236 @mock.patch('httpclient.RealHttpFetch') |
| (...skipping 13 matching lines...) Expand all Loading... |
| 246 def testFetch(self): | 250 def testFetch(self): |
| 247 request = httparchive.ArchivedHttpRequest( | 251 request = httparchive.ArchivedHttpRequest( |
| 248 'GET', 'www.test.com', '/', None, {}) | 252 'GET', 'www.test.com', '/', None, {}) |
| 249 response = self.createTestResponse() | 253 response = self.createTestResponse() |
| 250 archive = httparchive.HttpArchive() | 254 archive = httparchive.HttpArchive() |
| 251 archive[request] = response | 255 archive[request] = response |
| 252 fetch = httpclient.ReplayHttpArchiveFetch( | 256 fetch = httpclient.ReplayHttpArchiveFetch( |
| 253 archive, None, self.dummy_injector) | 257 archive, None, self.dummy_injector) |
| 254 self.checkTestResponse(fetch(request), archive, request) | 258 self.checkTestResponse(fetch(request), archive, request) |
| 255 | 259 |
| 260 @mock.patch('script_injector.util.resource_string') |
| 261 @mock.patch('script_injector.util.resource_exists') |
| 262 @mock.patch('script_injector.os.path.exists') |
| 263 def testInjectedDate(self, os_path, util_exists, util_resource_string): |
| 264 os_path.return_value = False |
| 265 util_exists.return_value = True |
| 266 util_resource_string.return_value = \ |
| 267 ["""var time_seed={}""".format(script_injector.TIME_SEED_MARKER)] |
| 268 request = httparchive.ArchivedHttpRequest( |
| 269 'GET', 'www.test.com', '/', None, {}) |
| 270 response = self.createTestResponse() |
| 271 archive = httparchive.HttpArchive() |
| 272 archive[request] = response |
| 273 |
| 274 fetch = httpclient.ReplayHttpArchiveFetch( |
| 275 archive, None, script_injector.GetScriptInjector("time_script.js")) |
| 276 self.assertEqual( |
| 277 ['<script>var time_seed=1479344523000</script><body>test</body>'], |
| 278 fetch(request).response_data) |
| 279 |
| 256 | 280 |
| 257 if __name__ == '__main__': | 281 if __name__ == '__main__': |
| 258 unittest.main() | 282 unittest.main() |
| OLD | NEW |