mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 03:05:29 -05:00
test: add a unit test for parsing http req
This commit is contained in:
parent
de7acbaf6d
commit
948b5e6389
2 changed files with 30 additions and 0 deletions
|
|
@ -488,8 +488,30 @@ module Request = struct
|
||||||
with
|
with
|
||||||
| Bad_req _ as e -> raise e
|
| Bad_req _ as e -> raise e
|
||||||
| e -> bad_reqf 500 "failed to read body: %s" (Printexc.to_string 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
|
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
|
module Response = struct
|
||||||
type body = [`String of string | `Stream of byte_stream]
|
type body = [`String of string | `Stream of byte_stream]
|
||||||
type t = {
|
type t = {
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,14 @@ module Request : sig
|
||||||
|
|
||||||
val read_body_full : byte_stream t -> string t
|
val read_body_full : byte_stream t -> string t
|
||||||
(** Read the whole body into a string. Potentially blocking. *)
|
(** 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
|
end
|
||||||
|
|
||||||
(** {2 Response Codes} *)
|
(** {2 Response Codes} *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue