turi.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. discard """
  2. targets: "c js"
  3. """
  4. import std/uri
  5. from std/uri {.all.} as uri2 import removeDotSegments
  6. from std/sequtils import toSeq
  7. template main() =
  8. block: # encodeUrl, decodeUrl
  9. const test1 = "abc\L+def xyz"
  10. doAssert encodeUrl(test1) == "abc%0A%2Bdef+xyz"
  11. doAssert decodeUrl(encodeUrl(test1)) == test1
  12. doAssert encodeUrl(test1, false) == "abc%0A%2Bdef%20xyz"
  13. doAssert decodeUrl(encodeUrl(test1, false), false) == test1
  14. doAssert decodeUrl(encodeUrl(test1)) == test1
  15. block: # removeDotSegments
  16. doAssert removeDotSegments("/foo/bar/baz") == "/foo/bar/baz"
  17. doAssert removeDotSegments("") == "" # empty test
  18. doAssert removeDotSegments(".") == "." # trailing period
  19. doAssert removeDotSegments("a1/a2/../a3/a4/a5/./a6/a7/././") == "a1/a3/a4/a5/a6/a7/"
  20. doAssert removeDotSegments("https://a1/a2/../a3/a4/a5/./a6/a7/././") == "https://a1/a3/a4/a5/a6/a7/"
  21. doAssert removeDotSegments("http://a1/a2") == "http://a1/a2"
  22. doAssert removeDotSegments("http://www.ai.") == "http://www.ai."
  23. when false: # xxx these cases are buggy
  24. # this should work, refs https://webmasters.stackexchange.com/questions/73934/how-can-urls-have-a-dot-at-the-end-e-g-www-bla-de
  25. doAssert removeDotSegments("http://www.ai./") == "http://www.ai./" # fails
  26. echo removeDotSegments("http://www.ai./") # http://www.ai/
  27. echo removeDotSegments("a/b.../c") # b.c
  28. echo removeDotSegments("a/b../c") # bc
  29. echo removeDotSegments("a/.../c") # .c
  30. echo removeDotSegments("a//../b") # a/b
  31. echo removeDotSegments("a/b/c//") # a/b/c//
  32. block: # parseUri
  33. block:
  34. let org = "udp://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
  35. let url = parseUri(org)
  36. doAssert url.hostname == "2001:0db8:85a3:0000:0000:8a2e:0370:7334" # true
  37. let newUrl = parseUri($url)
  38. doAssert newUrl.hostname == "2001:0db8:85a3:0000:0000:8a2e:0370:7334" # true
  39. block:
  40. let str = "http://localhost"
  41. let test = parseUri(str)
  42. doAssert test.path == ""
  43. block:
  44. let str = "http://localhost/"
  45. let test = parseUri(str)
  46. doAssert test.path == "/"
  47. block:
  48. let str = "http://localhost:8080/test"
  49. let test = parseUri(str)
  50. doAssert test.scheme == "http"
  51. doAssert test.port == "8080"
  52. doAssert test.path == "/test"
  53. doAssert test.hostname == "localhost"
  54. doAssert($test == str)
  55. block:
  56. let str = "foo://username:password@example.com:8042/over/there" &
  57. "/index.dtb?type=animal&name=narwhal#nose"
  58. let test = parseUri(str)
  59. doAssert test.scheme == "foo"
  60. doAssert test.username == "username"
  61. doAssert test.password == "password"
  62. doAssert test.hostname == "example.com"
  63. doAssert test.port == "8042"
  64. doAssert test.path == "/over/there/index.dtb"
  65. doAssert test.query == "type=animal&name=narwhal"
  66. doAssert test.anchor == "nose"
  67. doAssert($test == str)
  68. block:
  69. # IPv6 address
  70. let str = "foo://[::1]:1234/bar?baz=true&qux#quux"
  71. let uri = parseUri(str)
  72. doAssert uri.scheme == "foo"
  73. doAssert uri.hostname == "::1"
  74. doAssert uri.port == "1234"
  75. doAssert uri.path == "/bar"
  76. doAssert uri.query == "baz=true&qux"
  77. doAssert uri.anchor == "quux"
  78. block:
  79. let str = "urn:example:animal:ferret:nose"
  80. let test = parseUri(str)
  81. doAssert test.scheme == "urn"
  82. doAssert test.path == "example:animal:ferret:nose"
  83. doAssert($test == str)
  84. block:
  85. let str = "mailto:username@example.com?subject=Topic"
  86. let test = parseUri(str)
  87. doAssert test.scheme == "mailto"
  88. doAssert test.username == "username"
  89. doAssert test.hostname == "example.com"
  90. doAssert test.query == "subject=Topic"
  91. doAssert($test == str)
  92. block:
  93. let str = "magnet:?xt=urn:sha1:72hsga62ba515sbd62&dn=foobar"
  94. let test = parseUri(str)
  95. doAssert test.scheme == "magnet"
  96. doAssert test.query == "xt=urn:sha1:72hsga62ba515sbd62&dn=foobar"
  97. doAssert($test == str)
  98. block:
  99. let str = "/test/foo/bar?q=2#asdf"
  100. let test = parseUri(str)
  101. doAssert test.scheme == ""
  102. doAssert test.path == "/test/foo/bar"
  103. doAssert test.query == "q=2"
  104. doAssert test.anchor == "asdf"
  105. doAssert($test == str)
  106. block:
  107. let str = "test/no/slash"
  108. let test = parseUri(str)
  109. doAssert test.path == "test/no/slash"
  110. doAssert($test == str)
  111. block:
  112. let str = "//git@github.com:dom96/packages"
  113. let test = parseUri(str)
  114. doAssert test.scheme == ""
  115. doAssert test.username == "git"
  116. doAssert test.hostname == "github.com"
  117. doAssert test.port == "dom96"
  118. doAssert test.path == "/packages"
  119. block:
  120. let str = "file:///foo/bar/baz.txt"
  121. let test = parseUri(str)
  122. doAssert test.scheme == "file"
  123. doAssert test.username == ""
  124. doAssert test.hostname == ""
  125. doAssert test.port == ""
  126. doAssert test.path == "/foo/bar/baz.txt"
  127. block: # combine
  128. block:
  129. let concat = combine(parseUri("http://google.com/foo/bar/"), parseUri("baz"))
  130. doAssert concat.path == "/foo/bar/baz"
  131. doAssert concat.hostname == "google.com"
  132. doAssert concat.scheme == "http"
  133. block:
  134. let concat = combine(parseUri("http://google.com/foo"), parseUri("/baz"))
  135. doAssert concat.path == "/baz"
  136. doAssert concat.hostname == "google.com"
  137. doAssert concat.scheme == "http"
  138. block:
  139. let concat = combine(parseUri("http://google.com/foo/test"), parseUri("bar"))
  140. doAssert concat.path == "/foo/bar"
  141. block:
  142. let concat = combine(parseUri("http://google.com/foo/test"), parseUri("/bar"))
  143. doAssert concat.path == "/bar"
  144. block:
  145. let concat = combine(parseUri("http://google.com/foo/test"), parseUri("bar"))
  146. doAssert concat.path == "/foo/bar"
  147. block:
  148. let concat = combine(parseUri("http://google.com/foo/test/"), parseUri("bar"))
  149. doAssert concat.path == "/foo/test/bar"
  150. block:
  151. let concat = combine(parseUri("http://google.com/foo/test/"), parseUri("bar/"))
  152. doAssert concat.path == "/foo/test/bar/"
  153. block:
  154. let concat = combine(parseUri("http://google.com/foo/test/"), parseUri("bar/"),
  155. parseUri("baz"))
  156. doAssert concat.path == "/foo/test/bar/baz"
  157. block: # `/`
  158. block:
  159. let test = parseUri("http://example.com/foo") / "bar/asd"
  160. doAssert test.path == "/foo/bar/asd"
  161. block:
  162. let test = parseUri("http://example.com/foo/") / "/bar/asd"
  163. doAssert test.path == "/foo/bar/asd"
  164. block: # bug #3207
  165. doAssert parseUri("http://qq/1").combine(parseUri("https://qqq")).`$` == "https://qqq"
  166. block: # bug #4959
  167. let foo = parseUri("http://example.com") / "/baz"
  168. doAssert foo.path == "/baz"
  169. block: # bug found on stream 13/10/17
  170. let foo = parseUri("http://localhost:9515") / "status"
  171. doAssert $foo == "http://localhost:9515/status"
  172. block: # bug #6649 #6652
  173. var foo = parseUri("http://example.com")
  174. foo.hostname = "example.com"
  175. foo.path = "baz"
  176. doAssert $foo == "http://example.com/baz"
  177. foo.hostname = "example.com/"
  178. foo.path = "baz"
  179. doAssert $foo == "http://example.com/baz"
  180. foo.hostname = "example.com"
  181. foo.path = "/baz"
  182. doAssert $foo == "http://example.com/baz"
  183. foo.hostname = "example.com/"
  184. foo.path = "/baz"
  185. doAssert $foo == "http://example.com/baz"
  186. foo.hostname = "example.com/"
  187. foo.port = "8000"
  188. foo.path = "baz"
  189. doAssert $foo == "http://example.com:8000/baz"
  190. foo = parseUri("file:/dir/file")
  191. foo.path = "relative"
  192. doAssert $foo == "file:relative"
  193. block: # isAbsolute tests
  194. doAssert "www.google.com".parseUri().isAbsolute() == false
  195. doAssert "http://www.google.com".parseUri().isAbsolute() == true
  196. doAssert "file:/dir/file".parseUri().isAbsolute() == true
  197. doAssert "file://localhost/dir/file".parseUri().isAbsolute() == true
  198. doAssert "urn:ISSN:1535-3613".parseUri().isAbsolute() == true
  199. # path-relative URL *relative
  200. doAssert "about".parseUri().isAbsolute == false
  201. doAssert "about/staff.html".parseUri().isAbsolute == false
  202. doAssert "about/staff.html?".parseUri().isAbsolute == false
  203. doAssert "about/staff.html?parameters".parseUri().isAbsolute == false
  204. # absolute-path-relative URL *relative
  205. doAssert "/".parseUri().isAbsolute == false
  206. doAssert "/about".parseUri().isAbsolute == false
  207. doAssert "/about/staff.html".parseUri().isAbsolute == false
  208. doAssert "/about/staff.html?".parseUri().isAbsolute == false
  209. doAssert "/about/staff.html?parameters".parseUri().isAbsolute == false
  210. # scheme-relative URL *relative
  211. doAssert "//username:password@example.com:8888".parseUri().isAbsolute == false
  212. doAssert "//username@example.com".parseUri().isAbsolute == false
  213. doAssert "//example.com".parseUri().isAbsolute == false
  214. doAssert "//example.com/".parseUri().isAbsolute == false
  215. doAssert "//example.com/about".parseUri().isAbsolute == false
  216. doAssert "//example.com/about/staff.html".parseUri().isAbsolute == false
  217. doAssert "//example.com/about/staff.html?".parseUri().isAbsolute == false
  218. doAssert "//example.com/about/staff.html?parameters".parseUri().isAbsolute == false
  219. # absolute URL *absolute
  220. doAssert "https://username:password@example.com:8888".parseUri().isAbsolute == true
  221. doAssert "https://username@example.com".parseUri().isAbsolute == true
  222. doAssert "https://example.com".parseUri().isAbsolute == true
  223. doAssert "https://example.com/".parseUri().isAbsolute == true
  224. doAssert "https://example.com/about".parseUri().isAbsolute == true
  225. doAssert "https://example.com/about/staff.html".parseUri().isAbsolute == true
  226. doAssert "https://example.com/about/staff.html?".parseUri().isAbsolute == true
  227. doAssert "https://example.com/about/staff.html?parameters".parseUri().isAbsolute == true
  228. block: # encodeQuery tests
  229. doAssert encodeQuery({:}) == ""
  230. doAssert encodeQuery({"foo": "bar"}) == "foo=bar"
  231. doAssert encodeQuery({"foo": "bar & baz"}) == "foo=bar+%26+baz"
  232. doAssert encodeQuery({"foo": "bar & baz"}, usePlus = false) == "foo=bar%20%26%20baz"
  233. doAssert encodeQuery({"foo": ""}) == "foo"
  234. doAssert encodeQuery({"foo": ""}, omitEq = false) == "foo="
  235. doAssert encodeQuery({"a": "1", "b": "", "c": "3"}) == "a=1&b&c=3"
  236. doAssert encodeQuery({"a": "1", "b": "", "c": "3"}, omitEq = false) == "a=1&b=&c=3"
  237. block: # `?`
  238. block:
  239. var foo = parseUri("http://example.com") / "foo" ? {"bar": "1", "baz": "qux"}
  240. var foo1 = parseUri("http://example.com/foo?bar=1&baz=qux")
  241. doAssert foo == foo1
  242. block:
  243. var foo = parseUri("http://example.com") / "foo" ? {"do": "do", "bar": ""}
  244. var foo1 = parseUri("http://example.com/foo?do=do&bar")
  245. doAssert foo == foo1
  246. block: # getDataUri, dataUriBase64
  247. doAssert getDataUri("", "text/plain") == "data:text/plain;charset=utf-8;base64,"
  248. doAssert getDataUri(" ", "text/plain") == "data:text/plain;charset=utf-8;base64,IA=="
  249. doAssert getDataUri("c\xf7>", "text/plain") == "data:text/plain;charset=utf-8;base64,Y/c+"
  250. doAssert getDataUri("Hello World", "text/plain") == "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQ="
  251. doAssert getDataUri("leasure.", "text/plain") == "data:text/plain;charset=utf-8;base64,bGVhc3VyZS4="
  252. doAssert getDataUri("""!@#$%^&*()_+""", "text/plain") == "data:text/plain;charset=utf-8;base64,IUAjJCVeJiooKV8r"
  253. doAssert(getDataUri("the quick brown dog jumps over the lazy fox", "text/plain") ==
  254. "data:text/plain;charset=utf-8;base64,dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA==")
  255. doAssert(getDataUri("The present is theirs\n The future, for which I really worked, is mine.", "text/plain") ==
  256. "data:text/plain;charset=utf-8;base64,VGhlIHByZXNlbnQgaXMgdGhlaXJzCiAgICAgIFRoZSBmdXR1cmUsIGZvciB3aGljaCBJIHJlYWxseSB3b3JrZWQsIGlzIG1pbmUu")
  257. block: # decodeQuery
  258. doAssert toSeq(decodeQuery("a=1&b=0")) == @[("a", "1"), ("b", "0")]
  259. doAssert toSeq(decodeQuery("a=1&b=2c=6")) == @[("a", "1"), ("b", "2c=6")]
  260. block: # bug #17481
  261. let u1 = parseUri("./")
  262. let u2 = parseUri("./path")
  263. let u3 = parseUri("a/path")
  264. doAssert u1.scheme.len == 0
  265. doAssert u1.path == "./"
  266. doAssert u2.scheme.len == 0
  267. doAssert u2.path == "./path"
  268. doAssert u3.scheme.len == 0
  269. doAssert u3.path == "a/path"
  270. static: main()
  271. main()