Source

amall / tests / test_uri.ml

Full commit
open Uri
;

open Printf
;

value (parse_and_dump : string -> unit) u =
  match (parse u) with
  [ Some uri ->
      let uri_dump = dump_uri uri in
      printf "%S => %s\n%!" u uri_dump
  | None ->
      printf "%S is not an uri.\n%!" u
  ]
;


List.iter parse_and_dump
  [ "http://@ya.ru:/"
  ; "sch:qwe:asd:zxc?q"
  ; "http://asd@thehost:123/zxc/cvb#fff"
  ; "http://user:pass@127.0.0.1:8989/?qry"
  ; "asdfg"
  ; "/"
  ; ""
  ; "#a"
  ]
;



(*
value str = input_line stdin;
value lb = Lexing.from_string str;
value () = Urilex.test1 lb;
*)


(* query params test *)

value qp_tests =
  [ ( "" , [] )
  ; ( "\x00" , [] )
  ; ( "abc" , [("abc", "")] )
  ; ( "=def" , [] )
  ; ( "a=b" , [("a", "b")] )
  ; ( "a=b&c=d" , [("a", "b"); ("c", "d")] )
  ; ( "a=b;c=d" , [("a", "b"); ("c", "d")] )
  ; ( "a=b;c=d;a=e" , [("a", "b"); ("c", "d")] )
  ; ( "a=b;c=d;c=e" , [("a", "b"); ("c", "d")] )
  ; ( "a=b;c=" , [("a", "b"); ("c", "")] )
  ; ( "a=b;c=\x00" , [("a", "b"); ("c", "")] )
  ; ( "&&&;;;a=b;;&&c=d&&;" , [("a", "b"); ("c", "d")] )
  ; ( "a=b&%D0%BA%D0%B8%D1%81%D0%B0=%D0%BA%D1%83%D0%BA%D1%83&c=d"
    , [("a", "b"); ("киса", "куку"); ("c", "d")]
    )
  ]
;

value () =
  List.iter
    (fun (params, expected) ->
       let got = Uri.parse_params params in
       if got = expected
       then ()
       else
         let msg = Printf.sprintf "params: %s, got: %s" params
           (String.concat "; "
              (List.map
                 (fun (k, v) -> Printf.sprintf "%S=%S" k v)
                 got
              )
           )
         in failwith msg
    )
    qp_tests
;
value () = print_string "uri tests ok.\n"
;