example.com -> example.org
[Seppo.git] / test / t_uri.ml
blob1183c974576f6ba49f434c51c96119d77fc7f384
1 (*
2 * _ _ ____ _
3 * _| || |_/ ___| ___ _ __ _ __ ___ | |
4 * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
5 * |_ _|___) | __/ |_) | |_) | (_) |_|
6 * |_||_| |____/ \___| .__/| .__/ \___/(_)
7 * |_| |_|
9 * Personal Social Web.
11 * uri_time.ml
13 * Copyright (C) The #Seppo contributors. All rights reserved.
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.
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.
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/>.
29 open Seppo_lib
31 let test_resolve () =
32 let a = Uri.of_string "https://example.xom/a/a.a/?a#a"
33 and b = Uri.of_string "./b?b#b" in
34 Http.reso ~base:a b
35 |> Uri.to_string
36 |> Assrt.equals_string __LOC__ "https://example.xom/a/a.a/b?b#b";
37 let b = Uri.of_string "http://example.com/c?c#c" in
38 Http.reso ~base:a b
39 |> Uri.to_string
40 |> Assrt.equals_string __LOC__ "http://example.com/c?c#c"
42 let test_00 () =
43 let sub ?(pos = 0) ?(len = -1) s =
44 let len = if len >= 0
45 then len
46 else (String.length s) - pos in
47 String.sub s pos len
49 Uri.make
50 ~userinfo:"fo@ o"
51 ~host:"example.com"
53 |> Uri.to_string
54 |> sub ~pos:2
55 |> Assrt.equals_string __LOC__ "fo%40%20o@example.com"
57 let test_0 () =
58 Logr.info (fun m -> m "uri_test.test_0" );
59 let ends_with ~suffix s =
60 let l = s |> String.length
61 and l' = suffix |> String.length in
62 let rec f i' i =
63 (* Logr.info (fun m -> m "uri_test.test_0 f %d %d" i' i); *)
64 let i' = pred i'
65 and i = pred i in
66 i' < 0
67 || (suffix.[i'] = s.[i]
68 && (f i' i))
70 (l' <= l)
71 && (f l' l)
73 assert (ends_with ~suffix:"" "");
74 assert (ends_with ~suffix:"s" "s");
75 assert (ends_with ~suffix:"/seppo.cgi" "uhu/seppo.cgi");
76 assert (ends_with ~suffix:"🐫" "s🐫");
77 assert (not (ends_with ~suffix:"s" ""));
78 assert (not (ends_with ~suffix:"s" "su"));
79 assert (not (ends_with ~suffix:"s" "su"));
80 assert true
83 let test_1 () =
84 Logr.info (fun m -> m "uri_test.test_1" );
85 let u = "https://uid@example.com/dir/seppo.cgi"
86 |> Uri.of_string
88 u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "uid" ;
89 u |> Uri.host |> Option.get |> Assrt.equals_string __LOC__ "example.com" ;
90 u |> Uri.path |> Assrt.equals_string __LOC__ "/dir/seppo.cgi" ;
91 let u = u |> Uri.with_uri ~userinfo:(Some "ufo") in
92 u |> Uri.user |> Option.get |> Assrt.equals_string __LOC__ "ufo" ;
93 assert true
95 let () =
96 test_resolve ();
97 test_00 ();
98 test_0 ();
99 test_1 ();
100 assert true