uri_test.ml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * uri_time.ml
  12. *
  13. * Copyright (C) The #Seppo contributors. All rights reserved.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *)
  28. open Seppo_lib
  29. let test_resolve () =
  30. let a = Uri.of_string "https://example.xom/a/a.a/?a#a"
  31. and b = Uri.of_string "./b?b#b" in
  32. Http.reso ~base:a b
  33. |> Uri.to_string
  34. |> Assrt.equals_string __LOC__ "https://example.xom/a/a.a/b?b#b";
  35. let b = Uri.of_string "http://example.com/c?c#c" in
  36. Http.reso ~base:a b
  37. |> Uri.to_string
  38. |> Assrt.equals_string __LOC__ "http://example.com/c?c#c"
  39. let test_00 () =
  40. let sub ?(pos = 0) ?(len = -1) s =
  41. let len = if len >= 0
  42. then len
  43. else (String.length s) - pos in
  44. String.sub s pos len
  45. in
  46. Uri.make
  47. ~userinfo:"fo@ o"
  48. ~host:"example.com"
  49. ()
  50. |> Uri.to_string
  51. |> sub ~pos:2
  52. |> Assrt.equals_string __LOC__ "fo%40%20o@example.com"
  53. let test_0 () =
  54. Logr.info (fun m -> m "uri_test.test_0" );
  55. let ends_with ~suffix s =
  56. let l = s |> String.length
  57. and l' = suffix |> String.length in
  58. let rec f i' i =
  59. (* Logr.info (fun m -> m "uri_test.test_0 f %d %d" i' i); *)
  60. let i' = i' - 1 and i = i - 1 in
  61. i' < 0
  62. || (suffix.[i'] = s.[i]
  63. && (f i' i))
  64. in
  65. (l' <= l)
  66. && (f l' l)
  67. in
  68. assert (ends_with ~suffix:"" "");
  69. assert (ends_with ~suffix:"s" "s");
  70. assert (ends_with ~suffix:"/seppo.cgi" "uhu/seppo.cgi");
  71. assert (ends_with ~suffix:"🐫" "s🐫");
  72. assert (not (ends_with ~suffix:"s" ""));
  73. assert (not (ends_with ~suffix:"s" "su"));
  74. assert (not (ends_with ~suffix:"s" "su"));
  75. assert true
  76. let test_1 () =
  77. Logr.info (fun m -> m "uri_test.test_1" );
  78. let u = "https://uid@example.com/dir/seppo.cgi"
  79. |> Uri.of_string
  80. in
  81. u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "uid" ;
  82. u |> Uri.host |> Option.get |> Assrt.equals_string __LOC__ "example.com" ;
  83. u |> Uri.path |> Assrt.equals_string __LOC__ "/dir/seppo.cgi" ;
  84. let u = u |> Uri.with_uri ~userinfo:(Some "ufo") in
  85. u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "ufo" ;
  86. assert true
  87. let () =
  88. test_resolve ();
  89. test_00 ();
  90. test_0 ();
  91. test_1 ();
  92. assert true