http-mirage-client/test/test.ml

218 lines
7.7 KiB
OCaml
Raw Permalink Normal View History

2022-10-31 12:16:41 +00:00
(* Functoria *)
2022-11-01 13:41:10 +00:00
module DNS_client =
2023-02-02 13:33:39 +00:00
Dns_client_mirage.Make (Mirage_crypto_rng) (Time) (Mclock) (Pclock)
2022-11-01 13:41:10 +00:00
(Tcpip_stack_socket.V4V6)
module Happy_eyeballs =
Happy_eyeballs_mirage.Make (Time) (Mclock) (Tcpip_stack_socket.V4V6)
(DNS_client)
module Mimic_happy_eyeballs =
Mimic_happy_eyeballs.Make (Tcpip_stack_socket.V4V6) (DNS_client)
(Happy_eyeballs)
2022-10-31 12:16:41 +00:00
module HTTP_server = Paf_mirage.Make (Tcpip_stack_socket.V4V6.TCP)
2022-11-01 13:41:10 +00:00
module HTTP_client =
Http_mirage_client.Make (Pclock) (Tcpip_stack_socket.V4V6.TCP)
(Mimic_happy_eyeballs)
2022-10-31 12:16:41 +00:00
let http_1_1_error_handler ?notify (ipaddr, port) ?request:_ error respond =
2022-11-01 13:41:10 +00:00
let contents =
match error with
2022-10-31 12:16:41 +00:00
| `Bad_gateway -> Fmt.str "Bad gateway (%a:%d)" Ipaddr.pp ipaddr port
| `Bad_request -> Fmt.str "Bad request (%a:%d)" Ipaddr.pp ipaddr port
2022-11-01 13:41:10 +00:00
| `Exn exn ->
Fmt.str "Exception %S (%a:%d)" (Printexc.to_string exn) Ipaddr.pp ipaddr
port
| `Internal_server_error ->
Fmt.str "Internal server error (%a:%d)" Ipaddr.pp ipaddr port in
2022-10-31 12:16:41 +00:00
let open Httpaf in
2022-11-01 13:41:10 +00:00
Option.iter (fun push -> push (Some ((ipaddr, port), error))) notify
; let headers =
Headers.of_list
[
"content-type", "text/plain"
; "content-length", string_of_int (String.length contents)
; "connection", "close"
] in
let body = respond headers in
Body.write_string body contents
; Body.close_writer body
2022-10-31 12:16:41 +00:00
2022-11-01 13:41:10 +00:00
let alpn_error_handler :
type reqd headers request response ro wo.
?notify:(((Ipaddr.t * int) * Alpn.server_error) option -> unit)
-> Ipaddr.t * int
-> (reqd, headers, request, response, ro, wo) Alpn.protocol
-> ?request:request
-> Alpn.server_error
-> (headers -> wo)
-> unit =
fun ?notify (ipaddr, port) protocol ?request:_ error respond ->
let contents =
match error with
| `Bad_gateway -> Fmt.str "Bad gateway (%a:%d)" Ipaddr.pp ipaddr port
| `Bad_request -> Fmt.str "Bad request (%a:%d)" Ipaddr.pp ipaddr port
| `Exn exn ->
Fmt.str "Exception %S (%a:%d)" (Printexc.to_string exn) Ipaddr.pp ipaddr
port
| `Internal_server_error ->
Fmt.str "Internal server error (%a:%d)" Ipaddr.pp ipaddr port in
Option.iter (fun push -> push (Some ((ipaddr, port), error))) notify
; let headers =
[
"content-type", "text/plain"
; "content-length", string_of_int (String.length contents)
] in
2022-10-31 12:16:41 +00:00
match protocol with
| Alpn.HTTP_1_1 _ ->
let open Httpaf in
let headers = Headers.of_list (("connection", "close") :: headers) in
let body = respond headers in
2022-11-01 13:41:10 +00:00
Body.write_string body contents
; Body.close_writer body
2022-10-31 12:16:41 +00:00
| Alpn.H2 _ ->
let open H2 in
let headers = Headers.of_list headers in
let body = respond headers in
2022-11-01 13:41:10 +00:00
H2.Body.Writer.write_string body contents
; H2.Body.Writer.close body
2022-10-31 12:16:41 +00:00
2022-11-01 13:41:10 +00:00
type alpn_handler = {
handler:
'reqd 'headers 'request 'response 'ro 'wo.
'reqd
-> ('reqd, 'headers, 'request, 'response, 'ro, 'wo) Alpn.protocol
-> unit
}
2022-10-31 12:16:41 +00:00
[@@unboxed]
let server ?error ?stop stack = function
| `HTTP_1_1 (port, handler) ->
let open Lwt.Syntax in
let+ http_server = HTTP_server.init ~port stack in
2022-11-01 13:41:10 +00:00
let http_service =
HTTP_server.http_service
~error_handler:(http_1_1_error_handler ?notify:error)
(fun _flow (_ipaddr, _port) -> handler) in
2022-10-31 12:16:41 +00:00
HTTP_server.serve ?stop http_service http_server
| `ALPN (tls, port, handler) ->
let open Lwt.Syntax in
let alpn_handler =
2022-11-01 13:41:10 +00:00
{
Alpn.error=
(fun edn protocol ?request v respond ->
alpn_error_handler ?notify:error edn protocol ?request v respond)
; Alpn.request=
(fun _flow (_ipaddr, _port) reqd protocol ->
handler.handler reqd protocol)
} in
2022-10-31 12:16:41 +00:00
let+ http_server = HTTP_server.init ~port stack in
let alpn_service = HTTP_server.alpn_service ~tls alpn_handler in
HTTP_server.serve ?stop alpn_service http_server
let stack () =
2022-10-31 12:16:41 +00:00
let open Lwt.Syntax in
let ip = Ipaddr.V4.(Prefix.make 8 localhost) in
let ipv4_only = true and ipv6_only = false in
2022-11-01 13:41:10 +00:00
let* tcpv4v6 =
Tcpip_stack_socket.V4V6.TCP.connect ~ipv4_only ~ipv6_only ip None
in
2022-11-01 13:41:10 +00:00
let* udpv4v6 =
Tcpip_stack_socket.V4V6.UDP.connect ~ipv4_only ~ipv6_only ip None
in
2022-10-31 12:16:41 +00:00
Tcpip_stack_socket.V4V6.connect udpv4v6 tcpv4v6
let test01 =
Alcotest_lwt.test_case "Simple Hello World! (GET)" `Quick @@ fun _sw () ->
let open Lwt.Syntax in
let stop = Lwt_switch.create () in
let handler reqd =
let open Httpaf in
let contents = "Hello World!" in
2022-11-01 13:41:10 +00:00
let headers =
Headers.of_list
[
"content-type", "text/plain"
; "content-length", string_of_int (String.length contents)
; "connection", "close"
] in
2022-10-31 12:16:41 +00:00
let response = Response.create ~headers `OK in
Reqd.respond_with_string reqd response contents in
let* stack = stack () in
2022-10-31 12:16:41 +00:00
let happy_eyeballs = Happy_eyeballs.create stack in
let* ctx = Mimic_happy_eyeballs.connect happy_eyeballs in
let* t = HTTP_client.connect ctx in
2022-11-01 13:41:10 +00:00
let* (`Initialized _thread) =
server ~stop (Tcpip_stack_socket.V4V6.tcp stack) (`HTTP_1_1 (8080, handler))
in
let* result =
Http_mirage_client.request t "http://localhost:8080/"
(fun _response buf str -> Buffer.add_string buf str ; Lwt.return buf)
(Buffer.create 0x100) in
2022-10-31 12:16:41 +00:00
match result with
| Error err ->
let* () = Lwt_switch.turn_off stop in
let* () = Tcpip_stack_socket.V4V6.disconnect stack in
Alcotest.failf "Client error: %a" Mimic.pp_error err
| Ok (_response, buf) ->
let* () = Lwt_switch.turn_off stop in
let* () = Tcpip_stack_socket.V4V6.disconnect stack in
let body = Buffer.contents buf in
2022-11-01 13:41:10 +00:00
Alcotest.(check string) "body" "Hello World!" body
; Lwt.return_unit
2022-10-31 14:30:42 +00:00
let random_string ~len =
let res = Bytes.create len in
for i = 0 to len - 1 do
Bytes.set res i (Char.chr (Random.bits () land 0xff))
2022-11-01 13:41:10 +00:00
done
; Bytes.unsafe_to_string res
2022-10-31 14:30:42 +00:00
let test02 =
Alcotest_lwt.test_case "Repeat (POST)" `Quick @@ fun _sw () ->
let open Lwt.Syntax in
let stop = Lwt_switch.create () in
let handler reqd =
let open Httpaf in
2022-11-01 13:41:10 +00:00
let {Request.meth; _} = Reqd.request reqd in
if meth <> `POST then invalid_arg "Invalid HTTP method"
; let headers = Headers.of_list ["content-type", "text/plain"] in
let response = Response.create ~headers `OK in
let src = Reqd.request_body reqd in
let dst = Reqd.respond_with_streaming reqd response in
let rec on_eof () = Body.close_reader src ; Body.close_writer dst
and on_read buf ~off ~len =
Body.write_bigstring dst ~off ~len buf
; Body.schedule_read src ~on_eof ~on_read in
2022-10-31 14:30:42 +00:00
Body.schedule_read src ~on_eof ~on_read in
let* stack = stack () in
2022-10-31 14:30:42 +00:00
let happy_eyeballs = Happy_eyeballs.create stack in
let* ctx = Mimic_happy_eyeballs.connect happy_eyeballs in
let* t = HTTP_client.connect ctx in
2022-11-01 13:41:10 +00:00
let* (`Initialized _thread) =
server ~stop (Tcpip_stack_socket.V4V6.tcp stack) (`HTTP_1_1 (8080, handler))
in
2022-10-31 14:30:42 +00:00
let str = random_string ~len:0x1000 in
2022-11-01 13:41:10 +00:00
let* result =
Http_mirage_client.request ~meth:`POST ~body:str t "http://localhost:8080/"
(fun _response buf str -> Buffer.add_string buf str ; Lwt.return buf)
(Buffer.create 0x1000) in
2022-10-31 14:30:42 +00:00
match result with
| Error err ->
let* () = Lwt_switch.turn_off stop in
let* () = Tcpip_stack_socket.V4V6.disconnect stack in
Alcotest.failf "Client error: %a" Mimic.pp_error err
| Ok (_response, buf) ->
let* () = Lwt_switch.turn_off stop in
let* () = Tcpip_stack_socket.V4V6.disconnect stack in
let body = Buffer.contents buf in
2022-11-01 13:41:10 +00:00
Alcotest.(check string) "body" str body
; Lwt.return_unit
let () =
Alcotest_lwt.run "http-mirage-client" ["http/1.1", [test01; test02]]
|> Lwt_main.run