Skip to content

Commit de5e1e8

Browse files
committed
Update tests to run on Node.js
1 parent 4e51485 commit de5e1e8

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

test/Main.purs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,47 @@ assertEq x y = if x == y
5555
typeIs :: forall e a. a -> Assert e Unit
5656
typeIs = const (return unit)
5757

58-
main = runAff throwException (const $ log "affjax: All good!") $ do
58+
main = runAff (\e -> print e >>= \_ -> throwException e) (const $ log "affjax: All good!") $ do
5959
let ok200 = StatusCode 200
6060
let notFound404 = StatusCode 404
6161

62-
A.log "GET /does-not-exists: should be 404 Not found after retries"
63-
(attempt $ retry (Just 5000) affjax $ defaultRequest { url = "/does-not-exist" }) >>= assertRight >>= \res -> do
62+
-- A complete URL is necessary for tests to work on Node.js
63+
let prefix = append "http://localhost:3838"
64+
let mirror = prefix "/mirror"
65+
let doesNotExist = prefix "/does-not-exist"
66+
let notJson = prefix "/not-json"
67+
68+
A.log "GET /does-not-exist: should be 404 Not found after retries"
69+
(attempt $ retry (Just 5000) affjax $ defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
6470
typeIs (res :: AffjaxResponse String)
6571
assertEq notFound404 res.status
6672

6773
A.log "GET /mirror: should be 200 OK"
68-
(attempt $ affjax $ defaultRequest { url = "/mirror" }) >>= assertRight >>= \res -> do
74+
(attempt $ affjax $ defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
6975
typeIs (res :: AffjaxResponse Foreign)
7076
assertEq ok200 res.status
7177

7278
A.log "GET /does-not-exist: should be 404 Not found"
73-
(attempt $ affjax $ defaultRequest { url = "/does-not-exist" }) >>= assertRight >>= \res -> do
79+
(attempt $ affjax $ defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
7480
typeIs (res :: AffjaxResponse String)
7581
assertEq notFound404 res.status
7682

7783
A.log "GET /not-json: invalid JSON with Foreign response should throw an error"
78-
assertLeft =<< attempt (get "/not-json" :: Affjax _ Foreign)
84+
assertLeft =<< attempt (get doesNotExist :: Affjax _ Foreign)
7985

8086
A.log "GET /not-json: invalid JSON with String response should be ok"
81-
(attempt $ get "/not-json") >>= assertRight >>= \res -> do
87+
(attempt $ get notJson) >>= assertRight >>= \res -> do
8288
typeIs (res :: AffjaxResponse String)
8389
assertEq ok200 res.status
8490

8591
A.log "POST /mirror: should use the POST method"
86-
(attempt $ post "/mirror" "test") >>= assertRight >>= \res -> do
92+
(attempt $ post mirror "test") >>= assertRight >>= \res -> do
8793
assertEq ok200 res.status
8894
assertEq "POST" (_.method $ unsafeFromForeign res.response)
8995

9096
A.log "PUT with a request body"
9197
let content = "the quick brown fox jumps over the lazy dog"
92-
(attempt $ put "/mirror" content) >>= assertRight >>= \res -> do
98+
(attempt $ put mirror content) >>= assertRight >>= \res -> do
9399
typeIs (res :: AffjaxResponse Foreign)
94100
assertEq ok200 res.status
95101
let mirroredReq = unsafeFromForeign res.response
@@ -103,6 +109,6 @@ main = runAff throwException (const $ log "affjax: All good!") $ do
103109
-- assertEq (Just "test=test") (lookupHeader "Set-Cookie" res.headers)
104110

105111
A.log "Testing cancellation"
106-
canceler <- forkAff (post_ "/mirror" "do it now")
112+
canceler <- forkAff (post_ mirror "do it now")
107113
canceled <- canceler `cancel` error "Pull the cord!"
108114
assertMsg "Should have been canceled" canceled

0 commit comments

Comments
 (0)