From 948b5e63891104b32066d446ae40431404f38613 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 3 Dec 2019 17:32:12 -0600 Subject: [PATCH] test: add a unit test for parsing http req --- src/Tiny_httpd.ml | 22 ++++++++++++++++++++++ src/Tiny_httpd.mli | 8 ++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Tiny_httpd.ml b/src/Tiny_httpd.ml index b2012fd4..ff3246bb 100644 --- a/src/Tiny_httpd.ml +++ b/src/Tiny_httpd.ml @@ -488,8 +488,30 @@ module Request = struct with | Bad_req _ as e -> raise e | e -> bad_reqf 500 "failed to read body: %s" (Printexc.to_string e) + + module Internal_ = struct + let parse_req_start ?(buf=Buf_.create()) bs = + parse_req_start ~buf bs |> unwrap_resp_result + + let parse_body ?(buf=Buf_.create()) req bs : _ t = + parse_body_ ~tr_stream:(fun s->s) ~buf {req with body=bs} |> unwrap_resp_result + end end +(*$R + let q = "GET hello HTTP/1.1\r\nHost: coucou\r\nContent-Length: 11\r\n\r\nsalutationsSOMEJUNK" in + let str = Byte_stream.of_string q in + let r = Request.Internal_.parse_req_start str in + match r with + | None -> assert_failure "should parse" + | Some req -> + assert_equal (Some "coucou") (Headers.get "Host" req.Request.headers); + assert_equal "hello" req.Request.path; + let req = Request.Internal_.parse_body req str |> Request.read_body_full in + assert_equal ~printer:(fun s->s) "salutations" req.Request.body; + () +*) + module Response = struct type body = [`String of string | `Stream of byte_stream] type t = { diff --git a/src/Tiny_httpd.mli b/src/Tiny_httpd.mli index 869cfde7..d5b4824d 100644 --- a/src/Tiny_httpd.mli +++ b/src/Tiny_httpd.mli @@ -244,6 +244,14 @@ module Request : sig val read_body_full : byte_stream t -> string t (** Read the whole body into a string. Potentially blocking. *) + + (**/**) + (* for testing purpose, do not use *) + module Internal_ : sig + val parse_req_start : ?buf:Buf_.t -> byte_stream -> unit t option + val parse_body : ?buf:Buf_.t -> unit t -> byte_stream -> byte_stream t + end + (**/**) end (** {2 Response Codes} *)