From 4fd6e2f7cdca1c1adf1ba0bd76a0702328c380f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 4 Sep 2017 20:48:07 +0200 Subject: Accept sendfile tuple with 0 length in cowboy_req This will result in no data being sent. It's simply easier to do this than to have to handle 0 size cases in user code. --- test/handlers/resp_h.erl | 3 +++ test/req_SUITE.erl | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) (limited to 'test') diff --git a/test/handlers/resp_h.erl b/test/handlers/resp_h.erl index 694f04a..9b94e3f 100644 --- a/test/handlers/resp_h.erl +++ b/test/handlers/resp_h.erl @@ -58,6 +58,9 @@ do(<<"resp_headers_empty">>, Req, Opts) -> do(<<"set_resp_body">>, Req0, Opts) -> Arg = cowboy_req:binding(arg, Req0), Req1 = case Arg of + <<"sendfile0">> -> + AppFile = code:where_is_file("cowboy.app"), + cowboy_req:set_resp_body({sendfile, 0, 0, AppFile}, Req0); <<"sendfile">> -> AppFile = code:where_is_file("cowboy.app"), cowboy_req:set_resp_body({sendfile, 0, filelib:file_size(AppFile), AppFile}, Req0); diff --git a/test/req_SUITE.erl b/test/req_SUITE.erl index d155aa8..f7682cc 100644 --- a/test/req_SUITE.erl +++ b/test/req_SUITE.erl @@ -509,6 +509,27 @@ set_resp_body(Config) -> {200, _, AppFile} = do_get("/resp/set_resp_body/sendfile", Config), ok. +set_resp_body_sendfile0(Config) -> + doc("Response using set_resp_body with a sendfile of length 0."), + Path = "/resp/set_resp_body/sendfile0", + ConnPid = gun_open(Config), + %% First request. + Ref1 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]), + {response, IsFin, 200, _} = gun:await(ConnPid, Ref1), + {ok, <<>>} = case IsFin of + nofin -> gun:await_body(ConnPid, Ref1); + fin -> {ok, <<>>} + end, + %% Second request will confirm everything works as intended. + Ref2 = gun:get(ConnPid, Path, [{<<"accept-encoding">>, <<"gzip">>}]), + {response, IsFin, 200, _} = gun:await(ConnPid, Ref2), + {ok, <<>>} = case IsFin of + nofin -> gun:await_body(ConnPid, Ref2); + fin -> {ok, <<>>} + end, + gun:close(ConnPid), + ok. + has_resp_header(Config) -> doc("Has response header?"), {200, Headers, <<"OK">>} = do_get("/resp/has_resp_header", Config), -- cgit v1.2.3