Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report exceptions before closing the body #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/client_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ module Oneshot = struct
| Awaiting_response ->
set_error_and_handle_without_shutdown t error;
| Received_response(_, response_body) ->
set_error_and_handle_without_shutdown t error;
Body.close_reader response_body;
Body.execute_read response_body;
set_error_and_handle_without_shutdown t error;
end
;;

Expand Down
33 changes: 33 additions & 0 deletions lib_test/test_client_connection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ let test_report_exn () =
!error_message
;;

let test_report_exn_during_body_read () =
let request' = Request.create `GET "/" in
let response = Response.create `OK in

let body_done = ref false in
let error_message = ref None in
let body, t =
request
request'
~response_handler:(fun _ body ->
let on_read _ ~off:_ ~len:_ = () in
let on_eof () = body_done := true in
Body.schedule_read body ~on_read ~on_eof)
~error_handler:(function
| `Exn (Failure msg) ->
Alcotest.(check bool) "body is not complete" false !body_done;
error_message := Some msg
| _ -> assert false)
in
Body.close_writer body;
write_request t request';
writer_closed t;
reader_ready t;
read_response t response;
report_exn t (Failure "something went wrong");
connection_is_shutdown t;
Alcotest.(check (option string)) "something went wrong"
(Some "something went wrong")
!error_message;
Alcotest.(check bool) "body is complete" true !body_done;
;;

let test_input_shrunk () =
let request' = Request.create `GET "/" in
let response = Response.create `OK in (* not actually writen to the channel *)
Expand Down Expand Up @@ -268,6 +300,7 @@ let tests =
; "Response EOF", `Quick, test_response_eof
; "Response header order preserved", `Quick, test_response_header_order
; "report_exn" , `Quick, test_report_exn
; "report_exn_during_body_read", `Quick, test_report_exn_during_body_read
; "input_shrunk", `Quick, test_input_shrunk
; "failed response parse", `Quick, test_failed_response_parse
]