From 29043aa7b4d11e377bc76d453f592ea5a6df1f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 7 Nov 2018 18:55:06 +0100 Subject: Add support for range requests (RFC7233) in cowboy_rest This is currently undocumented but is planned to be documented in the next version. --- test/handlers/range_satisfiable_h.erl | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/handlers/range_satisfiable_h.erl (limited to 'test/handlers/range_satisfiable_h.erl') diff --git a/test/handlers/range_satisfiable_h.erl b/test/handlers/range_satisfiable_h.erl new file mode 100644 index 0000000..357c30e --- /dev/null +++ b/test/handlers/range_satisfiable_h.erl @@ -0,0 +1,39 @@ +%% This module defines the range_satisfiable callback +%% and return something different depending on query string. + +-module(range_satisfiable_h). + +-export([init/2]). +-export([content_types_provided/2]). +-export([ranges_provided/2]). +-export([range_satisfiable/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}. + +ranges_provided(Req, State) -> + {[{<<"bytes">>, get_text_plain_bytes}], Req, State}. + +%% Simulate the callback being missing, otherwise expect true/false. +range_satisfiable(#{qs := <<"missing">>}, _) -> + no_call; +range_satisfiable(Req=#{qs := <<"false-int">>}, State) -> + {{false, 123}, Req, State}; +range_satisfiable(Req=#{qs := <<"false-bin">>}, State) -> + {{false, <<"*/456">>}, Req, State}; +range_satisfiable(Req=#{qs := Qs}, State) -> + {Qs =:= <<"true">>, 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}. -- cgit v1.2.3