stream_handler_h.erl 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. %% This module behaves differently depending on a specific header.
  2. -module(stream_handler_h).
  3. -behavior(cowboy_stream).
  4. -export([init/3]).
  5. -export([data/4]).
  6. -export([info/3]).
  7. -export([terminate/3]).
  8. -export([early_error/5]).
  9. init(StreamID, Req, Opts) ->
  10. %% @todo Vary behavior depending on x-test-case.
  11. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
  12. Pid ! {Pid, self(), init, StreamID, Req, Opts},
  13. {[{headers, 200, #{}}], Pid}.
  14. data(StreamID, IsFin, Data, State=Pid) ->
  15. Pid ! {Pid, self(), data, StreamID, IsFin, Data, State},
  16. {[], State}.
  17. info(StreamID, Info, State=Pid) ->
  18. Pid ! {Pid, self(), info, StreamID, Info, State},
  19. {[], State}.
  20. terminate(StreamID, Reason, State=Pid) ->
  21. Pid ! {Pid, self(), terminate, StreamID, Reason, State},
  22. ok.
  23. %% This clause can only test for early errors that reached the required header.
  24. early_error(StreamID, Reason, PartialReq, Resp, Opts) ->
  25. Pid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, PartialReq))),
  26. Pid ! {Pid, self(), early_error, StreamID, Reason, PartialReq, Resp, Opts},
  27. Resp.