Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cakekindel committed Sep 30, 2023
1 parent 0a3ff07 commit 182094a
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 53 deletions.
5 changes: 3 additions & 2 deletions docs/Examples/AsyncResponse/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve)
import Node.Encoding (Encoding(UTF8))
import Node.FS.Aff (readTextFile)
import Routing.Duplex as RD
Expand All @@ -24,7 +25,7 @@ route = RD.root $ RG.sum
filePath :: String
filePath = "./docs/Examples/AsyncResponse/Hello"

router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { route: SayHello } = readTextFile UTF8 filePath >>= ok

-- | Boot up the server
Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/BinaryRequest/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve, toBuffer)
import HTTPurple (Request, Response, ServerM, ok, serve, toBuffer)
import Node.Buffer (Buffer)
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
Expand All @@ -22,7 +23,7 @@ route = RD.root $ RG.sum
foreign import sha256sum :: Buffer -> String

-- | Respond with file's sha256sum
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { body } = toBuffer body >>= sha256sum >>> ok

-- | Boot up the server
Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/BinaryResponse/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseHeaders, ResponseM, ServerM, header, ok', serve)
import HTTPurple (Request, ResponseHeaders, Response, ServerM, header, ok', serve)
import Node.FS.Aff (readFile)
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
Expand All @@ -27,7 +28,7 @@ responseHeaders :: ResponseHeaders
responseHeaders = header "Content-Type" "image/png"

-- | Respond with image data when run
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router = const $ readFile filePath >>= ok' responseHeaders

-- | Boot up the server
Expand Down
4 changes: 2 additions & 2 deletions docs/Examples/Chunked/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve)
import Node.ChildProcess (defaultSpawnOptions, spawn, stdout)
import Node.Stream (Readable)
import Routing.Duplex as RD
Expand All @@ -28,7 +28,7 @@ runScript script =
liftEffect $ stdout <$> spawn "sh" [ "-c", script ] defaultSpawnOptions

-- | Say 'hello world!' in chunks when run
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= ok

-- | Boot up the server
Expand Down
17 changes: 3 additions & 14 deletions docs/Examples/CustomStack/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ module Examples.CustomStack.Main where

import Prelude

import Control.Monad.Reader (class MonadAsk, ReaderT, asks, runReaderT)
import Control.Monad.Reader (class MonadAsk, asks, runReaderT)
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Aff.Class (class MonadAff)
import Effect.Console (log)
import HTTPurple (Request, Response, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve')
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG

Expand All @@ -24,15 +22,6 @@ route = RD.root $ RG.sum
-- | A type to hold the environment for our ReaderT
type Env = { name :: String }

-- | A middleware that introduces a ReaderT
readerMiddleware ::
forall route.
(Request route -> ReaderT Env Aff Response) ->
Request route ->
ResponseM
readerMiddleware router request = do
runReaderT (router request) { name: "joe" }

-- | Say 'hello, joe' when run
sayHello :: forall m. MonadAff m => MonadAsk Env m => Request Route -> m Response
sayHello _ = do
Expand All @@ -42,7 +31,7 @@ sayHello _ = do
-- | Boot up the server
main :: ServerM
main =
serve { hostname: "localhost", port: 8080, onStarted } { route, router: readerMiddleware sayHello }
serve' (\a -> runReaderT a {name: "joe"}) { hostname: "localhost", port: 8080, onStarted } { route, router: sayHello }
where
onStarted = do
log " ┌───────────────────────────────────────┐"
Expand Down
7 changes: 4 additions & 3 deletions docs/Examples/ExtensibleMiddleware/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import Data.Generic.Rep (class Generic)
import Data.JSDate (JSDate)
import Data.JSDate as JSDate
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPurple (ExtRequest, Middleware, Request, RequestR, ResponseM, ServerM, ok, serve)
import HTTPurple (ExtRequest, Middleware, Request, RequestR, Response, ServerM, ok, serve)
import HTTPurple as Headers
import Prim.Row (class Nub, class Union)
import Record (merge)
Expand Down Expand Up @@ -43,12 +44,12 @@ sayHelloRoute = RD.root $ RG.sum
}

-- | Say 'hello <USER>' when run with X-Token, otherwise 'hello anonymous'
sayHello :: ExtRequest SayHello (user :: Maybe String, time :: JSDate) -> ResponseM
sayHello :: ExtRequest SayHello (user :: Maybe String, time :: JSDate) -> Aff Response
sayHello { user: Just user, time } = ok $ "hello " <> user <> ", it is " <> JSDate.toDateString time <> " " <> JSDate.toTimeString time
sayHello { user: Nothing, time } = ok $ "hello " <> "anonymous, it is " <> JSDate.toDateString time <> " " <> JSDate.toTimeString time

-- | The stack of middlewares to use for the server
middlewareStack :: forall route. (ExtRequest route (user :: Maybe String, time :: JSDate) -> ResponseM) -> Request route -> ResponseM
middlewareStack :: forall route. (ExtRequest route (user :: Maybe String, time :: JSDate) -> Aff Response) -> Request route -> Aff Response
middlewareStack = authenticator <<< requestTime

-- | Boot up the server
Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/Headers/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module Examples.Headers.Main where
import Prelude

import Data.Generic.Rep (class Generic)
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseHeaders, ResponseM, ServerM, ok', serve, (!@))
import HTTPurple (Request, ResponseHeaders, Response, ServerM, ok', serve, (!@))
import HTTPurple.Headers (headers)
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
Expand All @@ -26,7 +27,7 @@ responseHeaders = headers
}

-- | Route to the correct handler
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { headers } = ok' responseHeaders $ headers !@ "X-Input"

