-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform_content_test.py
executable file
·168 lines (149 loc) · 5.31 KB
/
transform_content_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# Copyright 2008 Brett Slatkin
#
# 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.
__author__ = "Brett Slatkin (bslatkin@gmail.com) and Brandon Barker (brandonbarker767@gmail.com)"
import logging
import unittest
import transform_content
################################################################################
class TransformTest(unittest.TestCase):
def _RunTransformTest(self, base_url, accessed_url, original, expected):
tag_tests = [
'<img src="%s"/>',
"<img src='%s'/>",
"<img src=%s/>",
"<img src=\"%s'/>",
"<img src='%s\"/>",
"<img src \t= '%s'/>",
"<img src \t= \t '%s'/>",
"<img src = '%s'/>",
'<a href="%s">',
"<a href='%s'>",
"<a href=%s>",
"<a href=\"%s'>",
"<a href='%s\">",
"<a href \t = \t'%s'>",
"<a href \t = '%s'>",
"<a href = \t'%s'>",
"<td background=%s>",
"<td background='%s'>",
'<td background="%s">',
'<form action="%s">',
"<form action='%s'>",
"<form action=%s>",
"<form action=\"%s'>",
"<form action='%s\">",
"<form action \t = \t'%s'>",
"<form action \t = '%s'>",
"<form action = \t'%s'>",
"@import '%s';",
"@import '%s'\nnext line here",
"@import \t '%s';",
"@import %s;",
"@import %s",
'@import "%s";',
'@import "%s"\nnext line here',
"@import url(%s)",
"@import url('%s')",
'@import url("%s")',
"background: transparent url(%s) repeat-x left;",
'background: transparent url("%s") repeat-x left;',
"background: transparent url('%s') repeat-x left;",
'<meta http-equiv="Refresh" content="0; URL=%s">',
]
for tag in tag_tests:
test = tag % original
correct = tag % expected
result = transform_content.TransformContent(base_url, accessed_url, test)
logging.info("Test with\n"
"Accessed: %s\n"
"Input : %s\n"
"Received: %s\n"
"Expected: %s",
accessed_url, test, result, correct)
if result != correct:
logging.info("FAIL")
self.assertEquals(correct, result)
def testBaseTransform(self):
self._RunTransformTest(
"slashdot.org",
"http://slashdot.org",
"//images.slashdot.org/iestyles.css?T_2_5_0_204",
"/images.slashdot.org/iestyles.css?T_2_5_0_204")
def testAbsolute(self):
self._RunTransformTest(
"slashdot.org",
"http://slashdot.org",
"http://slashdot.org/slashdot_files/all-minified.js",
"/slashdot.org/slashdot_files/all-minified.js")
def testRelative(self):
self._RunTransformTest(
"slashdot.org",
"http://slashdot.org",
"images/foo.html",
"/slashdot.org/images/foo.html")
def testUpDirectory(self):
self._RunTransformTest(
"a248.e.akamai.net",
"http://a248.e.akamai.net/foobar/is/the/path.html",
"../layout/mh_phone-home.png",
"/a248.e.akamai.net/foobar/is/the/../layout/mh_phone-home.png")
def testSameDirectoryRelative(self):
self._RunTransformTest(
"a248.e.akamai.net",
"http://a248.e.akamai.net/foobar/is/the/path.html",
"./layout/mh_phone-home.png",
"/a248.e.akamai.net/foobar/is/the/./layout/mh_phone-home.png")
def testSameDirectory(self):
self._RunTransformTest(
"a248.e.akamai.net",
"http://a248.e.akamai.net/foobar/is/the/path.html",
"mh_phone-home.png",
"/a248.e.akamai.net/foobar/is/the/mh_phone-home.png")
def testSameDirectoryNoParent(self):
self._RunTransformTest(
"a248.e.akamai.net",
"http://a248.e.akamai.net/path.html",
"mh_phone-home.png",
"/a248.e.akamai.net/mh_phone-home.png")
def testSameDirectoryWithParent(self):
self._RunTransformTest(
"a248.e.akamai.net",
("http://a248.e.akamai.net/7/248/2041/1447/store.apple.com"
"/rs1/css/aos-screen.css"),
"aos-layout.css",
("/a248.e.akamai.net/7/248/2041/1447/store.apple.com"
"/rs1/css/aos-layout.css"))
def testRootDirectory(self):
self._RunTransformTest(
"a248.e.akamai.net",
"http://a248.e.akamai.net/foobar/is/the/path.html",
"/",
"/a248.e.akamai.net/")
def testSecureContent(self):
self._RunTransformTest(
"slashdot.org",
"https://slashdot.org",
"https://images.slashdot.org/iestyles.css?T_2_5_0_204",
"/images.slashdot.org/iestyles.css?T_2_5_0_204")
def testPartiallySecureContent(self):
self._RunTransformTest(
"slashdot.org",
"http://slashdot.org",
"https://images.slashdot.org/iestyles.css?T_2_5_0_204",
"/images.slashdot.org/iestyles.css?T_2_5_0_204")
################################################################################
if __name__ == "__main__":
logging.getLogger().setLevel(logging.DEBUG)
unittest.main()