Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rewrite test server with Express

  • Loading branch information...
commit 99b15ca2250375c4adb9a84f29752599d92ff26f 1 parent 7fe707a
@mislav mislav authored
Showing with 59 additions and 67 deletions.
  1. +2 −1  package.json
  2. +1 −1  script/test
  3. +56 −0 test/server.coffee
  4. +0 −65 test/server.js
View
3  package.json
@@ -15,6 +15,7 @@
]
, "devDependencies": {
"uglify-js": "1.2.6"
- , "connect": "2.7.1"
+ , "express": "3.1.x"
+ , "coffee-script": "1.4.x"
}
}
View
2  script/test
@@ -2,7 +2,7 @@
port=3999
pidfile=test/server.pid
-node test/server.js $port &
+./node_modules/.bin/coffee test/server.coffee $port &
pid=$!
terminate_test_server() {
View
56 test/server.coffee
@@ -0,0 +1,56 @@
+express = require 'express'
+app = express()
+
+module.exports = app
+
+project_root = __dirname.replace(/\/[^\/]+$/, '/')
+app.use express.static(project_root)
+app.use express.static(project_root + 'node_modules/mocha')
+app.use express.static(project_root + 'node_modules/chai')
+
+app.use express.bodyParser()
+
+mime = (req) ->
+ type = req.headers['content-type'] or ''
+ type.split(';')[0]
+
+dump = (obj) ->
+ obj = '' unless obj
+ obj = JSON.stringify(obj) if obj and typeof obj isnt "string"
+ obj
+
+app.all '/test/echo', (req, res) ->
+ res.send """
+ #{req.method} ?#{dump(req.query)}
+ content-type: #{mime(req)}
+ accept: #{req.headers['accept']}
+ #{dump(req.body)}
+ """
+
+app.get '/test/jsonp', (req, res) ->
+ res.jsonp
+ query: req.query
+ hello: 'world'
+
+app.get '/test/json', (req, res) ->
+ if /json/.test req.headers['accept']
+ res.json
+ query: req.query
+ hello: 'world'
+ else
+ res.send 400, 'FAIL'
+
+app.all '/test/slow', (req, res) ->
+ setTimeout ->
+ res.send 'DONE'
+ , 200
+
+app.all '/test/error', (req, res) ->
+ res.send 500, 'BOOM'
+
+if process.argv[1] is __filename
+ port = process.argv[2]
+ unless port
+ port = 3000
+ console.log "Listening on port #{port}"
+ app.listen port
View
65 test/server.js
@@ -1,65 +0,0 @@
-var connect = require('connect')
- , parse = connect.utils.parseUrl
- , pause = connect.utils.pause
- , mime = connect.utils.mime
- , http = require('http')
- , fs = require('fs')
- , port = process.argv[2] || 3000
- , app = connect()
- .use(connect.static(__dirname + '/../')) // serve static files from root dir
- .use(connect.query())
- .use(connect.urlencoded())
- .use(connect.json())
- .use(function(req, res, next) {
- var url = parse(req)
- // provide an echo endpoint for POSTs and such
- if (url.pathname == "/test/echo") {
- var body = req.method
- // echo query params
- if (req.query) body += ' ?' + JSON.stringify(req.query)
- // echo request headers
- body += "\ncontent-type: " + mime(req)
- body += "\naccept: " + req.headers['accept']
- // echo request body
- if (typeof req.body == "string") body += "\n" + req.body
- else if (req.body) body += "\n" + JSON.stringify(req.body)
- res.end(body)
- }
- else if (url.pathname == "/test/jsonp") {
- var name = req.query.callback
- if (name) {
- delete req.query.callback
- var payload = { query: req.query, hello: "world" }
- res.setHeader('content-type', 'application/javascript')
- res.end(name + '(' + JSON.stringify(payload) + ')')
- } else {
- res.statusCode = 400
- res.end("FAIL")
- }
- }
- else if (url.pathname == "/test/json") {
- var accepts = String(req.headers['accept'])
- if (accepts.match(/json/)) {
- var payload = { query: req.query, hello: "world" }
- res.setHeader('content-type', 'application/json')
- res.end(JSON.stringify(payload))
- } else {
- res.statusCode = 400
- res.end("invalid accept type: " + accepts)
- }
- }
- else if (url.pathname == "/test/slow") {
- var stopped = pause(req)
- setTimeout(function(){
- stopped.resume()
- res.end("DONE")
- }, 200)
- }
- else if (url.pathname == "/test/error") {
- res.statusCode = 500
- res.end("BOOM")
- }
- else next()
- })
-
-http.createServer(app).listen(port)
Please sign in to comment.
Something went wrong with that request. Please try again.