Skip to content

Commit 4220d9c

Browse files
committed
Bring back basic http testing flow
1 parent 73bf411 commit 4220d9c

File tree

6 files changed

+76
-34
lines changed

6 files changed

+76
-34
lines changed

src/WebRow.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ module WebRow
22
( module HTTP.Response
33
, module HTTP.Request
44
-- , module HTTP.Except
5-
)
6-
where
5+
) where
76

87
import WebRow.HTTP.Response (ok) as HTTP.Response
98
import WebRow.HTTP.Request (body, fullPath, method) as HTTP.Request

src/WebRow/I18N/Routing.purs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,61 @@ import Type.Row (type (+))
1414
import WebRow.HTTP (HTTPExcept)
1515
import WebRow.HTTP (redirect) as HTTP.Response
1616
import WebRow.I18N.ISO639.TwoLetter (Languages, languageCode, parse, toString)
17-
import WebRow.Routing (FullUrl(..), ROUTING, RelativeUrl(..), Routing, _routing, fromRelativeUrl)
18-
import WebRow.Routing (printFullRoute, printRoute) as Routing
17+
import WebRow.Routing (FullUrl(..), ROUTING, RelativeUrl(..), _routing, fromRelativeUrl)
18+
import WebRow.Routing (Routing, printFullRoute, printRoute) as Routing
19+
20+
type Route' langs route = { language Variant langs, route route }
1921

2022
duplex
2123
langs route
2224
. Contractable Languages langs
2325
Variant langs
2426
RouteDuplex' route
25-
RouteDuplex' { language Variant langs, route route }
27+
RouteDuplex' (Route' langs route)
2628
duplex default (RouteDuplex routePrinter routeParser) =
2729
RouteDuplex printer parser
2830
where
2931
RouteDuplex langPrinter langParser =
3032
(as (languageCode >>> toString) (parse >>> note "Invalid language code")) segment
3133

32-
printer { language, route: r } = if languageCode language == languageCode default
34+
printer { language: l, route: r } = if languageCode l == languageCode default
3335
then routePrinter r
34-
else langPrinter language <> routePrinter r
36+
else langPrinter l <> routePrinter r
3537

3638
parser
3739
= ({ language:_, route: _ } <$> langParser <*> routeParser)
3840
<|> ({ language: default, route: _ } <$> routeParser)
3941

40-
type Routing' langs routes eff = Routing ({ language Variant langs, route Variant routes }) eff
42+
type Routing' langs routes eff = Routing.Routing (Route' langs routes) eff
4143

42-
type ROUTING' (langs ∷ # Type) (v ∷ # Type) = ROUTING ({ language Variant langs, route Variant v })
44+
type ROUTING' (langs ∷ # Type) route = ROUTING (Route' langs route)
4345

