This commit is contained in:
Orion Kindel
2024-12-01 17:14:19 -06:00
parent c5cc100c29
commit 15e15aa921
2 changed files with 157 additions and 42 deletions

View File

@@ -3,7 +3,7 @@ import Path from 'path'
export const rootDir = Path.resolve(__dirname, '..') export const rootDir = Path.resolve(__dirname, '..')
export const packageDirs = async () => ['./src'] export const packageDirs = async () => ['./src', './test']
export const packageSources = async () => { export const packageSources = async () => {
const packages = await packageDirs() const packages = await packageDirs()

View File

@@ -5,7 +5,13 @@ import Prelude
import Axon.Request (Request) import Axon.Request (Request)
import Axon.Request as Request import Axon.Request as Request
import Axon.Request.Method (Method(..)) import Axon.Request.Method (Method(..))
import Axon.Request.Parts.Class (Json(..), Patch(..), Path(..), Post(..), extractRequestParts) import Axon.Request.Parts.Class
( Json(..)
, Patch(..)
, Path(..)
, Post(..)
, extractRequestParts
)
import Axon.Request.Parts.Path (type (/), IgnoreRest) import Axon.Request.Parts.Path (type (/), IgnoreRest)
import Control.Monad.Error.Class (liftEither, liftMaybe) import Control.Monad.Error.Class (liftEither, liftMaybe)
import Data.Bifunctor (lmap) import Data.Bifunctor (lmap)
@@ -28,96 +34,205 @@ import Test.Spec.Assertions (shouldEqual)
spec :: Spec Unit spec :: Spec Unit
spec = describe "Parts" do spec = describe "Parts" do
it "extracts the whole request" do it "extracts the whole request" do
req <- liftEffect $ Request.make {body: Request.BodyEmpty, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} req <- liftEffect $ Request.make
void $ extractRequestParts @Request req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") { body: Request.BodyEmpty
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
void $ extractRequestParts @Request req <#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
it "extracts method, path, JSON body" do it "extracts method, path, JSON body" do
stream <- Buffer.fromString """{"firstName": "henry"}""" UTF8 >>= Stream.readableFromBuffer # liftEffect stream <- Buffer.fromString """{"firstName": "henry"}""" UTF8
req <- liftEffect $ Request.make {body: Request.BodyReadable stream, url: URL.fromString "http://localhost:80/users/12" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: PATCH} >>= Stream.readableFromBuffer
a <- extractRequestParts @(Patch ((Path ("users" / Int) _) /\ Json {firstName :: String})) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") # liftEffect
a `shouldEqual` Patch (Path 12 /\ Json {firstName: "henry"}) req <- liftEffect $ Request.make
{ body: Request.BodyReadable stream
, url: URL.fromString "http://localhost:80/users/12" # unsafePartial
fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: PATCH
}
a <-
extractRequestParts
@(Patch ((Path ("users" / Int) _) /\ Json { firstName :: String }))
req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe
(error "was nothing")
a `shouldEqual` Patch (Path 12 /\ Json { firstName: "henry" })
describe "Path" do describe "Path" do
it "matches a route matching literal" do it "matches a route matching literal" do
req <- liftEffect $ Request.make {body: Request.BodyCachedString "foo", url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} req <- liftEffect $ Request.make
a <- extractRequestParts @(Path "foo" _) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
a <- extractRequestParts @(Path "foo" _) req <#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Path unit) a `shouldEqual` (Path unit)
it "matches a route matching multiple literals" do it "matches a route matching multiple literals" do
req <- liftEffect $ Request.make {body: Request.BodyCachedString "foo", url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} req <- liftEffect $ Request.make
a <- extractRequestParts @(Path ("foo" / "bar" / "baz") _) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial
fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
a <- extractRequestParts @(Path ("foo" / "bar" / "baz") _) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Path unit) a `shouldEqual` (Path unit)
it "does not partially match a route ..." do it "does not partially match a route ..." do
req <- liftEffect $ Request.make req <- liftEffect $ Request.make
{ body: Request.BodyCachedString "foo" { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial fromJust , url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial
fromJust
, headers: Map.empty , headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81} , address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET , method: GET
} }
a <- extractRequestParts @(Path ("foo" / "bar") _) req <#> lmap (error <<< show) >>= liftEither a <- extractRequestParts @(Path ("foo" / "bar") _) req
<#> lmap (error <<< show)
>>= liftEither
a `shouldEqual` Nothing a `shouldEqual` Nothing
it "... but does if ends in IgnoreRest" do it "... but does if ends in IgnoreRest" do
req <- liftEffect $ Request.make req <- liftEffect $ Request.make
{ body: Request.BodyCachedString "foo" { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial fromJust , url: URL.fromString "http://localhost:80/foo/bar/baz" # unsafePartial
fromJust
, headers: Map.empty , headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81} , address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET , method: GET
} }
a <- extractRequestParts @(Path ("foo" / "bar" / IgnoreRest) _) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") a <- extractRequestParts @(Path ("foo" / "bar" / IgnoreRest) _) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Path unit) a `shouldEqual` (Path unit)
it "extracts an int" do it "extracts an int" do
req <- liftEffect $ Request.make req <- liftEffect $ Request.make
{ body: Request.BodyCachedString "foo" { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo/123/bar" # unsafePartial fromJust , url: URL.fromString "http://localhost:80/foo/123/bar" # unsafePartial
fromJust
, headers: Map.empty , headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81} , address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET , method: GET
} }
a <- extractRequestParts @(Path ("foo" / Int / "bar") _) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") a <- extractRequestParts @(Path ("foo" / Int / "bar") _) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Path 123) a `shouldEqual` (Path 123)
it "extracts an int and a string" do it "extracts an int and a string" do
req <- liftEffect $ Request.make req <- liftEffect $ Request.make
{ body: Request.BodyCachedString "foo" { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo/123/bar/baz" # unsafePartial fromJust , url: URL.fromString "http://localhost:80/foo/123/bar/baz" #
unsafePartial fromJust
, headers: Map.empty , headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81} , address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET , method: GET
} }
a <- extractRequestParts @(Path ("foo" / Int / "bar" / String) _) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") a <- extractRequestParts @(Path ("foo" / Int / "bar" / String) _) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Path $ 123 /\ "baz") a `shouldEqual` (Path $ 123 /\ "baz")
describe "Body" do describe "Body" do
it "extracts a string body from a cached string" do it "extracts a string body from a cached string" do
req <- liftEffect $ Request.make {body: Request.BodyCachedString "foo", url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} req <- liftEffect $ Request.make
a <- extractRequestParts @(Either Request.BodyStringError String) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") { body: Request.BodyCachedString "foo"
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
a <- extractRequestParts @(Either Request.BodyStringError String) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Right "foo") a `shouldEqual` (Right "foo")
it "extracts a string body from a readable stream" do it "extracts a string body from a readable stream" do
stream <- Buffer.fromString "foo" UTF8 >>= Stream.readableFromBuffer # liftEffect stream <- Buffer.fromString "foo" UTF8 >>= Stream.readableFromBuffer #
req <- liftEffect $ Request.make {body: Request.BodyReadable stream, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} liftEffect
a <- extractRequestParts @(Either Request.BodyStringError String) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") req <- liftEffect $ Request.make
{ body: Request.BodyReadable stream
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
a <- extractRequestParts @(Either Request.BodyStringError String) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Right "foo") a `shouldEqual` (Right "foo")
a' <- extractRequestParts @String req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") a' <- extractRequestParts @String req <#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a' `shouldEqual` "foo" a' `shouldEqual` "foo"
it "extracts a string body from a buffer" do it "extracts a string body from a buffer" do
buf <- Buffer.fromString "foo" UTF8 # liftEffect buf <- Buffer.fromString "foo" UTF8 # liftEffect
req <- liftEffect $ Request.make {body: Request.BodyCached buf, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: GET} req <- liftEffect $ Request.make
a <- extractRequestParts @(Either Request.BodyStringError String) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") { body: Request.BodyCached buf
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: GET
}
a <- extractRequestParts @(Either Request.BodyStringError String) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` (Right "foo") a `shouldEqual` (Right "foo")
a' <- extractRequestParts @String req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") a' <- extractRequestParts @String req <#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a' `shouldEqual` "foo" a' `shouldEqual` "foo"
it "extracts a JSON body" do it "extracts a JSON body" do
stream <- Buffer.fromString """{"foo": 123, "bar": "abc"}""" UTF8 >>= Stream.readableFromBuffer # liftEffect stream <- Buffer.fromString """{"foo": 123, "bar": "abc"}""" UTF8
req <- liftEffect $ Request.make {body: Request.BodyReadable stream, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust, headers: Map.empty, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4 {address: "127.0.0.1", port: 81}, method: POST} >>= Stream.readableFromBuffer
a <- extractRequestParts @(Post (Json {foo :: Int, bar :: String})) req <#> lmap (error <<< show) >>= liftEither >>= liftMaybe (error "was nothing") # liftEffect
a `shouldEqual` Post (Json {foo: 123, bar: "abc"}) req <- liftEffect $ Request.make
{ body: Request.BodyReadable stream
, url: URL.fromString "http://localhost:80/foo" # unsafePartial fromJust
, headers: Map.empty
, address: Left $ unsafePerformEffect $ SocketAddress.newIpv4
{ address: "127.0.0.1", port: 81 }
, method: POST
}
a <- extractRequestParts @(Post (Json { foo :: Int, bar :: String })) req
<#> lmap (error <<< show)
>>= liftEither
>>= liftMaybe (error "was nothing")
a `shouldEqual` Post (Json { foo: 123, bar: "abc" })