-
Notifications
You must be signed in to change notification settings - Fork 59
/
test.coffee
90 lines (73 loc) · 1.9 KB
/
test.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
chai = require 'chai'
request = require 'request'
_ = require 'underscore'
async = require 'async'
assert = chai.assert
chai.use(require 'chai2-json-schema')
String::contains = (it) ->
@indexOf(it) != -1
class Test
constructor: () ->
@name = ''
@skip = false
@request =
server: ''
path: ''
method: 'GET'
params: {}
query: {}
headers: {}
body: {}
@response =
status: ''
schema: null
headers: null
body: null
url: () ->
path = @request.server + @request.path
for key, value of @request.params
path = path.replace "{#{key}}", value
return path
run: (callback) ->
assertResponse = @assertResponse
options = _.pick @request, 'headers', 'method'
options['url'] = @url()
options['body'] = JSON.stringify @request.body
options['qs'] = @request.query
async.waterfall [
(callback) ->
request options, (error, response, body) ->
callback null, error, response, body
,
(error, response, body, callback) ->
assertResponse(error, response, body)
callback()
], callback
assertResponse: (error, response, body) =>
assert.isNull error
assert.isNotNull response, 'Response'
# Status code
assert.equal response.statusCode, @response.status, """
Got unexpected response code:
#{body}
Error
"""
# Body
if @response.schema
schema = @response.schema
validateJson = _.partial JSON.parse, body
body = '[empty]' if body is ''
assert.doesNotThrow validateJson, JSON.SyntaxError, """
Invalid JSON:
#{body}
Error
"""
json = validateJson()
assert.jsonSchema json, schema, """
Got unexpected response body:
#{JSON.stringify(json, null, 4)}
Error
"""
# Update @response
@response.body = json
module.exports = Test