From ce6119d4567213132c903b7a3c6c10ec9ba6c7d4 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 2 Dec 2024 14:56:45 -0500 Subject: [PATCH] and tests --- tests/multipart_form/dune | 2 +- .../t_content_disposition.expected | 3 ++ tests/multipart_form/t_content_disposition.ml | 39 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/multipart_form/t_content_disposition.expected create mode 100644 tests/multipart_form/t_content_disposition.ml diff --git a/tests/multipart_form/dune b/tests/multipart_form/dune index 48e31cbb..38e8d9f8 100644 --- a/tests/multipart_form/dune +++ b/tests/multipart_form/dune @@ -1,3 +1,3 @@ (tests - (names t_chunk t_parse t_content_type) + (names t_chunk t_parse t_content_type t_content_disposition) (libraries tiny_httpd tiny_httpd.multipart-form-data)) diff --git a/tests/multipart_form/t_content_disposition.expected b/tests/multipart_form/t_content_disposition.expected new file mode 100644 index 00000000..a44bee6c --- /dev/null +++ b/tests/multipart_form/t_content_disposition.expected @@ -0,0 +1,3 @@ +h: ["content-foobar": "yolo";"other": "whatev"], no content disp +h ["content-disposition": "form-data; name=helloworld; junk";"other": "whatev"]: got {kind="form-data"; name="helloworld"; filename=None}, expected {kind="form-data"; name="helloworld"; filename=None}, same=true +h ["content-disposition": "form-data; lol=mdr; filename=\"some quoted stuff\""]: got {kind="form-data"; name=None; filename="some quoted stuff"}, expected {kind="form-data"; name=None; filename="some quoted stuff"}, same=true diff --git a/tests/multipart_form/t_content_disposition.ml b/tests/multipart_form/t_content_disposition.ml new file mode 100644 index 00000000..355b6cf7 --- /dev/null +++ b/tests/multipart_form/t_content_disposition.ml @@ -0,0 +1,39 @@ +module MFD = Tiny_httpd_multipart_form_data + +let pf = Printf.printf +let spf = Printf.sprintf + +let pp_headers hs = + spf "[%s]" (String.concat ";" @@ List.map (fun (k, v) -> spf "%S: %S" k v) hs) + +let test_headers h (exp : _ option) = + match MFD.Content_disposition.parse h, exp with + | Some c1, Some c2 -> + pf "h %s: got %s, expected %s, same=%b\n" (pp_headers h) + (MFD.Content_disposition.to_string c1) + (MFD.Content_disposition.to_string c2) + (c1 = c2) + | Some c1, None -> + pf "h: %s, unexpected content disp %s\n" (pp_headers h) + (MFD.Content_disposition.to_string c1) + | None, Some c2 -> + pf "h: %s, expected content disp %s\n" (pp_headers h) + (MFD.Content_disposition.to_string c2) + | None, None -> pf "h: %s, no content disp\n" (pp_headers h) + +let () = + test_headers [ "content-foobar", "yolo"; "other", "whatev" ] None; + test_headers + [ + "content-disposition", "form-data; name=helloworld; junk"; + "other", "whatev"; + ] + (Some { kind = "form-data"; name = Some "helloworld"; filename = None }); + test_headers + [ + ( "content-disposition", + "form-data; lol=mdr; filename=\"some quoted stuff\"" ); + ] + (Some + { kind = "form-data"; name = None; filename = Some "some quoted stuff" }); + ()