Skip to content

Function to parse JSON in yesod-test #1643

@MaxGabriel

Description

@MaxGabriel

At work we have a function to parse a response in yesod-test to a JSON value. This seemed pretty widely helpful to me, would you want such a function in yesod-test?

-- | Parses the response body from JSON and returns it.
requireJsonParse :: FromJSON a => YesodExample App a
requireJsonParse = do
    YT.withResponse $ \ (SResponse _status headers body) -> do
        unless
            ((hContentType, "application/json; charset=utf-8") `elem` headers)
            (failure $ T.pack $ "Expected `Content-Type: application/json; charset=utf-8` in the headers, got: " ++ show headers)
        case eitherDecode' body of
            Left err -> failure $ T.concat ["Failed to parse JSON response; error: ", T.pack err]
            Right v -> return v

It might fit more into the existing naming schemes of yesod-test if it was named assertJsonParse (or assertJSONParse) and took a string parameter for the assertion name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions