| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2013 Google Inc. All Rights Reserved. | |
| 3 # | |
| 4 # Licensed under the Apache License, Version 2.0 (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 | |
| 7 # | |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 # | |
| 10 # Unless required by applicable law or agreed to in writing, software | |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 # See the License for the specific language governing permissions and | |
| 14 # limitations under the License. | |
| 15 | |
| 16 import httparchive | |
| 17 import script_injector | |
| 18 import unittest | |
| 19 | |
| 20 | |
| 21 LONG_COMMENT = '<!--' + 'comment,' * 200 + '-->' | |
| 22 COMMENT_OR_NOT = ('', LONG_COMMENT) | |
| 23 SCRIPT_TO_INJECT = 'var flag = 0;' | |
| 24 EXPECTED_SCRIPT = '<script>' + SCRIPT_TO_INJECT + '</script>' | |
| 25 TEXT_HTML = 'text/html' | |
| 26 TEXT_CSS = 'text/css' | |
| 27 APPLICATION = 'application/javascript' | |
| 28 SEPARATOR = httparchive.ArchivedHttpResponse.CHUNK_EDIT_SEPARATOR | |
| 29 SEPARATORS_OR_NOT = ('', SEPARATOR, SEPARATOR*3) | |
| 30 | |
| 31 TEMPLATE_HEAD = """\ | |
| 32 {boundary_at_start}\ | |
| 33 <!doc{boundary_in_doctype}type html>{boundary_after_doctype}\ | |
| 34 <ht{boundary_in_html}ml>{boundary_after_html}\ | |
| 35 <he{boundary_in_head}ad>{injection}{boundary_after_head}\ | |
| 36 </head></html>\ | |
| 37 """ | |
| 38 TEMPLATE_HTML = """\ | |
| 39 {boundary_at_start}\ | |
| 40 <!doc{boundary_in_doctype}type html>{boundary_after_doctype}\ | |
| 41 <ht{boundary_in_html}ml>{injection}{boundary_after_html}\ | |
| 42 </html>\ | |
| 43 """ | |
| 44 TEMPLATE_DOCTYPE = """\ | |
| 45 {boundary_at_start}\ | |
| 46 <!doc{boundary_in_doctype}type html>{injection}{boundary_after_doctype}\ | |
| 47 <body></body>\ | |
| 48 """ | |
| 49 TEMPLATE_RAW = """\ | |
| 50 {boundary_at_start}\ | |
| 51 {injection}<body></body>\ | |
| 52 """ | |
| 53 NORMAL_TEMPLATES = (TEMPLATE_HEAD, TEMPLATE_HTML, | |
| 54 TEMPLATE_DOCTYPE, TEMPLATE_RAW) | |
| 55 TEMPLATE_COMMENT = """\ | |
| 56 {comment_before_doctype}<!doctype html>{comment_after_doctype}\ | |
| 57 <html>{comment_after_html}<head>{injection}</head></html>\ | |
| 58 """ | |
| 59 | |
| 60 | |
| 61 def _wrap_inject_script(source, application, script_to_inject): | |
| 62 text_chunks = source.split(SEPARATOR) | |
| 63 text_chunks, just_injected = script_injector.InjectScript( | |
| 64 text_chunks, application, script_to_inject) | |
| 65 result = SEPARATOR.join(text_chunks) | |
| 66 return result, just_injected | |
| 67 | |
| 68 | |
| 69 class ScriptInjectorTest(unittest.TestCase): | |
| 70 | |
| 71 def _assert_no_injection(self, source, application): | |
| 72 new_source, just_injected = _wrap_inject_script( | |
| 73 source, application, SCRIPT_TO_INJECT) | |
| 74 self.assertEqual(new_source, source) | |
| 75 self.assertFalse(just_injected) | |
| 76 | |
| 77 def _assert_successful_injection(self, template): | |
| 78 source, just_injected = _wrap_inject_script( | |
| 79 template.format(injection=''), TEXT_HTML, SCRIPT_TO_INJECT) | |
| 80 self.assertEqual(source, template.format(injection=EXPECTED_SCRIPT)) | |
| 81 self.assertTrue(just_injected) | |
| 82 | |
| 83 def test_unsupported_content_type(self): | |
| 84 self._assert_no_injection('abc', TEXT_CSS) | |
| 85 self._assert_no_injection('abc', APPLICATION) | |
| 86 | |
| 87 def test_empty_content_as_already_injected(self): | |
| 88 self._assert_no_injection('', TEXT_HTML) | |
| 89 | |
| 90 def test_non_html_content_with_html_content_type(self): | |
| 91 self._assert_no_injection('{"test": 1"}', TEXT_HTML) | |
| 92 | |
| 93 def test_already_injected(self): | |
| 94 parameters = {'injection': SCRIPT_TO_INJECT} | |
| 95 for template in NORMAL_TEMPLATES: | |
| 96 for parameters['boundary_at_start'] in SEPARATORS_OR_NOT: | |
| 97 for parameters['boundary_in_doctype'] in SEPARATORS_OR_NOT: | |
| 98 for parameters['boundary_after_doctype'] in SEPARATORS_OR_NOT: | |
| 99 for parameters['boundary_in_html'] in SEPARATORS_OR_NOT: | |
| 100 for parameters['boundary_after_html'] in SEPARATORS_OR_NOT: | |
| 101 for parameters['boundary_in_head'] in SEPARATORS_OR_NOT: | |
| 102 for parameters['boundary_after_head'] in SEPARATORS_OR_NOT: | |
| 103 source = template.format(**parameters) | |
| 104 self._assert_no_injection(source, TEXT_HTML) | |
| 105 | |
| 106 def test_normal(self): | |
| 107 parameters = {'injection': '{injection}'} | |
| 108 for template in NORMAL_TEMPLATES: | |
| 109 for parameters['boundary_at_start'] in SEPARATORS_OR_NOT: | |
| 110 for parameters['boundary_in_doctype'] in SEPARATORS_OR_NOT: | |
| 111 for parameters['boundary_after_doctype'] in SEPARATORS_OR_NOT: | |
| 112 for parameters['boundary_in_html'] in SEPARATORS_OR_NOT: | |
| 113 for parameters['boundary_after_html'] in SEPARATORS_OR_NOT: | |
| 114 for parameters['boundary_in_head'] in SEPARATORS_OR_NOT: | |
| 115 for parameters['boundary_after_head'] in SEPARATORS_OR_NOT: | |
| 116 template = template.format(**parameters) | |
| 117 self._assert_successful_injection(template) | |
| 118 | |
| 119 def test_comments(self): | |
| 120 parameters = {'injection': '{injection}'} | |
| 121 for parameters['comment_before_doctype'] in COMMENT_OR_NOT: | |
| 122 for parameters['comment_after_doctype'] in COMMENT_OR_NOT: | |
| 123 for parameters['comment_after_html'] in COMMENT_OR_NOT: | |
| 124 template = TEMPLATE_COMMENT.format(**parameters) | |
| 125 self._assert_successful_injection(template) | |
| 126 | |
| 127 | |
| 128 if __name__ == '__main__': | |
| 129 unittest.main() | |
| OLD | NEW |