-
Notifications
You must be signed in to change notification settings - Fork 59
/
add-tests.coffee
96 lines (74 loc) · 2.45 KB
/
add-tests.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
91
92
93
94
95
96
async = require 'async'
_ = require 'underscore'
csonschema = require 'csonschema'
Test = require './test'
parseSchema = (source) ->
if source.contains('$schema')
#jsonschema
# @response.schema = JSON.parse @response.schema
JSON.parse source
else
csonschema.parse source
# @response.schema = csonschema.parse @response.schema
parseHeaders = (raml) ->
return {} unless raml
headers = {}
for key, v of raml
headers[key] = v.example
headers
# addTests(raml, tests, [parent], callback)
addTests = (raml, tests, parent, callback) ->
# Handle 3th optional param
if _.isFunction(parent)
callback = parent
parent = null
return callback() unless raml.resources
# Iterate endpoint
async.each raml.resources, (resource, callback) ->
path = resource.relativeUri
params = {}
# Apply parent properties
if parent
path = parent.path + path
params = _.clone parent.params
# Setup param
if resource.uriParameters
for key, param of resource.uriParameters
params[key] = param.example
# In case of issue #8, resource does not define methods
resource.methods ?= []
# Iterate response method
async.each resource.methods, (api, callback) ->
method = api.method.toUpperCase()
# Iterate response status
for status, res of api.responses
# Append new test to tests
test = new Test
tests.push test
# Update test
test.name = "#{method} #{path} -> #{status}"
# Update test.request
test.request.path = path
test.request.method = method
test.request.headers = parseHeaders(api.headers)
if api.body?['application/json']
test.request.headers['Content-Type'] = 'application/json'
try
test.request.body = JSON.parse api.body['application/json']?.example
catch
console.warn "invalid request example of #{test.name}"
test.request.params = params
# Update test.response
test.response.status = status
test.response.schema = []
if (raml.schemas)
test.response.schema = raml.schemas
if (res?.body?['application/json']?.schema)
test.response.schema.push parseSchema res.body['application/json'].schema
callback()
, (err) ->
return callback(err) if err
# Recursive
addTests resource, tests, {path, params}, callback
, callback
module.exports = addTests