t_uri.ml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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' = pred i'
  61. and i = pred i in
  62. i' < 0
  63. || (suffix.[i'] = s.[i]
  64. && (f i' i))
  65. in
  66. (l' <= l)
  67. && (f l' l)
  68. in
  69. assert (ends_with ~suffix:"" "");
  70. assert (ends_with ~suffix:"s" "s");
  71. assert (ends_with ~suffix:"/seppo.cgi" "uhu/seppo.cgi");
  72. assert (ends_with ~suffix:"🐫" "s🐫");
  73. assert (not (ends_with ~suffix:"s" ""));
  74. assert (not (ends_with ~suffix:"s" "su"));
  75. assert (not (ends_with ~suffix:"s" "su"));
  76. assert true
  77. let test_1 () =
  78. Logr.info (fun m -> m "uri_test.test_1" );
  79. let u = "https://uid@example.com/dir/seppo.cgi"
  80. |> Uri.of_string
  81. in
  82. u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "uid" ;
  83. u |> Uri.host |> Option.get |> Assrt.equals_string __LOC__ "example.com" ;
  84. u |> Uri.path |> Assrt.equals_string __LOC__ "/dir/seppo.cgi" ;
  85. let u = u |> Uri.with_uri ~userinfo:(Some "ufo") in
  86. u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "ufo" ;
  87. assert true
  88. let () =
  89. test_resolve ();
  90. test_00 ();
  91. test_0 ();
  92. test_1 ();
  93. assert true