1 | module Pact.WAI.Response
4 | import Pact.WAI.Header
5 | import Pact.WAI.StatusCode
7 | import Data.SortedMap as Map
9 | import Derive.Prelude
11 | %language ElabReflection
14 | record Response where
15 | constructor MkResponse
20 | %runElab derive "Response" [Show]
25 | notFoundResponse : Response
26 | notFoundResponse = MkResponse notFound emptyHeaders Nothing
31 | badRequestResponse : String -> Response
32 | badRequestResponse body = MkResponse badRequest emptyHeaders (Just body)
37 | renderHeaders : Headers -> String
38 | renderHeaders headers =
39 | let headersList = Map.toList headers
40 | in Data.String.joinBy "\r\n" . map (\(k, v) => "\{k}: \{v}" ) $
headersList
46 | renderResponse : Response -> String
47 | renderResponse (MkResponse status headers body) =
48 | let statusLine = "HTTP/1.1 \{status} \{statusMessage status}\r\n"
49 | bodyStr = maybe "" id body
50 | in statusLine ++ renderHeaders headers ++ "\r\n\r\n" ++ bodyStr
53 | testResponse : Response
54 | testResponse = MkResponse ok (fromList [("Content-Type", "text/plain"), ("Content-Length", "12")]) (Just "Hello, World!")
56 | test = renderResponse testResponse