Skip to content

Commit 20bc9b4

Browse files
committed
Start and stop test server as part of running the tests
1 parent 3f5df54 commit 20bc9b4

File tree

5 files changed

+113
-88
lines changed

5 files changed

+113
-88
lines changed

.travis.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
language: node_js
22
dist: trusty
33
sudo: required
4-
node_js: 6
4+
node_js: stable
55
install:
66
- npm install bower -g
77
- npm install
88
script:
99
- bower install --production
1010
- npm run -s build
1111
- bower install
12-
- node ./test-server.js &
13-
- sleep 2 && npm -s test
12+
- npm run -s test
1413
after_success:
1514
- >-
1615
test $TRAVIS_TAG &&

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"express": "^4.14.0",
1212
"pulp": "^11.0.0",
1313
"purescript-psa": "^0.5.0",
14-
"purescript": "slamdata/node-purescript#0.12",
14+
"purescript": "^0.12.0",
1515
"rimraf": "^2.5.4",
1616
"xhr2": "^0.1.3"
1717
}

test-server.js

-39
This file was deleted.

test/Main.js

+56
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,59 @@ exports.logAny = function (a) {
66
return {};
77
};
88
};
9+
10+
exports.startServer = function (errback, callback) {
11+
var express = require('express');
12+
var app = express();
13+
var bodyParser = require('body-parser');
14+
15+
// Always make req.body available as a String
16+
app.use(bodyParser.text(function() { return true; }));
17+
18+
app.use(express.static(__dirname));
19+
20+
app.get('/', function (req, res) {
21+
res.send('<html><script src="tmp/test.js"></script></html>');
22+
});
23+
24+
app.get('/arrayview', function(req, res) {
25+
res.send('TODO');
26+
});
27+
28+
app.get('/not-json', function(req, res) {
29+
res.header({'content-type': 'text/plain'});
30+
res.send('This is not JSON');
31+
});
32+
33+
app.all('/mirror', function(req, res) {
34+
res.json({
35+
headers: req.headers,
36+
body: req.body,
37+
query: req.query,
38+
method: req.method,
39+
});
40+
});
41+
42+
var server = app.listen(function () {
43+
callback({ port: server.address().port, server: server });
44+
});
45+
server.on('error', function (error) {
46+
errback(error);
47+
});
48+
49+
return function (cancelError, onCancelerError, onCancelerSuccess) {
50+
onCancelerSuccess();
51+
};
52+
};
53+
54+
exports.stopServer = function (server) {
55+
return function (errback, callback) {
56+
server.close(function (err) {
57+
if (err) errback(err);
58+
else callback();
59+
});
60+
return function (cancelError, onCancelerError, onCancelerSuccess) {
61+
onCancelerSuccess();
62+
};
63+
};
64+
};

test/Main.purs

+54-45
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Data.Either (Either(..), either)
1212
import Data.Maybe (Maybe(..))
1313
import Data.Time.Duration (Milliseconds(..))
1414
import Effect (Effect)
15-
import Effect.Aff (Aff, forkAff, attempt, runAff, killFiber)
15+
import Effect.Aff (Aff, attempt, finally, forkAff, killFiber, runAff)
16+
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
1617
import Effect.Class (liftEffect)
1718
import Effect.Class.Console as A
1819
import Effect.Console (log, logShow)
@@ -21,6 +22,10 @@ import Foreign.Object as FO
2122

2223
foreign import logAny :: forall a. a -> Effect Unit
2324

25+
foreign import data Server :: Type
26+
foreign import startServer :: EffectFnAff { server :: Server, port :: Int }
27+
foreign import stopServer :: Server -> EffectFnAff Unit
28+
2429
logAny' :: forall a. a -> Aff Unit
2530
logAny' = liftEffect <<< logAny
2631

