From 87d0bfff926892d2dc0a55a3dc45d8c5f8a682f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 11 Mar 2020 19:45:16 +0100 Subject: Make Gun use the cookie store when configured to --- test/handlers/cookie_echo_h.erl | 11 ++++++++++ test/handlers/cookie_parser_h.erl | 23 ++++++++++++++++++++ test/handlers/cookie_parser_result_h.erl | 25 ++++++++++++++++++++++ test/handlers/cookie_set_h.erl | 36 ++++++++++++++++++++++++++++++++ test/handlers/ws_cookie_h.erl | 24 +++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 test/handlers/cookie_echo_h.erl create mode 100644 test/handlers/cookie_parser_h.erl create mode 100644 test/handlers/cookie_parser_result_h.erl create mode 100644 test/handlers/cookie_set_h.erl create mode 100644 test/handlers/ws_cookie_h.erl (limited to 'test/handlers') 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}. -- cgit v1.2.3