@@ -3,36 +3,101 @@ module Test.Main where
3
3
import Prelude
4
4
5
5
import Control.Monad.Aff
6
+ import Control.Bind
6
7
import Control.Monad.Eff
7
8
import Control.Monad.Eff.Class
8
- import Control.Monad.Eff.Console (CONSOLE (), log )
9
+ import Control.Monad.Eff.Console (CONSOLE (), log , print )
10
+ import qualified Control.Monad.Aff.Console as A
9
11
import Control.Monad.Eff.Exception
10
12
import Data.Either
13
+ import Data.Maybe
11
14
import Data.Foreign
12
15
import Network.HTTP.Affjax
13
16
import Network.HTTP.Affjax.Response
14
17
import Network.HTTP.Affjax.Request
15
18
import Network.HTTP.Method
16
19
import Network.HTTP.MimeType.Common
17
20
import Network.HTTP.RequestHeader
21
+ import Network.HTTP.StatusCode
18
22
19
23
foreign import logAny
20
24
:: forall e a . a -> Eff (console :: CONSOLE | e ) Unit
21
25
22
- main = launchAff $ do
26
+ logAny' :: forall e a . a -> Assert e Unit
27
+ logAny' = liftEff <<< logAny
23
28
24
- res <- attempt $ affjax $ defaultRequest { url = " /api" , method = POST }
25
- liftEff $ either logAny (logAny :: AffjaxResponse String -> _ ) res
29
+ type Assert e a = Aff (err :: EXCEPTION , console :: CONSOLE , ajax :: AJAX | e ) a
26
30
27
- res <- attempt $ post_ " /api" " test"
28
- liftEff $ either logAny logAny res
31
+ assertFail :: forall e a . String -> Assert e a
32
+ assertFail msg = let e = error msg
33
+ in makeAff \errback _ -> errback e
29
34
30
- res <- attempt $ get " /arrayview"
31
- liftEff $ either logAny (logAny :: AffjaxResponse Foreign -> _ ) res
35
+ assertMsg :: forall e . String -> Boolean -> Assert e Unit
36
+ assertMsg _ true = return unit
37
+ assertMsg msg false = assertFail msg
32
38
33
- res <- attempt $ get " ttp://www.google.com"
34
- liftEff $ either logAny (logAny :: AffjaxResponse Foreign -> _ ) res
39
+ assertRight :: forall e a b . Either a b -> Assert e b
40
+ assertRight x = case x of
41
+ Left y -> logAny' y >>= \_ -> assertFail " Expected a Right value"
42
+ Right y -> return y
35
43
36
- canceler <- forkAff (post_ " /api" " do it now" )
44
+ assertLeft :: forall e a b . Either a b -> Assert e a
45
+ assertLeft x = case x of
46
+ Right y -> logAny' y >>= \_ -> assertFail " Expected a Left value"
47
+ Left y -> return y
48
+
49
+ assertEq :: forall e a . (Eq a , Show a ) => a -> a -> Assert e Unit
50
+ assertEq x y = if x == y
51
+ then return unit
52
+ else assertFail $ " Expected " <> show x <> " , got " <> show y
53
+
54
+ -- | For helping type inference
55
+ typeIs :: forall e a . a -> Assert e Unit
56
+ typeIs = const (return unit)
57
+
58
+ main = runAff throwException (const $ log " affjax: All good!" ) $ do
59
+ let ok200 = StatusCode 200
60
+ let notFound404 = StatusCode 404
61
+
62
+ A .log " GET /mirror: should be 200 OK"
63
+ (attempt $ affjax $ defaultRequest { url = " /mirror" }) >>= assertRight >>= \res -> do
64
+ typeIs (res :: AffjaxResponse Foreign )
65
+ assertEq ok200 res.status
66
+
67
+ A .log " GET /does-not-exist: should be 404 Not found"
68
+ (attempt $ affjax $ defaultRequest { url = " /does-not-exist" }) >>= assertRight >>= \res -> do
69
+ typeIs (res :: AffjaxResponse String )
70
+ assertEq notFound404 res.status
71
+
72
+ A .log " GET /not-json: invalid JSON with Foreign response should throw an error"
73
+ assertLeft =<< attempt (get " /not-json" :: Affjax _ Foreign )
74
+
75
+ A .log " GET /not-json: invalid JSON with String response should be ok"
76
+ (attempt $ get " /not-json" ) >>= assertRight >>= \res -> do
77
+ typeIs (res :: AffjaxResponse String )
78
+ assertEq ok200 res.status
79
+
80
+ A .log " POST /mirror: should use the POST method"
81
+ (attempt $ post " /mirror" " test" ) >>= assertRight >>= \res -> do
82
+ assertEq ok200 res.status
83
+ assertEq " POST" (_.method $ unsafeFromForeign res.response)
84
+
85
+ A .log " PUT with a request body"
86
+ let content = " the quick brown fox jumps over the lazy dog"
87
+ (attempt $ put " /mirror" content) >>= assertRight >>= \res -> do
88
+ typeIs (res :: AffjaxResponse Foreign )
89
+ assertEq ok200 res.status
90
+ let mirroredReq = unsafeFromForeign res.response
91
+ assertEq " PUT" mirroredReq.method
92
+ assertEq content mirroredReq.body
93
+
94
+ A .log " Testing CORS, HTTPS"
95
+ (attempt $ get " https://cors-test.appspot.com/test" ) >>= assertRight >>= \res -> do
96
+ typeIs (res :: AffjaxResponse Foreign )
97
+ assertEq ok200 res.status
98
+ -- assertEq (Just "test=test") (lookupHeader "Set-Cookie" res.headers)
99
+
100
+ A .log " Testing cancellation"
101
+ canceler <- forkAff (post_ " /mirror" " do it now" )
37
102
canceled <- canceler `cancel` error " Pull the cord!"
38
- liftEff $ if canceled then (log " Canceled " ) else (log " Not Canceled " )
103
+ assertMsg " Should have been canceled " canceled
0 commit comments