44-
printRoute langs v eff. Variant v Run ( routing ROUTING' langs v | eff ) RelativeUrl
46+
printRoute eff langs route. route Run ( routing ROUTING' langs route | eff ) RelativeUrl
4547
printRoute v = map RelativeUrl $ do
4648
routing ← askAt _routing
4749
pure $ D.print routing.routeDuplex { language: routing.route.language, route: v }
4850

49-
printFullRoute eff langs v. Variant v Run ( routing ROUTING' langs v | eff ) FullUrl
51+
printFullRoute eff langs route. route Run ( routing ROUTING' langs route | eff ) FullUrl
5052
printFullRoute v = map FullUrl $ (<>) <$> (askAt _routing <#> _.domain) <*> (map (un RelativeUrl) $ printRoute v)
5153

52-
translatedRoute langs v eff. Variant langs Variant v Run (routing ROUTING' langs v | eff ) RelativeUrl
54+
translatedRoute eff langs route. Variant langs route Run (routing ROUTING' langs route | eff ) RelativeUrl
5355
translatedRoute lang v = Routing.printRoute { language: lang, route: v }
5456

55-
translatedFullRoute langs v eff. Variant langs Variant v Run (routing ROUTING' langs v | eff ) FullUrl
57+
translatedFullRoute eff langs route. Variant langs route Run (routing ROUTING' langs route | eff ) FullUrl
5658
translatedFullRoute lang v = Routing.printFullRoute { language: lang, route: v }
5759

58-
route eff langs route. Run (Routing' langs route + eff) (Variant route)
60+
fullRoute eff langs route. Run (Routing' langs route + eff) (Route' langs route)
61+
fullRoute = _.route <$> askAt _routing
62+
63+
route eff langs route. Run (Routing' langs route + eff) route
5964
route = _.route <<< _.route <$> askAt _routing
6065

66+
language eff langs route. Run (Routing' langs route + eff) (Variant langs)
67+
language = _.language <<< _.route <$> askAt _routing
68+
6169
redirect
6270
a eff langs route
63-
. Variant route
71+
. route
6472
Run
6573
( HTTPExcept
6674
+ Routing' langs route

src/WebRow/Routing.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ import WebRow.Routing.Types (Context, Domain, FullUrl(..), RelativeUrl(..), from
4141

4242
_routing = SProxy SProxy "routing"
4343

44+
-- | TODO:
45+
-- | Do we want to use custom effect here like
46+
-- |
47+
-- | `data RoutingF = PrintRouteF .. | PrintFullRouteF ... | RedirectF ...
48+
-- |
49+
-- | Then we can abstract over i18n and simple routes in generic applets.
4450
type ROUTING route = READER (Context route)
4551

4652
type Routing route eff = (routing ROUTING route | eff)

src/WebRow/Testing/Assertions.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module WebRow.Testing.Assertions where
2+
3+
import Prelude
4+
5+
import Run (Run, liftEffect)
6+
import Test.Spec.Assertions (shouldEqual) as Assertions
7+
import Type.Row (type (+))
8+
import WebRow.Contrib.Run (EffRow)
9+
10+
shouldEqual a eff. Show a Eq a a a Run (EffRow + eff) Unit
11+
shouldEqual expected given = liftEffect $ Assertions.shouldEqual expected given

test/WebRow/HTTP.purs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import HTTPure (Method(..))
2020
import HTTPure (Method(..), Request, fullPath) as HTTPure
2121
import HTTPure.Headers (empty) as Headers
2222
import Routing.Duplex (RouteDuplex', int, parse, print, root, segment, string) as D
23+
import Routing.Duplex.Generic (noArgs)
2324
import Routing.Duplex.Generic.Variant (variant') as RouteDuplex.Variant
2425
import Run (Run, extract, runBaseAff, runBaseAff', runBaseEffect)
2526
import Run (expand, runBaseAff, runBaseAff', runBaseEffect) as Run
@@ -31,7 +32,6 @@ import Run.Streaming.Prelude (feed, head, take) as S.P
3132
import Run.Streaming.Prelude (fold) as S.Prelude
3233
import Run.Streaming.Pull (chain) as Pull
3334
import Test.Spec (Spec, describe, it, pending)
34-
import Test.Spec.Assertions (shouldEqual)
3535
import Type.Prelude (SProxy(..))
3636
import Type.Row (type (+))
3737
import Unsafe.Coerce (unsafeCoerce)
@@ -40,35 +40,42 @@ import WebRow.Contrib.Run (AffRow, EffRow)
4040
import WebRow.Crypto (Crypto, _crypto, secret)
4141
import WebRow.Crypto (secret) as Crypto
4242
import WebRow.HTTP (HTTPExcept, HTTPResponse, Request, SetHeader, Cookies, notFound)
43+
import WebRow.HTTP (fullPath) as HTTP
4344
import WebRow.HTTP.Cookies (defaultAttributes)
4445
import WebRow.HTTP.Cookies (defaultAttributes, lookup, set) as Cookies
4546
import WebRow.HTTP.Request (_request)
4647
import WebRow.HTTP.Response (ok)
48+
import WebRow.Testing.Assertions (shouldEqual)
4749
import WebRow.Testing.HTTP (Client, HTTPSession, _httpSession, get, get_, request)
4850
import WebRow.Testing.HTTP (run, run') as Testing.HTTP
51+
import WebRow.Testing.HTTP.Response (Response(..))
4952

5053
spec :: Spec Unit
5154
spec = do
5255
pure unit
53-
-- describe "WebRow.HTTP" do
54-
-- describe "Response" do
55-
-- it "SetHeader" do
56-
-- let
57-
-- client = do
58-
-- get_ "1"
59-
-- get_ "2"
56+
describe "WebRow.HTTP" do
57+
describe "Response" do
58+
it "SetHeader" do
59+
let
60+
client = do
61+
response ← get "1"
62+
case response of
63+
HTTPResponse { parts: { body }} → shouldEqual body "TET"
64+
otherwise → pure unit
65+
get_ "2"
6066

61-
-- server req = do
62-
-- cs ← Crypto.secret
63-
-- c ← Lazy.force <$> Cookies.lookup "test"
64-
-- liftEffect $ logShow c
65-
-- void $ Cookies.set "test" { value: "test", attributes: Cookies.defaultAttributes }
66-
-- r ← liftEffect $ random
67-
-- ok $ (req.url <> ":" <> show r)
67+
server = do
68+
path ← HTTP.fullPath
69+
cs ← Crypto.secret
70+
c ← Lazy.force <$> Cookies.lookup "test"
71+
liftEffect $ logShow c
72+
void $ Cookies.set "test" { value: "test", attributes: Cookies.defaultAttributes }
73+
r ← liftEffect $ random
74+
ok $ "TEST" -- (req.url <> ":" <> show r)
6875

69-
-- httpSession <- runBaseAff' $ Testing.HTTP.run' {} pure server client
70-
-- logShow $ unsafeStringify httpSession
71-
-- pure unit
76+
httpSession <- runBaseAff' $ Testing.HTTP.run' {} noArgs pure server client
77+
logShow $ unsafeStringify httpSession
78+
pure unit
7279

7380
-- pending "feature complete"
7481
-- describe "Features" do

test/WebRow/I18N.purs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,23 @@ spec = do
3939
describe "I18N" do
4040
describe "route duplex" do
4141
it "should print translated path" do
42+
let
43+
path = print duplex ({ language: ur, route: NoArguments })
44+
shouldEqual "/ur" path
45+
46+
it "should print empty prefix for default lang" do
4247
let
4348
path = print duplex ({ language: la, route: NoArguments })
44-
shouldEqual "/la" path
49+
shouldEqual "/" path
4550

4651
it "should parse translated path" do
4752
let
4853
lang = parse duplex ("/ur")
4954

5055
shouldEqual (hush lang <#> _.language >>> languageCode) (Just (languageCode ur))
56+
57+
it "should parse default langauge path" do
58+
let
59+
lang = parse duplex ("/")
60+
61+
shouldEqual (hush lang <#> _.language >>> languageCode) (Just (languageCode la))

0 commit comments

Comments
 (0)