Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test handling multiple requests and restarting

  • Loading branch information...
commit 9fa7678855bcd7eea1d1143f8b619acc1342a3f1 1 parent 78c86c0
@sstephenson sstephenson authored
View
4 .gitignore
@@ -1,2 +1,2 @@
-test/fixtures/tmp
-
+/test/fixtures/*/tmp/restart.txt
+/test/fixtures/tmp/
View
0  test/fixtures/apps/hello/tmp/.gitkeep
No changes.
View
4 test/fixtures/apps/pid/config.ru
@@ -0,0 +1,4 @@
+run lambda {
+ body = Process.pid.to_s
+ [200, {'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s}, [body]]
+}
View
0  test/fixtures/apps/pid/tmp/.gitkeep
No changes.
View
9 test/lib/test_helper.coffee
@@ -11,11 +11,16 @@ exports.prepareFixtures = (callback) ->
callback()
exports.rm_rf = rm_rf = (path, callback) ->
- exec "rm -rf #{path}", (err, stdout, stderr) ->
+ exec "rm -rf #{path}", (err) ->
if err then callback err
else callback()
exports.mkdirp = mkdirp = (path, callback) ->
- exec "mkdir -p #{path}", (err, stdout, stderr) ->
+ exec "mkdir -p #{path}", (err) ->
+ if err then callback err
+ else callback()
+
+exports.touch = touch = (path, callback) ->
+ exec "touch #{path}", (err) ->
if err then callback err
else callback()
View
68 test/test_rack_handler.coffee
@@ -6,9 +6,34 @@ express = require "express"
Configuration = require "pow/configuration"
RackHandler = require "pow/rack_handler"
-{prepareFixtures, fixturePath} = require "test_helper"
+{prepareFixtures, fixturePath, touch} = require "test_helper"
-PORT = 20561
+serve = (path, callback) ->
+ handler = handlerFor path
+ server = express.createServer()
+ server.get "/", (req, res, next) ->
+ handler.handle req, res, next
+ server.listen 0, ->
+ request = createRequester server.address().port
+ callback request, (callback) ->
+ server.close()
+ handler.quit callback
+
+createRequester = (port) ->
+ (method, path, headers, callback) ->
+ callback = headers unless callback
+ client = http.createClient port
+ request = client.request method, path, headers
+ request.end()
+ request.on "response", (response) ->
+ body = ""
+ response.on "data", (chunk) -> body += chunk.toString "utf8"
+ response.on "end", ->
+ callback body, response
+
+handlerFor = (path) ->
+ configuration = new Configuration root: fixturePath("apps"), logRoot: fixturePath("tmp/logs")
+ new RackHandler configuration, fixturePath(path)
module.exports = testCase
setUp: (proceed) ->
@@ -16,20 +41,27 @@ module.exports = testCase
"handling a request": (test) ->
test.expect 1
+ serve "apps/hello", (request, done) ->
+ request "GET", "/", (body) ->
+ test.same "Hello", body
+ done -> test.done()
+
+ "handling multiple requests": (test) ->
+ test.expect 2
+ serve "apps/pid", (request, done) ->
+ request "GET", "/", (body) ->
+ test.ok pid = parseInt body
+ request "GET", "/", (body) ->
+ test.ok pid is parseInt body
+ done -> test.done()
- configuration = new Configuration root: fixturePath("apps"), logRoot: fixturePath("tmp/logs")
-
- handler = new RackHandler configuration, fixturePath("apps/hello")
- server = express.createServer()
- server.get "/", (req, res, next) -> handler.handle req, res, next
- server.listen PORT, ->
- client = http.createClient PORT
- request = client.request "GET", "/", host: "hello.test"
- request.end()
- request.on "response", (response) ->
- body = ""
- response.on "data", (chunk) -> body += chunk.toString "utf8"
- response.on "end", ->
- test.same "Hello", body
- server.close()
- test.done()
+ "handling a request, restart, request": (test) ->
+ test.expect 3
+ serve "apps/pid", (request, done) ->
+ request "GET", "/", (body) ->
+ test.ok pid = parseInt body
+ touch fixturePath("apps/pid/tmp/restart.txt"), ->
+ request "GET", "/", (body) ->
+ test.ok newpid = parseInt body
+ test.ok pid isnt newpid
+ done -> test.done()
Please sign in to comment.
Something went wrong with that request. Please try again.