aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/if_range_h.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-11-07 18:55:06 +0100
committerLoïc Hoguin <[email protected]>2018-11-07 18:55:06 +0100
commit29043aa7b4d11e377bc76d453f592ea5a6df1f43 (patch)
tree921c9439bd9678f7e06c87f460f8ff892b350292 /test/handlers/if_range_h.erl
parentbdd324ec01b134be22e3fde73dfa75d6e56dfc0d (diff)
downloadcowboy-29043aa7b4d11e377bc76d453f592ea5a6df1f43.tar.gz
cowboy-29043aa7b4d11e377bc76d453f592ea5a6df1f43.tar.bz2
cowboy-29043aa7b4d11e377bc76d453f592ea5a6df1f43.zip
Add support for range requests (RFC7233) in cowboy_rest
This is currently undocumented but is planned to be documented in the next version.
Diffstat (limited to 'test/handlers/if_range_h.erl')
-rw-r--r--test/handlers/if_range_h.erl46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/handlers/if_range_h.erl b/test/handlers/if_range_h.erl
new file mode 100644
index 0000000..868edbf
--- /dev/null
+++ b/test/handlers/if_range_h.erl
@@ -0,0 +1,46 @@
+%% This module defines the ranges_provided callback
+%% and a generate_etag callback that returns something
+%% different depending on query string. It also defines
+%% a last_modified callback that must be ignored when a
+%% date is provided in if_range.
+
+-module(if_range_h).
+
+-export([init/2]).
+-export([content_types_provided/2]).
+-export([ranges_provided/2]).
+-export([generate_etag/2]).
+-export([last_modified/2]).
+-export([get_text_plain/2]).
+-export([get_text_plain_bytes/2]).
+
+init(Req, State) ->
+ {cowboy_rest, Req, State}.
+
+content_types_provided(Req, State) ->
+ {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
+
+%% Simulate the callback being missing.
+ranges_provided(#{qs := <<"missing-ranges_provided">>}, _) ->
+ no_call;
+ranges_provided(Req=#{qs := <<"empty-ranges_provided">>}, State) ->
+ {[], Req, State};
+ranges_provided(Req, State) ->
+ {[{<<"bytes">>, get_text_plain_bytes}], Req, State}.
+
+generate_etag(Req=#{qs := <<"weak-etag">>}, State) ->
+ {{weak, <<"weak-no-match">>}, Req, State};
+generate_etag(Req, State) ->
+ {{strong, <<"strong-and-match">>}, Req, State}.
+
+last_modified(Req, State) ->
+ {{{2222, 2, 22}, {11, 11, 11}}, Req, State}.
+
+get_text_plain(Req, State) ->
+ {<<"This is REST!">>, Req, State}.
+
+get_text_plain_bytes(Req, State) ->
+ %% We send everything in one part, since we are not testing
+ %% this callback specifically.
+ Body = <<"This is ranged REST!">>,
+ {[{{0, byte_size(Body) - 1, byte_size(Body)}, Body}], Req, State}.