forked from chromium/web-page-replay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_injector_test.py
executable file
·104 lines (83 loc) · 3.8 KB
/
script_injector_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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 = '<!--%s-->' % ('comment,' * 200)
SCRIPT_TO_INJECT = 'var flag = 0;'
EXPECTED_SCRIPT = '<script>%s</script>' % SCRIPT_TO_INJECT
TEXT_HTML = 'text/html'
TEXT_CSS = 'text/css'
APPLICATION = 'application/javascript'
TEMPLATE_HEAD = '<!doctype html><html><head>%s</head><body></body></html>'
TEMPLATE_HTML = '<!doctype html><html>%s<body></body></html>'
TEMPLATE_DOCTYPE = '<!doctype html>%s<body></body>'
TEMPLATE_RAW = '%s<body></body>'
TEMPLATE_COMMENT = '%s<!doctype html>%s<html>%s<head>%s</head></html>'
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()