#!/usr/bin/env python # Copyright 2013 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import script_injector import unittest LONG_COMMENT = '' % ('comment,' * 200) SCRIPT_TO_INJECT = 'var flag = 0;' EXPECTED_SCRIPT = '' % SCRIPT_TO_INJECT TEXT_HTML = 'text/html' TEXT_CSS = 'text/css' APPLICATION = 'application/javascript' TEMPLATE_HEAD = '%s' TEMPLATE_HTML = '%s' TEMPLATE_DOCTYPE = '%s' TEMPLATE_RAW = '%s' TEMPLATE_COMMENT = '%s%s%s%s' class ScriptInjectorTest(unittest.TestCase): def test_unsupported_content_type(self): source = 'abc' # CSS. new_source, already_injected = script_injector.InjectScript( source, TEXT_CSS, SCRIPT_TO_INJECT) self.assertEqual(new_source, source) self.assertFalse(already_injected) # Javascript. new_source, already_injected = script_injector.InjectScript( source, APPLICATION, SCRIPT_TO_INJECT) self.assertEqual(new_source, source) self.assertFalse(already_injected) def test_empty_content_as_already_injected(self): source, already_injected = script_injector.InjectScript( '', TEXT_HTML, SCRIPT_TO_INJECT) self.assertEqual(source, '') self.assertTrue(already_injected) def test_already_injected(self): source, already_injected = script_injector.InjectScript( TEMPLATE_HEAD % EXPECTED_SCRIPT, TEXT_HTML, SCRIPT_TO_INJECT) self.assertEqual(source, TEMPLATE_HEAD % EXPECTED_SCRIPT) self.assertTrue(already_injected) def _assert_successful_injection(self, template): source, already_injected = script_injector.InjectScript( template % '', TEXT_HTML, SCRIPT_TO_INJECT) self.assertEqual(source, template % EXPECTED_SCRIPT) self.assertFalse(already_injected) def test_normal(self): self._assert_successful_injection(TEMPLATE_HEAD) def test_no_head_tag(self): self._assert_successful_injection(TEMPLATE_HTML) def test_no_head_and_html_tag(self): self._assert_successful_injection(TEMPLATE_DOCTYPE) def test_no_head_html_and_doctype_tag(self): self._assert_successful_injection(TEMPLATE_RAW) def _assert_successful_injection_with_comment(self, before_doctype, after_doctype, after_html): source, already_injected = script_injector.InjectScript( TEMPLATE_COMMENT % (before_doctype, after_doctype, after_html, ''), TEXT_HTML, SCRIPT_TO_INJECT) expected_source = TEMPLATE_COMMENT % (before_doctype, after_doctype, after_html, EXPECTED_SCRIPT) self.assertEqual(source, expected_source) self.assertFalse(already_injected) def test_comment_before_doctype(self): self._assert_successful_injection_with_comment(LONG_COMMENT, '', '') def test_comment_after_doctype(self): self._assert_successful_injection_with_comment('', LONG_COMMENT, '') def test_comment_after_html(self): self._assert_successful_injection_with_comment('', '', LONG_COMMENT) def test_all_comments(self): self._assert_successful_injection_with_comment( LONG_COMMENT, LONG_COMMENT, LONG_COMMENT) if __name__ == '__main__': unittest.main()