-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
216 lines (189 loc) · 6.96 KB
/
index.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const JSONSchemaFaker = require('json-schema-faker')
const SwaggerParser = require('@apidevtools/swagger-parser')
const { dump } = require('js-yaml')
const fs = require('fs')
const merge = require('deepmerge')
const defaultOptions = {
generator: {
pathParams: true,
requestBody: true,
optionalParams: true,
useExampleValues: true,
useDefaultValues: true,
inlineSchema: true
},
check: {
status: true,
examples: true,
schema: true
},
contentType: 'application/json'
}
JSONSchemaFaker.format('binary', () => 'file.txt')
async function generateWorkflow (file, options) {
options = merge(defaultOptions, options)
JSONSchemaFaker.option({
alwaysFakeOptionals: options.optionalParams,
useExamplesValue: options.useExampleValues,
useDefaultValue: options.useDefaultValues
})
const swagger = await SwaggerParser.parse(file)
const workflow = {
version: "1.0",
name: swagger.info.title,
config: {
http: {
baseURL: swagger.servers ? swagger.servers[0].url : undefined
}
},
tests: {},
}
const taggedSchemas = []
if (swagger.components?.schemas) {
if (options.generator.inlineSchema) {
workflow.components = {
schemas: swagger.components?.schemas
}
}
for (const schema in swagger.components.schemas) {
taggedSchemas.push({ id: `#/components/schemas/${schema}`, ...swagger.components.schemas[schema] })
}
}
for (const path in swagger.paths) {
for (const method in swagger.paths[path]) {
const step = {
id: swagger.paths[path][method].operationId,
name: swagger.paths[path][method].summary,
http: {
url: swagger.paths[path][method].servers ? swagger.paths[path][method].servers[0].url : path,
method: method.toUpperCase()
}
}
if (swagger.paths[path][method].parameters) {
swagger.paths[path][method].parameters.filter(param => !options.generator.optionalParams ? param.required : true).forEach(param => {
const value =
param.schema?.default
|| param.example
|| (param.examples && Object.keys(param.examples > 0) ? Object.values(param.examples)[0].value : false)
|| param.schema ? JSONSchemaFaker.generate(param.schema, taggedSchemas) : false
if (param.in === 'path' && options.generator.pathParams) {
step.http.url = step.http.url.replace(`{${param.name}}`, value)
}
if (param.in === 'query') {
if (!step.http.params) step.http.params = {}
step.http.params[param.name] = value
}
if (param.in === 'header') {
if (!step.http.headers) step.http.headers = {}
step.http.headers[param.name] = value
}
if (param.in === 'cookie') {
if (!step.http.cookies) step.http.cookies = {}
step.http.cookies[param.name] = value
}
})
}
if (options.generator.requestBody && swagger.paths[path][method].requestBody && (!options.generator.optionalParams ? swagger.paths[path][method].requestBody.required : true)) {
const requestBody = swagger.paths[path][method].requestBody.content
for (const contentType in requestBody) {
const body =
requestBody[contentType].example
|| (requestBody[contentType].examples && Object.keys(requestBody[contentType].examples) > 0) ? Object.values(requestBody[contentType].examples)[0].value : false
|| JSONSchemaFaker.generate(requestBody[contentType].schema, taggedSchemas)
if (!step.http.headers) step.http.headers = {}
const bodyExists = step.http.json || step.http.xml || step.http.body || step.http.form || step.http.formData
switch (contentType) {
case 'application/json':
if (contentType == options.contentType) {
step.http.headers['Content-Type'] = contentType
step.http.headers['accept'] = contentType
step.http.json = body
}
break
case 'application/xml':
if (contentType == options.contentType) {
step.http.headers['Content-Type'] = contentType
step.http.headers['accept'] = contentType
step.http.xml = body
}
break
case 'application/x-www-form-urlencoded':
if (!bodyExists) {
step.http.headers['Content-Type'] = contentType
step.http.form = body
}
break
case 'multipart/form-data':
if (!bodyExists) {
step.http.headers['Content-Type'] = contentType
step.http.formData = body
}
break
case 'text/plain':
if (!bodyExists) {
step.http.headers['Content-Type'] = contentType
step.http.body = body
}
break
default:
step.http.headers['Content-Type'] = contentType
step.http.body = {
file: body
}
}
}
}
if (swagger.paths[path][method].responses) {
const response = Object.values(swagger.paths[path][method].responses)[0]
const responseContent = response.content?.[options.contentType]
if (response) {
if (Object.keys(options.check).length !== 0) step.http.check = {}
if (options.check.status) {
step.http.check.status =
Object.keys(swagger.paths[path][method].responses)[0] === 'default'
? 200
: Number(Object.keys(swagger.paths[path][method].responses)[0])
}
}
if (responseContent) {
if (options.check.schema) {
step.http.check.schema = responseContent.schema
}
if (options.check.examples) {
step.http.check.json = responseContent.example || (responseContent.examples ? Object.values(responseContent.examples)[0].value : undefined)
}
}
}
if (swagger.tags && swagger.paths[path][method].tags) {
swagger.paths[path][method].tags.forEach((tag) => {
if (Object.keys(workflow.tests).includes(tag)) {
workflow.tests[tag].steps.push(step)
} else {
workflow.tests[tag] = {
name: swagger.tags?.find((item) => item.name === tag)?.description,
steps: [step],
}
}
})
} else {
if (!workflow.tests.default) {
workflow.tests.default = {
name: 'Default',
steps: []
}
}
workflow.tests.default.steps.push(step)
}
}
}
return workflow
}
async function generateWorkflowFile (file, output, options) {
return fs.promises.writeFile(output, dump(await generateWorkflow(file, options), {
quotingType: '"'
}))
}
module.exports = {
generateWorkflow,
generateWorkflowFile
}