-- | Boot up the server
Expand Down
19 changes: 10 additions & 9 deletions docs/Examples/Middleware/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Prelude hiding ((/))
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import HTTPurple (type (<+>), Request, ResponseM, ServerM, fullPath, header, ok, ok', serve, (<+>))
import HTTPurple (type (<+>), Request, Response, ServerM, fullPath, header, ok, ok', serve, (<+>))
import Record as Record
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
Expand Down Expand Up @@ -35,9 +36,9 @@ sayHelloRoute = RD.root $ RG.sum
-- | A middleware that logs at the beginning and end of each request
loggingMiddleware ::
forall route.
(Request route -> ResponseM) ->
(Request route -> Aff Response) ->
Request route ->
ResponseM
Aff Response
loggingMiddleware router request = do
liftEffect $ log $ "Request starting for " <> path
response <- router request
Expand All @@ -50,9 +51,9 @@ loggingMiddleware router request = do
-- | wasn't already in the response
headerMiddleware ::
forall route.
(Request route -> ResponseM) ->
(Request route -> Aff Response) ->
Request route ->
ResponseM
Aff Response
headerMiddleware router request = do
response@{ headers } <- router request
pure $ response { headers = header' <> headers }
Expand All @@ -63,18 +64,18 @@ headerMiddleware router request = do
-- | router when requesting /middleware
pathMiddleware ::
forall route.
(Request route -> ResponseM) ->
(Request route -> Aff Response) ->
Request (Middleware <+> route) ->
ResponseM
Aff Response
pathMiddleware _ { route: Left Middleware } = ok "Middleware!"
pathMiddleware router request@{ route: Right r } = router $ Record.set (Proxy :: _ "route") r request

-- | Say 'hello' when run, and add a default value to the X-Middleware header
sayHello :: Request SayHello -> ResponseM
sayHello :: Request SayHello -> Aff Response
sayHello _ = ok' (header "X-Middleware" "router") "hello"

-- | The stack of middlewares to use for the server
middlewareStack :: forall route. (Request route -> ResponseM) -> Request (Either Middleware route) -> ResponseM
middlewareStack :: forall route. (Request route -> Aff Response) -> Request (Either Middleware route) -> Aff Response
middlewareStack = loggingMiddleware <<< headerMiddleware <<< pathMiddleware

-- | Boot up the server
Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/MultiRoute/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude hiding ((/))

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve)
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG
Expand All @@ -22,7 +23,7 @@ route = RD.root $ RG.sum
}

-- | Specify the routes
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { route: Hello } = ok "hello"
router { route: GoodBye } = ok "goodbye"

Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/NodeMiddleware/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (ExtRequest, NodeMiddleware, NodeMiddlewareStack(..), ResponseM, ServerM, ok, serveNodeMiddleware, usingMiddleware)
import HTTPurple (ExtRequest, NodeMiddleware, NodeMiddlewareStack(..), Response, ServerM, ok, serveNodeMiddleware, usingMiddleware)
import Routing.Duplex as RD
import Routing.Duplex.Generic as RG

Expand All @@ -30,7 +31,7 @@ sayHelloRoute = RD.root $ RG.sum
}

-- | Say 'hello <USER>' when run with X-Token, otherwise 'hello anonymous'
sayHello :: ExtRequest SayHello AuthenticatorR -> ResponseM
sayHello :: ExtRequest SayHello AuthenticatorR -> Aff Response
sayHello { user } = case Nullable.toMaybe user of
Just u -> ok $ "hello " <> u
Nothing -> ok $ "hello " <> "anonymous"
Expand Down
6 changes: 3 additions & 3 deletions docs/Examples/PathSegments/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Prelude hiding ((/))

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve)
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as G
Expand All @@ -23,7 +23,7 @@ route = RD.root $ G.sum
}

-- | Specify the routes
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { route: Segment elem } = ok elem
router { route: ManySegments elems } = ok $ show elems

Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/Post/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Method(Post), Request, ResponseM, ServerM, notFound, ok, serve, toString)
import HTTPurple (Method(Post), Request, Response, ServerM, notFound, ok, serve, toString)
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as G
Expand All @@ -20,7 +21,7 @@ route = RD.root $ G.sum
}

-- | Route to the correct handler
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { body, method: Post } = toString body >>= ok
router _ = notFound

Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/QueryParameters/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Prelude
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Class.Console (log)
import HTTPurple (Request, ResponseM, ServerM, notFound, ok, serve)
import HTTPurple (Request, Response, ServerM, notFound, ok, serve)
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as G
Expand All @@ -21,7 +22,7 @@ route = RD.root $ G.sum
}

-- | Specify the routes
router :: Request Route -> ResponseM
router :: Request Route -> Aff Response
router { route: (Route { foo: true }) } = ok "foo"
router { route: (Route { bar: Just "test" }) } = ok "bar"
router { route: (Route { bar: Just _ }) } = ok ""
Expand Down
5 changes: 3 additions & 2 deletions docs/Examples/SSL/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Prelude

import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Console (log)
import HTTPurple (Request, ResponseM, ServerM, ok, serve)
import HTTPurple (Request, Response, ServerM, ok, serve)
import Routing.Duplex (RouteDuplex')
import Routing.Duplex as RD
import Routing.Duplex.Generic as G
Expand All @@ -29,7 +30,7 @@ key :: String
key = "./docs/Examples/SSL/Key.key"

-- | Say 'hello world!' when run
sayHello :: Request Route -> ResponseM
sayHello :: Request Route -> Aff Response
sayHello _ = ok "hello world!"

-- | Boot up the server
Expand Down
Loading

0 comments on commit 182094a

Please sign in to comment.