From f017f8a0ecbffd5033d9ab49bf180186f7a523a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 11 Mar 2020 19:42:57 +0100 Subject: Add cow_cookie:cookie/1 --- src/cow_cookie.erl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src') diff --git a/src/cow_cookie.erl b/src/cow_cookie.erl index d1ffbc3..1f6d41b 100644 --- a/src/cow_cookie.erl +++ b/src/cow_cookie.erl @@ -16,6 +16,7 @@ -export([parse_cookie/1]). -export([parse_set_cookie/1]). +-export([cookie/1]). -export([setcookie/3]). -type cookie_attrs() :: #{ @@ -285,12 +286,41 @@ parse_set_cookie_test_() -> || {SetCookie, Res} <- Tests]. -endif. +%% Build a cookie header. + +-spec cookie([{iodata(), iodata()}]) -> iolist(). +cookie([]) -> + []; +cookie([{<<>>, Value}]) -> + [Value]; +cookie([{Name, Value}]) -> + [Name, $=, Value]; +cookie([{<<>>, Value}|Tail]) -> + [Value, $;, $\s|cookie(Tail)]; +cookie([{Name, Value}|Tail]) -> + [Name, $=, Value, $;, $\s|cookie(Tail)]. + +-ifdef(TEST). +cookie_test_() -> + Tests = [ + {[], <<>>}, + {[{<<"a">>, <<"b">>}], <<"a=b">>}, + {[{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}], <<"a=b; c=d">>}, + {[{<<>>, <<"b">>}, {<<"c">>, <<"d">>}], <<"b; c=d">>}, + {[{<<"a">>, <<"b">>}, {<<>>, <<"d">>}], <<"a=b; d">>} + ], + [{Res, fun() -> Res = iolist_to_binary(cookie(Cookies)) end} + || {Cookies, Res} <- Tests]. +-endif. + %% Convert a cookie name, value and options to its iodata form. %% %% Initially from Mochiweb: %% * Copyright 2007 Mochi Media, Inc. %% Initial binary implementation: %% * Copyright 2011 Thomas Burdick +%% +%% @todo Rename the function to set_cookie eventually. -spec setcookie(iodata(), iodata(), cookie_opts()) -> iolist(). setcookie(Name, Value, Opts) -> -- cgit v1.2.3