forked from annevk/url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urltestgenerator.html
88 lines (81 loc) · 2.44 KB
/
urltestgenerator.html
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
<!doctype html>
<!--
This lacks documentation. But basically you pass in an array of tests (in cases below)
and a common base URL and after some tweaking you get tests you can use in urltests.txt.
With some more tweaking you can vary the base URL, too.
Not covered by this format: testing URLs & encodings, and testing setting of properties,
such as .host.
-->
<meta charset=utf-8>
<script src=url.js></script>
<pre><script>
var base = "about:blank"
var cases = []
function serialize_components(url) {
var output = "",
components = ["_scheme", "_username", "_password", "_host", "_port", "_path", "_query", "_fragment"],
componentsKeys = ["s", "u", "pass", "h", "port", "p", "q", "f"]
for(var i=0, l = components.length; i < l; i++) {
var val = components[i] == "_path" ? url.pathname : url[components[i]]
if(val !== "") {
if(val === null) {
continue // only true for "_password"
}
if(output !== "")
output += " "
output += componentsKeys[i] + ":" + esc(val)
} else if(components[i] == "_password") {
if(output !== "")
output += " "
output += componentsKeys[i] + ":" // need to log empty string for "_password" as default is null
}
}
return output
}
function serialize(input, base, components) {
var output = esc(input)
if(base || components)
output += " "
if(base)
output += base
if(components)
output += " " + components
return output += "\n"
}
function hex(input, padding) {
var output = input.toString(16).toUpperCase()
while(output.length < padding) {
output = "0" + output
}
return output
}
function esc(input) {
var output = ""
for(var i=0, l = input.length; i < l; i++) {
if(input[i] == "\\") {
output += "\\\\"
} else if(input[i] == "\n") {
output += "\\n"
} else if(input[i] == "\r") {
output += "\\r"
} else if(input[i] == " ") {
output += "\\s"
} else if(input[i] == "\t") {
output += "\\t"
} else if(input.charCodeAt(i) > 127 || input.charCodeAt(i) < 20) {
output += "\\u" + hex(input.charCodeAt(i), 4)
} else {
output += input[i]
}
}
return output
}
var copyBase = base
//base = new jURL(base)
for(var i = 0, l = cases.length; i < l; i++) {
var input = cases[i][0]
var components = serialize_components(new jURL(input, new jURL(base)))
document.write(serialize(input, copyBase, components))
copyBase = ""
}
</script></pre>