| 1 |
%% This module defines the range_satisfiable callback |
| 2 |
%% and return something different depending on query string. |
| 3 |
|
| 4 |
-module(range_satisfiable_h). |
| 5 |
|
| 6 |
-export([init/2]). |
| 7 |
-export([content_types_provided/2]). |
| 8 |
-export([ranges_provided/2]). |
| 9 |
-export([range_satisfiable/2]). |
| 10 |
-export([get_text_plain/2]). |
| 11 |
-export([get_text_plain_bytes/2]). |
| 12 |
|
| 13 |
init(Req, State) -> |
| 14 |
{cowboy_rest, Req, State}. |
| 15 |
|
| 16 |
content_types_provided(Req, State) -> |
| 17 |
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. |
| 18 |
|
| 19 |
ranges_provided(Req, State) -> |
| 20 |
{[{<<"bytes">>, get_text_plain_bytes}], Req, State}. |
| 21 |
|
| 22 |
%% Simulate the callback being missing, otherwise expect true/false. |
| 23 |
range_satisfiable(#{qs := <<"missing">>}, _) -> |
| 24 |
no_call; |
| 25 |
range_satisfiable(Req=#{qs := <<"false-int">>}, State) -> |
| 26 |
{{false, 123}, Req, State}; |
| 27 |
range_satisfiable(Req=#{qs := <<"false-bin">>}, State) -> |
| 28 |
{{false, <<"*/456">>}, Req, State}; |
| 29 |
range_satisfiable(Req=#{qs := Qs}, State) -> |
| 30 |
{Qs =:= <<"true">>, Req, State}. |
| 31 |
|
| 32 |
get_text_plain(Req, State) -> |
| 33 |
{<<"This is REST!">>, Req, State}. |
| 34 |
|
| 35 |
get_text_plain_bytes(Req, State) -> |
| 36 |
%% We send everything in one part, since we are not testing |
| 37 |
%% this callback specifically. |
| 38 |
Body = <<"This is ranged REST!">>, |
| 39 |
{[{{0, byte_size(Body) - 1, byte_size(Body)}, Body}], Req, State}. |