Permalink
Browse files

Add static middleware option with CONNECT_STATIC

Setting this to a string will allow the built in connect server to serve static files
from that directory.
  • Loading branch information...
1 parent ae9e939 commit fc9a6615bc72c14954e05b490138209d83f83c59 @omega omega committed Jan 17, 2013
Showing with 26 additions and 1 deletion.
  1. +3 −0 README.md
  2. +2 −0 src/robot.coffee
  3. +15 −0 test/httpd_test.coffee
  4. +1 −1 test/robot_test.coffee
  5. +4 −0 test/scripts/test.coffee
  6. +1 −0 test/static/test
View
@@ -100,6 +100,9 @@ module.exports = (robot) ->
There are functions for GET, POST, PUT and DELETE, which all take a route and
callback function that accepts a request and a response.
+In addition, if you set `CONNECT_STATIC`, the HTTP listener will serve static
+files from this directory.
+
## Testing hubot locally
Install all of the required dependencies by running `npm install`.
View
@@ -185,6 +185,7 @@ class Robot
setupConnect: ->
user = process.env.CONNECT_USER
pass = process.env.CONNECT_PASSWORD
+ stat = process.env.CONNECT_STATIC
Connect = require 'connect'
Connect.router = require 'connect_router'
@@ -193,6 +194,7 @@ class Robot
@connect.use Connect.basicAuth(user, pass) if user and pass
@connect.use Connect.bodyParser()
+ @connect.use Connect.static(stat) if stat
@connect.use Connect.router (app) =>
@router =
View
@@ -0,0 +1,15 @@
+process.env.CONNECT_STATIC = 'test/static/'
+Tests = require './tests'
+assert = require 'assert'
+helper = Tests.helper()
+require './scripts/test'
+
+helper.adapter.cb = (msg) ->
+ assert.equal 1, helper.sent.length
+ assert.equal "static\n", msg
+ helper.stop()
+
+setTimeout( () ->
+ helper.adapter.receive 'static'
+, 100)
+
View
@@ -8,7 +8,7 @@ server = require('http').createServer (req, res) ->
res.end "fetched"
server.listen 9001, ->
- assert.equal 5, helper.listeners.length
+ assert.equal 6, helper.listeners.length
assert.equal 0, helper.sent.length
helper.adapter.receive 'test'
View
@@ -1,6 +1,10 @@
# Tests hubot listeners
module.exports = (robot) ->
assert = require 'assert'
+ robot.hear /static/i, (msg) ->
+ msg.http('http://127.0.0.1/test').port(process.env.PORT or 8080)
+ .get() (err, res, body) ->
+ msg.send body
robot.hear /test/i, (msg) ->
msg.send "OK"
View
@@ -0,0 +1 @@
+static

0 comments on commit fc9a661

Please sign in to comment.