2021-12-24 06:25:51 +03:00
|
|
|
module SetCookieTest exposing (all)
|
|
|
|
|
|
|
|
import Expect
|
2023-05-30 01:03:19 +03:00
|
|
|
import FatalError
|
2022-01-28 03:03:42 +03:00
|
|
|
import Server.SetCookie exposing (..)
|
2021-12-24 06:25:51 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
import Time
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
describe "SetCookie"
|
|
|
|
[ test "simple value" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "sessionId" "38afes7a8"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "sessionId=38afes7a8"
|
|
|
|
, test "with expiration" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2021-12-24 06:25:51 +03:00
|
|
|
|> withExpiration (Time.millisToPosix 1445412480000)
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT"
|
|
|
|
, test "http-only, multiple values" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2021-12-24 06:25:51 +03:00
|
|
|
|> withExpiration (Time.millisToPosix 1445412480000)
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; HttpOnly"
|
|
|
|
, test "immediate expiration" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2021-12-24 06:25:51 +03:00
|
|
|
|> withImmediateExpiration
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
|
|
|
|
, test "with path" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2021-12-24 06:25:51 +03:00
|
|
|
|> withPath "/"
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Path=/"
|
|
|
|
, test "with max-age" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2021-12-24 06:25:51 +03:00
|
|
|
|> withMaxAge 123
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 06:25:51 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Max-Age=123"
|
2021-12-24 06:44:52 +03:00
|
|
|
, test "encodes values" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "This needs encoding & it uses url encoding"
|
2021-12-24 06:44:52 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=This%20needs%20encoding%20%26%20it%20uses%20url%20encoding"
|
2021-12-24 07:04:49 +03:00
|
|
|
, test "with domain" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:04:49 +03:00
|
|
|
|> nonSecure
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2021-12-24 07:04:49 +03:00
|
|
|
|> withDomain "example.com"
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 07:04:49 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Domain=example.com"
|
|
|
|
, test "secure" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 07:04:49 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; Secure"
|
2021-12-24 07:14:15 +03:00
|
|
|
, test "SameSite" <|
|
|
|
|
\() ->
|
2023-05-25 01:05:04 +03:00
|
|
|
options
|
2021-12-24 07:14:15 +03:00
|
|
|
|> nonSecure
|
2022-01-28 03:03:42 +03:00
|
|
|
|> withSameSite Strict
|
2023-03-24 22:07:45 +03:00
|
|
|
|> withoutPath
|
2022-10-06 17:56:14 +03:00
|
|
|
|> makeVisibleToJavaScript
|
2022-10-06 01:30:29 +03:00
|
|
|
|> setCookie "id" "a3fWa"
|
2021-12-24 07:14:15 +03:00
|
|
|
|> toString
|
|
|
|
|> Expect.equal "id=a3fWa; SameSite=Strict"
|
2021-12-24 06:25:51 +03:00
|
|
|
]
|