-
Notifications
You must be signed in to change notification settings - Fork 11
/
unnecessary-tests-for-developers+debugging.php
324 lines (268 loc) · 19.3 KB
/
unnecessary-tests-for-developers+debugging.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
//Comment this if you want to run the weird tests
die();
//Note: rel2abs + the javascript version aren't included in this test file.
function runRel2AbsTests($tests) {
foreach ($tests as $test) {
$result = rel2abs($test["rel"], $test["base"]);
if ($result === $test["expected"]) {
echo "PASS: {$test['rel']} + {$test['base']} = $result\n";
} else {
echo "FAIL: Expected {$test['expected']}, got $result\n";
}
}
}
// Test cases
$tests = [
["rel" => "page2.html", "base" => "http://www.example.com/page1.html", "expected" => "http://www.example.com/page2.html"],
["rel" => "../page2.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/page2.html"],
["rel" => "page2.html", "base" => "http://www.example.com/dir/", "expected" => "http://www.example.com/dir/page2.html"],
["rel" => "/page2.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/page2.html"],
["rel" => "", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/"],
["rel" => "?query=1", "base" => "http://www.example.com/page1.html", "expected" => "http://www.example.com/page1.html?query=1"],
["rel" => "#fragment", "base" => "http://www.example.com/page1.html", "expected" => "http://www.example.com/page1.html#fragment"],
["rel" => "//cdn.example.com/lib.js", "base" => "http://www.example.com/page1.html", "expected" => "//cdn.example.com/lib.js"],
["rel" => "https://cdn.example.com/lib.js", "base" => "http://www.example.com/page1.html", "expected" => "https://cdn.example.com/lib.js"],
["rel" => "./page2.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html"],
["rel" => "../dir2/page2.html", "base" => "http://www.example.com/dir1/dir/page1.html", "expected" => "http://www.example.com/dir1/dir2/page2.html"],
["rel" => "/page2.html", "base" => "https://www.example.com:8080/dir/page1.html", "expected" => "https://www.example.com:8080/page2.html"],
["rel" => "page2.html", "base" => "http://www.example.com/dir1/dir/", "expected" => "http://www.example.com/dir1/dir/page2.html"],
["rel" => "page2.html?query=1", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html?query=1"],
["rel" => "page2.html#fragment", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html#fragment"],
// Testing with user info and port in base URL
["rel" => "page2.html", "base" => "http://user:[email protected]:8080/page1.html", "expected" => "http://user:[email protected]:8080/page2.html"],
// Base URL ends with a filename
["rel" => "../page2.html", "base" => "http://www.example.com/dir1/page1.html", "expected" => "http://www.example.com/page2.html"],
// Base URL ends with a slash
["rel" => "../page2.html", "base" => "http://www.example.com/dir1/", "expected" => "http://www.example.com/page2.html"],
// Complex relative path
["rel" => "../../../page2.html", "base" => "http://www.example.com/dir1/dir2/dir3/dir4/", "expected" => "http://www.example.com/page2.html"],
// Relative URL goes beyond the root directory
["rel" => "../../../../../../page2.html", "base" => "http://www.example.com/dir1/dir2/", "expected" => "http://www.example.com/page2.html"],
// Query strings and fragments in base URL
["rel" => "page2.html", "base" => "http://www.example.com/page1.html?query=123", "expected" => "http://www.example.com/page2.html"],
["rel" => "page2.html", "base" => "http://www.example.com/page1.html#anchor", "expected" => "http://www.example.com/page2.html"],
// Relative URL with query string and fragment
["rel" => "page2.html?query=123#anchor", "base" => "http://www.example.com/dir/", "expected" => "http://www.example.com/dir/page2.html?query=123#anchor"],
// Testing with different schemes
["rel" => "page2.html", "base" => "ftp://www.example.com/dir/", "expected" => "ftp://www.example.com/dir/page2.html"],
// Testing with file scheme
["rel" => "file2.txt", "base" => "file:///C:/dir1/file1.txt", "expected" => "file:///C:/dir1/file2.txt"],
// Testing without trailing slash in directory
["rel" => "page2.html", "base" => "http://www.example.com", "expected" => "http://www.example.com/page2.html"],
// Testing protocol-relative URL with https base
["rel" => "//cdn.example.com/lib.js", "base" => "https://www.example.com/page1.html", "expected" => "//cdn.example.com/lib.js"],
// Testing base URL with subdomain
["rel" => "page2.html", "base" => "http://sub.example.com/dir/", "expected" => "http://sub.example.com/dir/page2.html"],
// Testing with IPv6 address in base URL
["rel" => "page2.html", "base" => "http://[::1]/dir/", "expected" => "http://[::1]/dir/page2.html"],
["rel" => "page2.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html"],
["rel" => "page2.html?query=1", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html?query=1"],
["rel" => "page2.html#fragment", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page2.html#fragment"],
["rel" => "http://www.otherdomain.com/page.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.otherdomain.com/page.html"],
["rel" => "//www.otherdomain.com/page.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "//www.otherdomain.com/page.html"],
["rel" => "/rootfile.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/rootfile.html"],
["rel" => "../updir/page3.html", "base" => "http://www.example.com/dir/subdir/page1.html", "expected" => "http://www.example.com/dir/updir/page3.html"],
["rel" => "../../../updir/page2.html", "base" => "http://www.example.com/dir/subdir/another/page1.html", "expected" => "http://www.example.com/updir/page2.html"],
["rel" => "../../../../updir/page2.html", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/updir/page2.html"],
["rel" => "secure/page2.html", "base" => "https://www.example.com/dir/page1.html", "expected" => "https://www.example.com/dir/secure/page2.html"],
["rel" => "secure/page2.html", "base" => "https://www.example.com:443/dir/page1.html", "expected" => "https://www.example.com/dir/secure/page2.html"],
["rel" => "page2.html", "base" => "http://www.example.com:8080/dir/page1.html", "expected" => "http://www.example.com:8080/dir/page2.html"],
["rel" => "", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/"],
["rel" => "?newquery=1", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page1.html?newquery=1"],
["rel" => "#newfragment", "base" => "http://www.example.com/dir/page1.html", "expected" => "http://www.example.com/dir/page1.html#newfragment"],
["rel" => "file2.txt", "base" => "ftp://ftp.example.com/dir1/file1.txt", "expected" => "ftp://ftp.example.com/dir1/file2.txt"],
["rel" => "subdir/file2.txt", "base" => "ftp://ftp.example.com/dir1/file1.txt", "expected" => "ftp://ftp.example.com/dir1/subdir/file2.txt"],
["rel" => "file2.txt", "base" => "ftp://ftp.example.com/dir1/", "expected" => "ftp://ftp.example.com/dir1/file2.txt"],
["rel" => "/file2.txt", "base" => "ftp://ftp.example.com/dir1/", "expected" => "ftp://ftp.example.com/file2.txt"],
["rel" => "ftp://ftp.example2.com/file2.txt", "base" => "ftp://ftp.example.com/dir1/", "expected" => "ftp://ftp.example2.com/file2.txt"],
];
runTests($tests);
?>
<script>
/* Run the tests on the javascript alternative, note: the function isn't included here, you must do that yourself*/
const tests = [
{ rel: "page2.html", base: "http://www.example.com/page1.html", expected: "http://www.example.com/page2.html" },
{ rel: "../page2.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/page2.html" },
{ rel: "page2.html", base: "http://www.example.com/dir/", expected: "http://www.example.com/dir/page2.html" },
{ rel: "/page2.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/page2.html" },
{ rel: "", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/" },
{ rel: "?query=1", base: "http://www.example.com/page1.html", expected: "http://www.example.com/page1.html?query=1" },
{ rel: "#fragment", base: "http://www.example.com/page1.html", expected: "http://www.example.com/page1.html#fragment" },
{ rel: "//cdn.example.com/lib.js", base: "http://www.example.com/page1.html", expected: "//cdn.example.com/lib.js" },
{ rel: "https://cdn.example.com/lib.js", base: "http://www.example.com/page1.html", expected: "https://cdn.example.com/lib.js" },
{ rel: "./page2.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html" },
{ rel: "../dir2/page2.html", base: "http://www.example.com/dir1/dir/page1.html", expected: "http://www.example.com/dir1/dir2/page2.html" },
{ rel: "/page2.html", base: "https://www.example.com:8080/dir/page1.html", expected: "https://www.example.com:8080/page2.html" },
{ rel: "page2.html", base: "http://www.example.com/dir1/dir/", expected: "http://www.example.com/dir1/dir/page2.html" },
{ rel: "page2.html?query=1", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html?query=1" },
{ rel: "page2.html#fragment", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html#fragment" },
{ rel: "http://www.otherdomain.com/page.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.otherdomain.com/page.html" },
{ rel: "//www.otherdomain.com/page.html", base: "http://www.example.com/dir/page1.html", expected: "//www.otherdomain.com/page.html" },
{ rel: "/rootfile.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/rootfile.html" },
{ rel: "../updir/page3.html", base: "http://www.example.com/dir/subdir/page1.html", expected: "http://www.example.com/dir/updir/page3.html" },
{ rel: "../../../updir/page2.html", base: "http://www.example.com/dir/subdir/another/page1.html", expected: "http://www.example.com/updir/page2.html" },
{ rel: "../../../../updir/page2.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/updir/page2.html" },
{ rel: "secure/page2.html", base: "https://www.example.com/dir/page1.html", expected: "https://www.example.com/dir/secure/page2.html" },
{ rel: "secure/page2.html", base: "https://www.example.com:443/dir/page1.html", expected: "https://www.example.com/dir/secure/page2.html" },
{ rel: "page2.html", base: "http://www.example.com:8080/dir/page1.html", expected: "http://www.example.com:8080/dir/page2.html" },
{ rel: "", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/" },
{ rel: "?newquery=1", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page1.html?newquery=1" },
{ rel: "#newfragment", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page1.html#newfragment" },
{ rel: "../../../page2.html", base: "http://www.example.com/dir1/dir2/dir3/dir4/", expected: "http://www.example.com/page2.html" },
{ rel: "../../../../../../page2.html", base: "http://www.example.com/dir1/dir2/", expected: "http://www.example.com/page2.html" },
{ rel: "page2.html", base: "http://www.example.com/page1.html?query=123", expected: "http://www.example.com/page2.html" },
{ rel: "page2.html", base: "http://www.example.com/page1.html#anchor", expected: "http://www.example.com/page2.html" },
{ rel: "page2.html?query=123#anchor", base: "http://www.example.com/dir/", expected: "http://www.example.com/dir/page2.html?query=123#anchor" },
{ rel: "page2.html", base: "ftp://www.example.com/dir/", expected: "ftp://www.example.com/dir/page2.html" },
{ rel: "file2.txt", base: "file:///C:/dir1/file1.txt", expected: "file:///C:/dir1/file2.txt" },
{ rel: "page2.html", base: "http://www.example.com", expected: "http://www.example.com/page2.html" },
{ rel: "//cdn.example.com/lib.js", base: "https://www.example.com/page1.html", expected: "//cdn.example.com/lib.js" },
{ rel: "page2.html", base: "http://sub.example.com/dir/", expected: "http://sub.example.com/dir/page2.html" },
{ rel: "page2.html", base: "http://[::1]/dir/", expected: "http://[::1]/dir/page2.html" },
{ rel: "page2.html", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html" },
{ rel: "page2.html?query=1", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html?query=1" },
{ rel: "page2.html#fragment", base: "http://www.example.com/dir/page1.html", expected: "http://www.example.com/dir/page2.html#fragment" },
{ rel: "file2.txt", base: "ftp://ftp.example.com/dir1/file1.txt", expected: "ftp://ftp.example.com/dir1/file2.txt" },
{ rel: "subdir/file2.txt", base: "ftp://ftp.example.com/dir1/file1.txt", expected: "ftp://ftp.example.com/dir1/subdir/file2.txt" },
{ rel: "file2.txt", base: "ftp://ftp.example.com/dir1/", expected: "ftp://ftp.example.com/dir1/file2.txt" },
{ rel: "/file2.txt", base: "ftp://ftp.example.com/dir1/", expected: "ftp://ftp.example.com/file2.txt" },
{ rel: "ftp://ftp.example2.com/file2.txt", base: "ftp://ftp.example.com/dir1/", expected: "ftp://ftp.example2.com/file2.txt" },
];
// Run tests
tests.forEach(test => {
const result = rel2abs(test.rel, test.base);
if (result === test.expected) {
//console.log(`PASS: ${test.rel} + ${test.base} = ${result}`);
} else {
console.log(`FAIL: Expected ${test.expected}, got ${result}`);
}
});
</script>
<?php
//Test proxifyCSS
// Base URL for testing relative URLs
$baseURL = 'http://example.com/';
// Test cases
$testCases = [
// Simple URL
"body { background-image: url('image.jpg'); }",
// URL with spaces
"div { background: url('image with spaces.jpg'); }",
"div { background: url('image with spa:\"ces.jpg'); }",
// URL with special characters
"div::before { content: url(\"special&char*acters?.jpg\"); }",
"div::before { content: url(\"specia'l&char*acters?.jpg\"); }",
// HTTPS URL
"footer { background-image: url(https://example.com/image.jpg); }",
// Data URL
"p { cursor: url('data:image/png;base64,...'), auto; }",
"p { cursor: 'data:image/png;base64,...', auto; }",
"p { cursor: \"data:image/png;base64,...\", auto; }",
// @import rules
"@import url('style.css');",
"@import 'another-style.css';",
// @font-face rule
"@font-face { font-family: 'MyFont'; src: url('fonts/MyFont.woff2') format('woff2'); }",
// Complex case with multiple URLs
"div { background-image: url(image.jpg); }\n@import url('style.css');\np { cursor: url(data:image/png;base64,...), auto; }",
// Edge cases: empty url, missing quotes
"a { background: url(); }",
"a { background: url(''); }",
"a { background: url(\"\"); }",
"a { background: url(unquoted.jpg); }",
"
@import url('child1.css');
@import 'child1.css';
@import url('child2.css');
@import 'child2.css';
@import url(\"child3.css\");
@import \"child3.css\";
@import url('child4.css');
@import 'child4.css';
"
];
// Run tests
foreach ($testCases as $testCase) {
//echo "Original CSS:\n$testCase\n\n";
$proxifiedCSS = proxifyCSS($testCase, $baseURL);
if ($testCase == $proxifiedCSS) {
echo 'FUCK FUCK' . $proxifiedCSS;
}
}
// After all tests, echo proxified CSS
foreach ($testCases as $testCase) {
$proxifiedCSS = proxifyCSS($testCase, $baseURL);
echo "Proxified CSS:\n$proxifiedCSS\n";
echo "--------------------------------\n";
}
//Test proxifySrcset
$testCases = [
[
'srcset' => 'photos/pic1.jpg 1x, photos/[email protected] 2x, photos/[email protected] 3x',
'baseURL' => 'http://example.com/',
'expectedResult' => 'http://proxy.example.com/photos/pic1.jpg 1x, http://proxy.example.com/photos/[email protected] 2x, http://proxy.example.com/photos/[email protected] 3x'
],
[
'srcset' => 'gallery/picture2.jpg 1x, gallery/[email protected] 2x, gallery/[email protected] 3x',
'baseURL' => 'http://sub.example.com/path/',
'expectedResult' => 'http://proxy.example.com/path/gallery/picture2.jpg 1x, http://proxy.example.com/path/gallery/[email protected] 2x, http://proxy.example.com/path/gallery/[email protected] 3x'
],
[
'srcset' => 'assets/img3.jpg 1x, assets/[email protected] 2x, assets/[email protected] 3x',
'baseURL' => 'https://example.net/',
'expectedResult' => 'http://proxy.example.com/assets/img3.jpg 1x, http://proxy.example.com/assets/[email protected] 2x, http://proxy.example.com/assets/[email protected] 3x'
],
[
'srcset' => 'pictures/photo_4.jpg 1x, pictures/[email protected] 2x, pictures/[email protected] 3x',
'baseURL' => 'http://subdomain.example.net/path2/',
'expectedResult' => 'http://proxy.example.com/path2/pictures/photo_4.jpg 1x, http://proxy.example.com/path2/pictures/[email protected] 2x, http://proxy.example.com/path2/pictures/[email protected] 3x'
],
[
'srcset' => 'images/img5.jpg 1x, images/[email protected] 2x, images/[email protected] 3x',
'baseURL' => 'http://sub2.example.org/',
'expectedResult' => 'http://proxy.example.com/images/img5.jpg 1x, http://proxy.example.com/images/[email protected] 2x, http://proxy.example.com/images/[email protected] 3x'
],
[
'srcset' => 'pictures/image6.jpg 1x, pictures/[email protected] 2x, pictures/[email protected] 3x',
'baseURL' => 'http://example.org/',
'expectedResult' => 'http://proxy.example.com/pictures/image6.jpg 1x, http://proxy.example.com/pictures/[email protected] 2x, http://proxy.example.com/pictures/[email protected] 3x'
],
[
'srcset' => 'photos/pic7.jpg 1x, photos/[email protected] 2x, photos/[email protected] 3x',
'baseURL' => 'https://subdomain.example.org/path3/',
'expectedResult' => 'http://proxy.example.com/path3/photos/pic7.jpg 1x, http://proxy.example.com/path3/photos/[email protected] 2x, http://proxy.example.com/path3/photos/[email protected] 3x'
],
[
'srcset' => 'images/picture8.jpg 1x, images/[email protected] 2x, images/[email protected] 3x',
'baseURL' => 'http://example.net/',
'expectedResult' => 'http://proxy.example.com/images/picture8.jpg 1x, http://proxy.example.com/images/[email protected] 2x, http://proxy.example.com/images/[email protected] 3x'
],
[
'srcset' => 'photos/img9.jpg 1x, photos/[email protected] 2x, photos/[email protected] 3x',
'baseURL' => 'https://sub.example.net/',
'expectedResult' => 'http://proxy.example.com/photos/img9.jpg 1x, http://proxy.example.com/photos/[email protected] 2x, http://proxy.example.com/photos/[email protected] 3x'
],
[
'srcset' => 'pictures/pic10.jpg 1x, pictures/[email protected] 2x, pictures/[email protected] 3x',
'baseURL' => 'http://sub.example.net/path4/',
'expectedResult' => 'http://proxy.example.com/path4/pictures/pic10.jpg 1x, http://proxy.example.com/path4/pictures/[email protected] 2x, http://proxy.example.com/path4/pictures/[email protected] 3x'
],
// Add more unique test cases here...
];
// Test the proxifySrcset function for each test case
foreach ($testCases as $index => $testCase) {
$srcset = $testCase['srcset'];
$baseURL = $testCase['baseURL'];
$expectedResult = PROXY_PREFIX . $testCase['expectedResult'];
// Call the proxifySrcset function
$proxifiedSrcset = proxifySrcset($srcset, $baseURL);
// Output the original and proxified srcset attributes for comparison
//echo "Test Case $index:" . PHP_EOL;
echo "Original srcset: $srcset" . PHP_EOL;
// echo "Base URL: $baseURL" . PHP_EOL;
echo "Expected result: $expectedResult" . PHP_EOL;
echo "Actual result: $proxifiedSrcset" . PHP_EOL;
echo "Test Result: " . (($proxifiedSrcset === $expectedResult) ? "Pass" : "Fail") . PHP_EOL;
echo PHP_EOL;
}