aboutsummaryrefslogtreecommitdiffstats
path: root/test/rest_handler_SUITE.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-11 16:09:28 +0100
committerLoïc Hoguin <[email protected]>2018-11-11 16:09:28 +0100
commit7840f6db7b340d4d0630019507eaffa725abbaad (patch)
tree9ab68d486e6cbf8d516b48e792f956635b3516f1 /test/rest_handler_SUITE.erl
parentdd0fbab6b79e1cefa31adf6e9647f0d76bbade06 (diff)
downloadcowboy-7840f6db7b340d4d0630019507eaffa725abbaad.tar.gz
cowboy-7840f6db7b340d4d0630019507eaffa725abbaad.tar.bz2
cowboy-7840f6db7b340d4d0630019507eaffa725abbaad.zip
Add ProvideRangeCallback tests using sendfile
And fix this case when multiple ranges are requested.
Diffstat (limited to 'test/rest_handler_SUITE.erl')
-rw-r--r--test/rest_handler_SUITE.erl64
1 files changed, 64 insertions, 0 deletions
diff --git a/test/rest_handler_SUITE.erl b/test/rest_handler_SUITE.erl
index 58898b2..363f789 100644
--- a/test/rest_handler_SUITE.erl
+++ b/test/rest_handler_SUITE.erl
@@ -413,6 +413,30 @@ provide_range_callback(Config) ->
{ok, <<"This is ranged REST!">>} = gun:await_body(ConnPid, Ref),
ok.
+provide_range_callback_sendfile(Config) ->
+ doc("A successful request for a single range results in a "
+ "206 partial content response with content-range set. (RFC7233 4.1, RFC7233 4.2)"),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ {<<"range">>, <<"bytes=0-">>}
+ ]),
+ Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
+ Size = filelib:file_size(Path),
+ {ok, Body} = file:read_file(Path),
+ {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
+ {_, ContentRange} = lists:keyfind(<<"content-range">>, 1, Headers),
+ ContentRange = iolist_to_binary([
+ <<"bytes 0-">>,
+ integer_to_binary(Size - 1),
+ <<"/">>,
+ integer_to_binary(Size)
+ ]),
+ {_, <<"text/plain">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+ {ok, Body} = gun:await_body(ConnPid, Ref),
+ ok.
+
provide_range_callback_multipart(Config) ->
doc("A successful request for multiple ranges results in a "
"206 partial content response using the multipart/byteranges "
@@ -442,6 +466,46 @@ provide_range_callback_multipart(Config) ->
<<"ThisisrangedREST!">> = BodyAcc,
ok.
+provide_range_callback_multipart_sendfile(Config) ->
+ doc("A successful request for multiple ranges results in a "
+ "206 partial content response using the multipart/byteranges "
+ "content-type and the content-range not being set. The real "
+ "content-type and content-range of the parts can be found in "
+ "the multipart headers. (RFC7233 4.1, RFC7233 A)"),
+ ConnPid = gun_open(Config),
+ Ref = gun:get(ConnPid, "/provide_range_callback?sendfile", [
+ {<<"accept-encoding">>, <<"gzip">>},
+ %% This range selects a few random chunks of the file.
+ {<<"range">>, <<"bytes=50-99, 150-199, 250-299, -99">>}
+ ]),
+ Path = code:lib_dir(cowboy) ++ "/ebin/cowboy.app",
+ Size = filelib:file_size(Path),
+ Skip = Size - 399,
+ {ok, <<
+ _:50/binary, Body1:50/binary,
+ _:50/binary, Body2:50/binary,
+ _:50/binary, Body3:50/binary,
+ _:Skip/binary, Body4/bits>>} = file:read_file(Path),
+ {response, nofin, 206, Headers} = gun:await(ConnPid, Ref),
+ {_, <<"bytes">>} = lists:keyfind(<<"accept-ranges">>, 1, Headers),
+ false = lists:keyfind(<<"content-range">>, 1, Headers),
+ {_, <<"multipart/byteranges; boundary=", Boundary/bits>>}
+ = lists:keyfind(<<"content-type">>, 1, Headers),
+ {ok, Body0} = gun:await_body(ConnPid, Ref),
+ Body = do_decode(Headers, Body0),
+ %% We will receive the ranges in the same order as requested.
+ {ContentRanges, BodyAcc} = do_provide_range_callback_multipart_body(Body, Boundary, [], <<>>),
+ LastFrom = 300 + Skip,
+ LastTo = Size - 1,
+ [
+ {bytes, 50, 99, Size},
+ {bytes, 150, 199, Size},
+ {bytes, 250, 299, Size},
+ {bytes, LastFrom, LastTo, Size}
+ ] = ContentRanges,
+ BodyAcc = <<Body1/binary, Body2/binary, Body3/binary, Body4/binary>>,
+ ok.
+
do_provide_range_callback_multipart_body(Rest, Boundary, ContentRangesAcc, BodyAcc) ->
case cow_multipart:parse_headers(Rest, Boundary) of
{ok, Headers, Rest1} ->