Update examples.

This commit is contained in:
Dillon Kearns 2023-05-29 15:03:19 -07:00
parent 5eb05ae128
commit e91fd307fd
12 changed files with 24 additions and 346 deletions

View File

@ -18,7 +18,7 @@
},
"../..": {
"name": "elm-pages",
"version": "3.0.0-beta.40",
"version": "3.0.0-beta.41",
"dev": true,
"license": "BSD-3-Clause",
"dependencies": {
@ -55,7 +55,7 @@
"@types/micromatch": "^4.0.2",
"@types/node": "^20.1.0",
"@types/serve-static": "^1.15.1",
"cypress": "^12.11.0",
"cypress": "^12.13.0",
"elm-codegen": "^0.3.0",
"elm-optimize-level-2": "^0.3.5",
"elm-review": "^2.10.2",

View File

@ -18,7 +18,7 @@
},
"../..": {
"name": "elm-pages",
"version": "3.0.0-beta.40",
"version": "3.0.0-beta.41",
"dev": true,
"license": "BSD-3-Clause",
"dependencies": {
@ -55,7 +55,7 @@
"@types/micromatch": "^4.0.2",
"@types/node": "^20.1.0",
"@types/serve-static": "^1.15.1",
"cypress": "^12.11.0",
"cypress": "^12.13.0",
"elm-codegen": "^0.3.0",
"elm-optimize-level-2": "^0.3.5",
"elm-review": "^2.10.2",

View File

@ -18,7 +18,7 @@
},
"../..": {
"name": "elm-pages",
"version": "3.0.0-beta.40",
"version": "3.0.0-beta.41",
"dev": true,
"license": "BSD-3-Clause",
"dependencies": {
@ -55,7 +55,7 @@
"@types/micromatch": "^4.0.2",
"@types/node": "^20.1.0",
"@types/serve-static": "^1.15.1",
"cypress": "^12.11.0",
"cypress": "^12.13.0",
"elm-codegen": "^0.3.0",
"elm-optimize-level-2": "^0.3.5",
"elm-review": "^2.10.2",

View File

@ -3,9 +3,9 @@ module ApiRouteTests exposing (all)
import ApiRoute exposing (..)
import BackendTask
import Expect
import FatalError
import Internal.ApiRoute exposing (tryMatch, withRoutes)
import Pattern exposing (Pattern(..))
import Server.Request
import Server.Response
import Test exposing (Test, describe, test)
@ -112,13 +112,10 @@ all =
[ test "no dynamic segments" <|
\() ->
succeed
(Server.Request.succeed ()
|> Server.Request.map
(\() ->
""
|> Server.Response.plainText
|> BackendTask.succeed
)
(\request ->
""
|> Server.Response.plainText
|> BackendTask.succeed
)
|> literal "no-dynamic-segments.json"
|> serverRender
@ -127,13 +124,10 @@ all =
, test "two literal segments" <|
\() ->
succeed
(Server.Request.succeed ()
|> Server.Request.map
(\() ->
""
|> Server.Response.plainText
|> BackendTask.succeed
)
(\request ->
""
|> Server.Response.plainText
|> BackendTask.succeed
)
|> literal "api"
|> slash
@ -180,11 +174,8 @@ all =
\() ->
succeed
(\repo ->
Server.Request.succeed ()
|> Server.Request.map
(\() ->
BackendTask.succeed ("Data for repo " ++ repo |> Server.Response.plainText)
)
\request ->
BackendTask.succeed ("Data for repo " ++ repo |> Server.Response.plainText)
)
|> literal "api"
|> slash

View File

@ -3,6 +3,7 @@ module CookieTest exposing (all)
import CookieParser
import Dict
import Expect
import FatalError
import Test exposing (Test, describe, test)

View File

@ -2,6 +2,7 @@ module FormDataTest exposing (all)
import Dict
import Expect
import FatalError
import FormData
import Test exposing (Test, describe, test)

View File

@ -1,6 +1,7 @@
module HeadTests exposing (suite)
import Expect
import FatalError
import Head
import Json.Decode
import Test exposing (Test, describe, test)

View File

@ -6,6 +6,7 @@ import Elm.CodeGen
import Elm.Pretty
import Elm.ToString
import Expect exposing (Expectation)
import FatalError
import Fuzz exposing (Fuzzer)
import Pages.Internal.RoutePattern as RoutePattern
import Pretty

View File

@ -1,6 +1,7 @@
module PathTests exposing (all)
import Expect
import FatalError
import Test exposing (Test, describe, test)
import UrlPath

View File

@ -1,6 +1,7 @@
module RouteTests exposing (all)
import Expect
import FatalError
import List.Extra
import Regex
import Test exposing (Test, describe, test)

View File

@ -1,320 +0,0 @@
module ServerRequestTest exposing (all)
import Dict exposing (Dict)
import Expect exposing (Expectation)
import Form
import Form.Field as Field
import Form.Handler
import Form.Validation as Validation
import FormData
import Internal.Request exposing (Parser(..))
import Json.Decode as Decode
import Json.Encode
import Server.Request as Request
import Test exposing (Test, describe, test)
all : Test
all =
describe "Server.Request matching"
[ test "succeed always matches" <|
\() ->
Request.succeed ()
|> expectMatch
{ method = Request.Get
, headers = []
, body = Nothing
, urlQueryString = Nothing
}
, test "accept GET" <|
\() ->
Request.succeed ()
|> Request.acceptMethod ( Request.Get, [] )
|> expectMatch
{ method = Request.Get
, headers = []
, body = Nothing
, urlQueryString = Nothing
}
, test "accept GET doesn't match POST" <|
\() ->
Request.succeed ()
|> Request.acceptMethod ( Request.Post, [] )
|> expectNoMatch
{ method = Request.Get
, headers = []
, body = Nothing
, urlQueryString = Nothing
}
"Expected HTTP method POST but was GET"
, test "formData extracts fields from query params for GET" <|
\() ->
Request.rawFormData
|> Request.map
(\formData ->
formData
)
|> expectMatchWith
{ method = Request.Get
, headers = []
, body = Nothing
, urlQueryString = Just "q=hello"
}
[ ( "q", "hello" ) ]
, test "tries multiple form post formats" <|
\() ->
Request.formData
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
|> Validation.andMap bar
, view =
\_ -> ()
}
)
|> Form.field "bar" Field.text
|> Form.Handler.init identity
|> Form.Handler.with identity
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
|> Validation.andMap bar
, view =
\_ -> ()
}
)
|> Form.field "foo" Field.text
)
)
|> expectMatch
{ method = Request.Post
, headers =
[ ( "content-type", "application/x-www-form-urlencoded" )
]
, body =
Just
(FormData
(Dict.fromList [ ( "foo", ( "bar", [] ) ) ])
)
, urlQueryString = Nothing
}
, test "expectFormPost with missing content-type" <|
\() ->
Request.formData
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
|> Validation.andMap bar
, view =
\_ -> ()
}
)
|> Form.field "bar" Field.text
|> Form.Handler.init identity
)
|> expectNoMatch
{ method = Request.Post
, headers =
[ ( "content_type", "application/x-www-form-urlencoded" )
]
, body =
Just
(FormData
(Dict.fromList [ ( "foo", ( "bar", [] ) ) ])
)
, urlQueryString = Nothing
}
"""expectFormPost did not match - Was form POST but expected content-type `application/x-www-form-urlencoded` but the request didn't have a content-type header"""
-- , test "one of no match" <|
-- \() ->
-- Request.oneOf
-- [ --Request.formParserResultNew
-- -- [ Form.init
-- -- (\bar ->
-- -- Validation.succeed identity
-- -- |> Validation.andMap bar
-- -- )
-- -- (\_ _ -> ())
-- -- |> Form.field "first" Field.text
-- -- ],
-- Request.expectJsonBody (Decode.field "first" Decode.string)
-- , Request.expectQueryParam "first"
-- , Request.expectMultiPartFormPost
-- (\{ field } ->
-- field "first"
-- )
-- ]
-- |> expectNoMatch
-- { method = Request.Get
-- , headers =
-- [ ( "content-type", "application/x-www-form-urlencoded" )
-- ]
-- , body = Nothing
-- }
-- """Server.Request.oneOf failed in the following 4 ways:
--
--(1) expectFormPost did not match - expected method POST, but the method was GET
--
--(2) Expected content-type to be application/json but it was application/x-www-form-urlencoded
--
--(3) Internal error - expected rawUrl field but the adapter script didn't provide one.
--
--(4) Expected content-type to be multipart/form-data but it was application/x-www-form-urlencoded
--Expected HTTP method POST but was GET"""
]
type alias Request =
{ method : Request.Method
, headers : List ( String, String )
, body : Maybe Body
, urlQueryString : Maybe String
}
type Body
= FormData (Dict String ( String, List String ))
| JsonBody Decode.Value
| StringBody String
expectMatch : Request -> Request.Parser value -> Expectation
expectMatch request (Parser decoder) =
case
request
|> requestToJson
|> Decode.decodeValue decoder
of
Ok ok ->
case ok of
( Ok _, [] ) ->
Expect.pass
( Err innerError, otherErrors ) ->
(innerError :: otherErrors)
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.fail
( Ok _, nonEmptyErrors ) ->
nonEmptyErrors
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.fail
Err error ->
Expect.fail (Decode.errorToString error)
expectMatchWith : Request -> value -> Request.Parser value -> Expectation
expectMatchWith request expected (Parser decoder) =
case
request
|> requestToJson
|> Decode.decodeValue decoder
of
Ok ok ->
case ok of
( Ok actual, [] ) ->
actual
|> Expect.equal expected
( Err innerError, otherErrors ) ->
(innerError :: otherErrors)
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.fail
( Ok _, nonEmptyErrors ) ->
nonEmptyErrors
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.fail
Err error ->
Expect.fail (Decode.errorToString error)
expectNoMatch : Request -> String -> Request.Parser value -> Expectation
expectNoMatch request expectedErrorString (Parser decoder) =
case
request
|> requestToJson
|> Decode.decodeValue decoder
of
Ok ok ->
case ok of
( Ok _, [] ) ->
Expect.fail "Expected this request not to match, but instead it did match."
( Err innerError, otherErrors ) ->
(innerError :: otherErrors)
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.equal expectedErrorString
( Ok _, nonEmptyErrors ) ->
nonEmptyErrors
|> List.map Request.errorToString
|> String.join "\n"
|> Expect.equal expectedErrorString
Err error ->
Expect.fail
("Expected this request to not match, but instead there was an internal error: "
++ Decode.errorToString error
)
requestToJson : Request -> Json.Encode.Value
requestToJson request =
Json.Encode.object
[ ( "method"
, request.method
|> Request.methodToString
|> Json.Encode.string
)
, ( "headers"
, Json.Encode.object
(List.map
(Tuple.mapSecond
Json.Encode.string
)
request.headers
)
)
, ( "body"
, request.body
|> Maybe.map encodeBody
|> Maybe.withDefault Json.Encode.null
)
, ( "query"
, Json.Encode.object
[ ( "q", Json.Encode.string "hello" )
]
)
, ( "rawUrl"
, Json.Encode.string
("http://localhost:1234/"
++ (request.urlQueryString |> Maybe.map (\q -> "?" ++ q) |> Maybe.withDefault "")
)
)
, ( "multiPartFormData", Json.Encode.null )
]
encodeBody : Body -> Decode.Value
encodeBody body =
case body of
JsonBody json ->
json
FormData formData ->
formData |> FormData.encode |> Json.Encode.string
StringBody string ->
string |> Json.Encode.string

View File

@ -1,6 +1,7 @@
module SetCookieTest exposing (all)
import Expect
import FatalError
import Server.SetCookie exposing (..)
import Test exposing (Test, describe, test)
import Time