@@ -50,49 +55,53 @@ main = void $ runAff (either (\e -> logShow e *> throwException e) (const $ log
5055
let ok200 = StatusCode 200
5156
let notFound404 = StatusCode 404
5257

53-
-- A complete URL is necessary for tests to work on Node.js
54-
let prefix = append "http://localhost:3838"
55-
let mirror = prefix "/mirror"
56-
let doesNotExist = prefix "/does-not-exist"
57-
let notJson = prefix "/not-json"
5858
let retryPolicy = AX.defaultRetryPolicy { timeout = Just (Milliseconds 500.0), shouldRetryWithStatusCode = \_ -> true }
5959

60-
A.log "GET /does-not-exist: should be 404 Not found after retries"
61-
(attempt $ AX.retry retryPolicy (AX.request ResponseFormat.ignore) $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
62-
assertEq notFound404 res.status
63-
64-
A.log "GET /mirror: should be 200 OK"
65-
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
66-
assertEq ok200 res.status
67-
68-
A.log "GET /does-not-exist: should be 404 Not found"
69-
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
70-
assertEq notFound404 res.status
71-
72-
A.log "GET /not-json: invalid JSON with Foreign response should throw an error"
73-
void $ assertLeft =<< attempt (AX.get ResponseFormat.json doesNotExist)
74-
75-
A.log "GET /not-json: invalid JSON with String response should be ok"
76-
(attempt $ AX.get ResponseFormat.string notJson) >>= assertRight >>= \res -> do
77-
assertEq ok200 res.status
78-
79-
A.log "POST /mirror: should use the POST method"
80-
(attempt $ AX.post ResponseFormat.json mirror (RequestBody.string "test")) >>= assertRight >>= \res -> do
81-
assertEq ok200 res.status
82-
assertEq (Just "POST") (J.toString =<< FO.lookup "method" =<< J.toObject res.response)
83-
84-
A.log "PUT with a request body"
85-
let content = "the quick brown fox jumps over the lazy dog"
86-
(attempt $ AX.put ResponseFormat.json mirror (RequestBody.string content)) >>= assertRight >>= \res -> do
87-
assertEq ok200 res.status
88-
assertEq (Just "PUT") (J.toString =<< FO.lookup "method" =<< J.toObject res.response)
89-
assertEq (Just content) (J.toString =<< FO.lookup "body" =<< J.toObject res.response)
90-
91-
A.log "Testing CORS, HTTPS"
92-
(attempt $ AX.get ResponseFormat.json "https://cors-test.appspot.com/test") >>= assertRight >>= \res -> do
93-
assertEq ok200 res.status
94-
-- assertEq (Just "test=test") (lookupHeader "Set-Cookie" res.headers)
95-
96-
A.log "Testing cancellation"
97-
forkAff (AX.post_ mirror (RequestBody.string "do it now")) >>= killFiber (error "Pull the cord!")
98-
assertMsg "Should have been canceled" true
60+
{ server, port } ← fromEffectFnAff startServer
61+
finally (fromEffectFnAff (stopServer server)) do
62+
A.log ("Test server running on port " <> show port)
63+
64+
let prefix = append ("http://localhost:" <> show port)
65+
let mirror = prefix "/mirror"
66+
let doesNotExist = prefix "/does-not-exist"
67+
let notJson = prefix "/not-json"
68+
69+
A.log "GET /does-not-exist: should be 404 Not found after retries"
70+
(attempt $ AX.retry retryPolicy (AX.request ResponseFormat.ignore) $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
71+
assertEq notFound404 res.status
72+
73+
A.log "GET /mirror: should be 200 OK"
74+
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = mirror }) >>= assertRight >>= \res -> do
75+
assertEq ok200 res.status
76+
77+
A.log "GET /does-not-exist: should be 404 Not found"
78+
(attempt $ AX.request ResponseFormat.ignore $ AX.defaultRequest { url = doesNotExist }) >>= assertRight >>= \res -> do
79+
assertEq notFound404 res.status
80+
81+
A.log "GET /not-json: invalid JSON with Foreign response should throw an error"
82+
void $ assertLeft =<< attempt (AX.get ResponseFormat.json doesNotExist)
83+
84+
A.log "GET /not-json: invalid JSON with String response should be ok"
85+
(attempt $ AX.get ResponseFormat.string notJson) >>= assertRight >>= \res -> do
86+
assertEq ok200 res.status
87+
88+
A.log "POST /mirror: should use the POST method"
89+
(attempt $ AX.post ResponseFormat.json mirror (RequestBody.string "test")) >>= assertRight >>= \res -> do
90+
assertEq ok200 res.status
91+
assertEq (Just "POST") (J.toString =<< FO.lookup "method" =<< J.toObject res.response)
92+
93+
A.log "PUT with a request body"
94+
let content = "the quick brown fox jumps over the lazy dog"
95+
(attempt $ AX.put ResponseFormat.json mirror (RequestBody.string content)) >>= assertRight >>= \res -> do
96+
assertEq ok200 res.status
97+
assertEq (Just "PUT") (J.toString =<< FO.lookup "method" =<< J.toObject res.response)
98+
assertEq (Just content) (J.toString =<< FO.lookup "body" =<< J.toObject res.response)
99+
100+
A.log "Testing CORS, HTTPS"
101+
(attempt $ AX.get ResponseFormat.json "https://cors-test.appspot.com/test") >>= assertRight >>= \res -> do
102+
assertEq ok200 res.status
103+
-- assertEq (Just "test=test") (lookupHeader "Set-Cookie" res.headers)
104+
105+
A.log "Testing cancellation"
106+
forkAff (AX.post_ mirror (RequestBody.string "do it now")) >>= killFiber (error "Pull the cord!")
107+
assertMsg "Should have been canceled" true

0 commit comments

Comments
 (0)