Fix hardcoded value in non-matching expectFormPost message.

This commit is contained in:
Dillon Kearns 2022-04-07 09:24:32 -07:00
parent ad22e15393
commit 8ce0e08efa
2 changed files with 34 additions and 18 deletions

View File

@ -887,25 +887,25 @@ expectFormPost toForm =
map4 (\parsedContentType a b c -> ( ( a, parsedContentType ), b, c ))
(rawContentType |> map (Maybe.map parseContentType))
(matchesContentType "application/x-www-form-urlencoded")
(matchesMethod ( Post, [] ))
(rawBody
|> map (Maybe.withDefault "")
--|> andThen
-- (\maybeBody ->
-- maybeBody
-- |> Result.fromMaybe "Expected a form POST but this HTTP request has no body."
-- |> fromResult
-- )
method
(rawBody |> map (Maybe.withDefault "")
-- TODO warn of empty body in case when field decoding fails?
)
|> andThen
(\( ( validContentType, parsedContentType ), validMethod, justBody ) ->
if not ((validContentType |> Maybe.withDefault False) && validMethod) then
if not ((validContentType |> Maybe.withDefault False) && validMethod == Post) then
Json.Decode.succeed
( Err
(NotFormPost
{ method = Just Get
, contentType = parsedContentType
}
(ValidationError <|
case ( validContentType |> Maybe.withDefault False, validMethod == Post, parsedContentType ) of
( False, True, Just contentType_ ) ->
"expectFormPost did not match - Was form POST but expected content-type `application/x-www-form-urlencoded` and instead got `" ++ contentType_ ++ "`"
( False, True, 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"
_ ->
"expectFormPost did not match - expected method POST, but the method was " ++ methodToString validMethod
)
, []
)

View File

@ -54,8 +54,7 @@ all =
]
, body = Nothing
}
"""Did not match formPost because
- Form post must have method POST, but the method was GET"""
"expectFormPost did not match - expected method POST, but the method was GET"
, test "tries multiple form post formats" <|
\() ->
Request.oneOf
@ -81,6 +80,24 @@ all =
(Dict.fromList [ ( "foo", NonEmpty.singleton "bar" ) ])
)
}
, test "expectFormPost with missing content-type" <|
\() ->
Request.expectFormPost
(\{ field } ->
field "bar"
)
|> expectNoMatch
{ method = Request.Post
, headers =
[ ( "content_type", "application/x-www-form-urlencoded" )
]
, body =
Just
(FormData
(Dict.fromList [ ( "foo", NonEmpty.singleton "bar" ) ])
)
}
"""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
@ -104,8 +121,7 @@ all =
}
"""Server.Request.oneOf failed in the following 4 ways:
(1) Did not match formPost because
- Form post must have method POST, but the method was GET
(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