-
Notifications
You must be signed in to change notification settings - Fork 280
/
execute-transaction.coffee
140 lines (114 loc) · 4.29 KB
/
execute-transaction.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
flattenHeaders = require './flatten-headers'
gavel = require 'gavel'
http = require 'http'
https = require 'https'
html = require 'html'
url = require 'url'
os = require 'os'
packageConfig = require './../package.json'
logger = require './logger'
indent = ' '
String::trunc = (n) ->
if this.length>n
return this.substr(0,n-1)+'...'
else
return this
String::startsWith = (str) ->
return this.slice(0, str.length) is str
executeTransaction = (transaction, callback) ->
configuration = transaction['configuration']
origin = transaction['origin']
request = transaction['request']
response = transaction['response']
parsedUrl = url.parse configuration['server']
flatHeaders = flattenHeaders request['headers']
# Add Dredd user agent if no User-Agent present
if flatHeaders['User-Agent'] == undefined
system = os.type() + ' ' + os.release() + '; ' + os.arch()
flatHeaders['User-Agent'] = "Dredd/" + \
packageConfig['version'] + \
" ("+ system + ")"
# Add length of body if no Content-Length present
caseInsensitiveMap = {}
for key, value of flatHeaders
caseInsensitiveMap[key.toLowerCase()] = key
if caseInsensitiveMap['content-length'] == undefined and request['body'] != ''
flatHeaders['Content-Length'] = request['body'].length
if configuration.options.header.length > 0
for header in configuration.options.header
splitHeader = header.split(':')
flatHeaders[splitHeader[0]] = splitHeader[1]
options =
host: parsedUrl['hostname']
port: parsedUrl['port']
path: request['uri']
method: request['method']
headers: flatHeaders
description = origin['resourceGroupName'] + \
' > ' + origin['resourceName'] + \
' > ' + origin['actionName'] + \
' > ' + origin['exampleName'] + \
':\n' + indent + options['method'] + \
' ' + options['path'] + \
' ' + JSON.stringify(request['body']).trunc(20)
test =
status: ''
title: options['method'] + ' ' + options['path']
message: description
configuration.emitter.emit 'test start', test
if configuration.options['dry-run']
logger.info "Dry run, skipping API Tests..."
return callback()
else if configuration.options.method.length > 0 and not (request.method in configuration.options.method)
configuration.emitter.emit 'test skip', test
return callback()
else
buffer = ""
handleRequest = (res) ->
res.on 'data', (chunk) ->
buffer = buffer + chunk
req.on 'error', (error) ->
configuration.emitter.emit 'test error', error, test if error
res.on 'end', () ->
real =
headers: res.headers
body: buffer
status: res.statusCode
expected =
headers: flattenHeaders response['headers']
body: response['body']
status: response['status']
expected['schema'] = response['schema'] if response['schema']
gavel.isValid real, expected, 'response', (error, isValid) ->
configuration.emitter.emit 'test error', error, test if error
if isValid
test.status = "pass"
test.actual = real
test.expected = expected
test.request = options
configuration.emitter.emit 'test pass', test
return callback(null, req, res)
else
gavel.validate real, expected, 'response', (error, result) ->
configuration.emitter.emit 'test error', error, test if error
message = ''
for entity, data of result
for entityResult in data['results']
message += entity + ": " + entityResult['message'] + "\n"
test =
status: "fail",
title: options['method'] + ' ' + options['path'],
message: message
actual: real
expected: expected
request: options
start: test.start
configuration.emitter.emit 'test fail', test
return callback()
if configuration.server.startsWith 'https'
req = https.request options, handleRequest
else
req = http.request options, handleRequest
req.write request['body'] if request['body'] != ''
req.end()
module.exports = executeTransaction