aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/set_options_h.erl
blob: ef88a6f69682e9843400b3b277b3b02ece41cdab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
%% This module sets options dynamically and performs
%% some related relevant operation for testing the change.

-module(set_options_h).

-export([init/2]).

init(Req, State) ->
	set_options(cowboy_req:binding(key, Req), Req, State).

set_options(<<"chunked_false">>, Req0, State) ->
	%% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
	#{pid := Pid, streamid := StreamID} = Req0,
	Pid ! {{Pid, StreamID}, {set_options, #{chunked => false}}},
	Req = cowboy_req:stream_reply(200, Req0),
	cowboy_req:stream_body(<<0:8000000>>, fin, Req),
	{ok, Req, State};
set_options(<<"chunked_false_ignored">>, Req0, State) ->
	%% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
	#{pid := Pid, streamid := StreamID} = Req0,
	Pid ! {{Pid, StreamID}, {set_options, #{chunked => false}}},
	Req = cowboy_req:reply(200, #{}, <<"Hello world!">>, Req0),
	{ok, Req, State};
set_options(<<"idle_timeout_short">>, Req0, State) ->
	%% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
	#{pid := Pid, streamid := StreamID} = Req0,
	Pid ! {{Pid, StreamID}, {set_options, #{idle_timeout => 500}}},
	{_, Body, Req} = cowboy_req:read_body(Req0),
	{ok, cowboy_req:reply(200, #{}, Body, Req), State};
set_options(<<"idle_timeout_long">>, Req0, State) ->
	%% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
	#{pid := Pid, streamid := StreamID} = Req0,
	Pid ! {{Pid, StreamID}, {set_options, #{idle_timeout => 60000}}},
	{_, Body, Req} = cowboy_req:read_body(Req0),
	{ok, cowboy_req:reply(200, #{}, Body, Req), State};
set_options(<<"metrics_user_data">>, Req, State) ->
	%% @todo This should be replaced by a cowboy_req:cast/cowboy_stream:cast.
	#{pid := Pid, streamid := StreamID} = Req,
	Pid ! {{Pid, StreamID}, {set_options, #{metrics_user_data => #{handler => ?MODULE}}}},
	{ok, cowboy_req:reply(200, #{}, <<"Hello world!">>, Req), State}.