range_satisfiable_h.erl 1.2 KB

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