web api test helper

This commit is contained in:
jysperm
2014-10-21 17:30:17 +08:00
parent 080f099fa0
commit 539cc68fe6
5 changed files with 68 additions and 7 deletions

View File

@@ -164,6 +164,8 @@ app.express.get '/', (req, res) ->
app.express.use harp.mount './core/static'
app.express.listen config.web.listen, ->
app.started = true
if fs.existsSync config.web.listen
fs.chmodSync config.web.listen, 0o770

View File

@@ -12,6 +12,15 @@ describe 'app', ->
, ->
done()
it 'express should be started', (done) ->
async.forever (callback) ->
if app.started
callback true
else
setImmediate callback
, ->
done()
it 'app.libs should be loaded', ->
{_, express, fs, mongoose} = app.libs
_.should.be.ok

View File

@@ -41,7 +41,7 @@
"mysql": "^2.4.2",
"nodemailer": "^1.2.1",
"redis": "^0.12.1",
"request": "^2.40.0",
"request": "^2.45.0",
"tmp": "^0.0.24",
"underscore": "^1.6.0",
"json-stable-stringify": "^1.0.0",
@@ -54,7 +54,8 @@
"mongoose": "^3.8.17",
"depd": "^1.0.0",
"body-parser": "^1.9.0",
"copy-to": "^1.0.1"
"copy-to": "^1.0.1",
"deepmerge": "^0.2.7"
},
"devDependencies": {
"mocha": "^1.21.5",

View File

@@ -0,0 +1,42 @@
request = require 'request'
if fs.existsSync config.web.listen
uri_prefix = "http://unix:#{config.web.listen}:"
else
uri_prefix = "http://127.0.0.1:#{config.web.listen}"
exports.get = (url, options, callback) ->
options = deepmerge module.exports.default_options, options
options = deepmerge options,
uri: "#{uri_prefix}#{options.uri_prefix ? ''}#{url}"
request options, (err, res, body) ->
throw err if err
if options.expect_status_code
res.statusCode.should.equal options.expect_status_code
if options.response_json
body = JSON.parse body
this.err = err
this.res = res
this.body = body
this.options = options
callback.apply this, [err, res, body]
exports.post = (url, options, callback) ->
options.method = 'POST'
return exports.get url, options, callback
exports.defaultOptions = (options) ->
default_options =
uri_prefix: '/'
expect_status_code: 200
response_json: true
exports.default_options = deepmerge default_options, options
exports.default_options = {}

View File

@@ -1,13 +1,20 @@
process.env.NODE_ENV = 'test'
global.fs = require 'fs'
global.async = require 'async'
require("chai").should()
if process.env.COV_TEST == 'true'
require('coffee-coverage').register
path: 'relative'
basePath: "#{__dirname}/../.."
exclude: ['test', 'node_modules', '.git', 'sample', 'core/static']
initAll: true
global.config = require '../../config'
global.config.web.listen = 12558
global._ = require 'underscore'
global.fs = require 'fs'
global.async = require 'async'
global.deepmerge = require 'deepmerge'
global.client = require './client'
require("chai").should()