aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2020-03-11 19:45:16 +0100
committerLoïc Hoguin <[email protected]>2020-03-12 18:08:16 +0100
commit87d0bfff926892d2dc0a55a3dc45d8c5f8a682f3 (patch)
tree5b381d3d2c2f691699baeddad78c781026d237b7 /test/handlers
parent04790d3a281a42fbd65c9a44fe88f437cfe025f3 (diff)
downloadgun-87d0bfff926892d2dc0a55a3dc45d8c5f8a682f3.tar.gz
gun-87d0bfff926892d2dc0a55a3dc45d8c5f8a682f3.tar.bz2
gun-87d0bfff926892d2dc0a55a3dc45d8c5f8a682f3.zip
Make Gun use the cookie store when configured to
Diffstat (limited to 'test/handlers')
-rw-r--r--test/handlers/cookie_echo_h.erl11
-rw-r--r--test/handlers/cookie_parser_h.erl23
-rw-r--r--test/handlers/cookie_parser_result_h.erl25
-rw-r--r--test/handlers/cookie_set_h.erl36
-rw-r--r--test/handlers/ws_cookie_h.erl24
5 files changed, 119 insertions, 0 deletions
diff --git a/test/handlers/cookie_echo_h.erl b/test/handlers/cookie_echo_h.erl
new file mode 100644
index 0000000..28c5dde
--- /dev/null
+++ b/test/handlers/cookie_echo_h.erl
@@ -0,0 +1,11 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie_echo_h).
+
+-export([init/2]).
+
+init(Req, State) ->
+ {ok, cowboy_req:reply(200,
+ #{<<"content-type">> => <<"text/plain">>},
+ cowboy_req:header(<<"cookie">>, Req, <<"UNDEF">>),
+ Req), State}.
diff --git a/test/handlers/cookie_parser_h.erl b/test/handlers/cookie_parser_h.erl
new file mode 100644
index 0000000..cff5901
--- /dev/null
+++ b/test/handlers/cookie_parser_h.erl
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie_parser_h).
+
+-export([init/2]).
+
+init(Req0=#{qs := Qs}, State) ->
+ %% Hardcoded path, but I doubt it's going to break anytime soon.
+ TestFile = iolist_to_binary(["../../test/wpt/cookies/", Qs, "-test"]),
+ {ok, Test} = file:read_file(TestFile),
+ %% We don't want the final empty line.
+ Lines = lists:reverse(tl(lists:reverse(string:split(Test, <<"\n">>, all)))),
+ Req = lists:foldl(fun
+ (<<"Set-Cookie: ",SetCookie/bits>>, Req1) ->
+ %% We do not use set_resp_cookie because we want to preserve ordering.
+ SetCookieList = cowboy_req:resp_header(<<"set-cookie">>, Req1, []),
+ cowboy_req:set_resp_header(<<"set-cookie">>, SetCookieList ++ [SetCookie], Req1);
+ (<<"Set-Cookie:">>, Req1) ->
+ Req1;
+ (<<"Location: ",Location/bits>>, Req1) ->
+ cowboy_req:set_resp_header(<<"location">>, Location, Req1)
+ end, Req0, Lines),
+ {ok, cowboy_req:reply(204, Req), State}.
diff --git a/test/handlers/cookie_parser_result_h.erl b/test/handlers/cookie_parser_result_h.erl
new file mode 100644
index 0000000..a1fa899
--- /dev/null
+++ b/test/handlers/cookie_parser_result_h.erl
@@ -0,0 +1,25 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie_parser_result_h).
+
+-export([init/2]).
+
+init(Req=#{qs := Qs}, State) ->
+ %% Hardcoded path, but I doubt it's going to break anytime soon.
+ ExpectedFile = iolist_to_binary(["../../test/wpt/cookies/", Qs, "-expected"]),
+ CookieHd = cowboy_req:header(<<"cookie">>, Req),
+ case file:read_file(ExpectedFile) of
+ {ok, Expected} when Expected =:= <<>>; Expected =:= <<"\n">> ->
+ undefined = CookieHd,
+ ok;
+ {ok, <<"Cookie: ",CookiesBin0/bits>>} ->
+ %% We only care about the first line.
+ [CookiesBin, <<>>|_] = string:split(CookiesBin0, <<"\n">>, all),
+ CookiesBin = CookieHd,
+ ok
+ end,
+ %% We echo back the cookie header in order to log it.
+ {ok, cowboy_req:reply(204, case CookieHd of
+ undefined -> #{<<"x-no-cookie-received">> => <<"Cookie header missing.">>};
+ _ -> #{<<"x-cookie-received">> => CookieHd}
+ end, Req), State}.
diff --git a/test/handlers/cookie_set_h.erl b/test/handlers/cookie_set_h.erl
new file mode 100644
index 0000000..29ff351
--- /dev/null
+++ b/test/handlers/cookie_set_h.erl
@@ -0,0 +1,36 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie_set_h).
+
+-export([init/2]).
+
+init(Req0, State) ->
+ SetCookieList = set_cookie_list(Req0),
+ Req = cowboy_req:set_resp_header(<<"set-cookie">>, SetCookieList, Req0),
+ {ok, cowboy_req:reply(204, Req), State}.
+
+-define(HOST, "web-platform.test").
+
+set_cookie_list(#{qs := <<"domain_with_and_without_leading_period">>}) ->
+ [
+ <<"a=b; Path=/; Domain=." ?HOST>>,
+ <<"a=c; Path=/; Domain=" ?HOST>>
+ ];
+set_cookie_list(#{qs := <<"domain_with_leading_period">>}) ->
+ [<<"a=b; Path=/; Domain=." ?HOST>>];
+set_cookie_list(#{qs := <<"domain_matches_host">>}) ->
+ [<<"a=b; Path=/; Domain=" ?HOST>>];
+set_cookie_list(#{qs := <<"domain_missing">>}) ->
+ [<<"a=b; Path=/;">>];
+set_cookie_list(#{qs := <<"path_default">>}) ->
+ [<<"cookie-path-default=1">>];
+set_cookie_list(#{qs := <<"path_default_expire">>}) ->
+ [<<"cookie-path-default=1; Max-Age=0">>];
+set_cookie_list(#{qs := <<"path=",Path/bits>>}) ->
+ [[<<"a=b; Path=">>, Path]];
+set_cookie_list(Req=#{qs := <<"prefix">>}) ->
+ [cowboy_req:header(<<"please-set-cookie">>, Req)];
+set_cookie_list(#{qs := <<"secure_http">>}) ->
+ [<<"secure_from_nonsecure_http=1; Secure; Path=/">>];
+set_cookie_list(#{qs := <<"secure_https">>}) ->
+ [<<"secure_from_secure_http=1; Secure; Path=/">>].
diff --git a/test/handlers/ws_cookie_h.erl b/test/handlers/ws_cookie_h.erl
new file mode 100644
index 0000000..39889b3
--- /dev/null
+++ b/test/handlers/ws_cookie_h.erl
@@ -0,0 +1,24 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(ws_cookie_h).
+
+-export([init/2]).
+-export([websocket_handle/2]).
+-export([websocket_info/2]).
+
+init(Req0, _) ->
+ Req = cowboy_req:set_resp_header(<<"set-cookie">>,
+ [<<"ws_cookie=1; Secure; path=/">>], Req0),
+ {cowboy_websocket, Req, undefined, #{
+ compress => true
+ }}.
+
+websocket_handle({text, Data}, State) ->
+ {[{text, Data}], State};
+websocket_handle({binary, Data}, State) ->
+ {[{binary, Data}], State};
+websocket_handle(_Frame, State) ->
+ {[], State}.
+
+websocket_info(_Info, State) ->
+ {[], State}.