Skip to content

Commit bf2da2c

Browse files
committed
Merge pull request #31 from hdgarrood/testing
Make the tests a bit better
2 parents c54532b + cda3fcd commit bf2da2c

File tree

4 files changed

+126
-13
lines changed

4 files changed

+126
-13
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"gulp-jscs": "^1.6.0",
66
"gulp-jshint": "^1.11.2",
77
"gulp-plumber": "^1.0.0",
8-
"gulp-purescript": "^0.5.0"
8+
"gulp-purescript": "^0.5.0",
9+
"express": "^4.13.1",
10+
"body-parser": "^1.13.2"
911
}
1012
}

test-server.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
var express = require('express');
3+
var app = express();
4+
var bodyParser = require('body-parser');
5+
6+
// Always make req.body available as a String
7+
app.use(bodyParser.text(function() { return true; }));
8+
9+
app.use(express.static(__dirname));
10+
11+
app.get('/', function (req, res) {
12+
res.send('<html><script src="tmp/test.js"></script></html>');
13+
});
14+
15+
app.get('/arrayview', function(req, res) {
16+
res.send('TODO');
17+
});
18+
19+
app.get('/not-json', function(req, res) {
20+
res.header({'content-type': 'text/plain'});
21+
res.send('This is not JSON');
22+
});
23+
24+
app.all('/mirror', function(req, res) {
25+
res.json({
26+
headers: req.headers,
27+
body: req.body,
28+
query: req.query,
29+
method: req.method,
30+
});
31+
});
32+
33+
var server = app.listen(3838, function () {
34+
var host = server.address().address;
35+
var port = server.address().port;
36+
37+
console.log('Test server listening at http://%s:%s', host, port);
38+
});
39+

test/Main.purs

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,101 @@ module Test.Main where
33
import Prelude
44

55
import Control.Monad.Aff
6+
import Control.Bind
67
import Control.Monad.Eff
78
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
911
import Control.Monad.Eff.Exception
1012
import Data.Either
13+
import Data.Maybe
1114
import Data.Foreign
1215
import Network.HTTP.Affjax
1316
import Network.HTTP.Affjax.Response
1417
import Network.HTTP.Affjax.Request
1518
import Network.HTTP.Method
1619
import Network.HTTP.MimeType.Common
1720
import Network.HTTP.RequestHeader
21+
import Network.HTTP.StatusCode
1822

1923
foreign import logAny
2024
:: forall e a. a -> Eff (console :: CONSOLE | e) Unit
2125

22-
main = launchAff $ do
26+
logAny' :: forall e a. a -> Assert e Unit
27+
logAny' = liftEff <<< logAny
2328

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
2630

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
2934

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
3238

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
3543

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")
37102
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

test/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# How to run these tests
2+
3+
* Compile the code and tests, by running `node_modules/.bin/gulp`
4+
* Start the harness server, by running `node test-server.js`
5+
* Visit <http://localhost:3838> in your browser
6+
* Check the console: the text "affjax: All good!" should appear if and only if
7+
all the tests passed.

0 commit comments

Comments
 (0)