-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiracktestcodegenerator.js
More file actions
227 lines (204 loc) · 6.46 KB
/
Copy pathminiracktestcodegenerator.js
File metadata and controls
227 lines (204 loc) · 6.46 KB
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
217
218
219
220
221
222
223
224
225
226
227
var addslashes = function(str) { return ("" + str).replace(/[\\"]/g, '\\$&'); };
var miniracktest_codegenerator = function() {
this.headers = function(request) {
var header_name, header_value, headers;
headers = request.headers;
return {
"has_headers": Object.keys(headers).length > 0,
"header_list": (function() {
var results;
results = [];
for (header_name in headers) {
header_value = headers[header_name];
results.push({
"header_name": header_name,
"header_value": header_value
});
}
return results;
})()
};
};
this.body = function(request) {
var json_body, multipart_body, name, raw_body, url_encoded_body, value;
json_body = request.jsonBody;
if (json_body) {
return {
"has_body": true,
"has_json_body": true,
"has_multipart_body_pre": false,
"json_body_object": this.json_body_object(json_body, 2)
};
}
url_encoded_body = request.urlEncodedBody;
if (url_encoded_body) {
return {
"has_body": true,
"has_url_encoded_body": true,
"has_multipart_body_pre": false,
"url_encoded_body": (function() {
var results;
results = [];
for (name in url_encoded_body) {
value = url_encoded_body[name];
results.push({
"name": addslashes(name),
"value": addslashes(value)
});
}
return results;
})()
};
}
multipart_body = request.multipartBody;
if (multipart_body) {
return {
"has_body": true,
"has_multipart_body_pre": true,
"has_multipart_body": false,
"multipart_body": (function() {
var results;
results = [];
for (name in multipart_body) {
value = multipart_body[name];
results.push({
"name": addslashes(name),
"value": addslashes(value)
});
}
return results;
})()
};
}
raw_body = request.body;
if (raw_body) {
if (raw_body.length < 5000) {
return {
"has_body": true,
"has_raw_body": true,
"raw_body": addslashes(raw_body)
};
} else {
return {
"has_body": true,
"has_long_body": true
};
}
}
};
this.json_body_object = function(object, indent) {
var indent_str, indent_str_children, key, s, value;
if (indent == null) {
indent = 0;
}
if (object === null) {
s = "null";
} else if (typeof object === 'string') {
s = "\"" + (addslashes(object)) + "\"";
} else if (typeof object === 'number') {
s = "" + object;
} else if (typeof object === 'boolean') {
s = "" + (object ? "true" : "false");
} else if (typeof object === 'object') {
indent_str = Array(indent + 1).join(' ');
indent_str_children = Array(indent + 2).join(' ');
if (object.length != null) {
s = "[\n" + ((function() {
var i, len, results;
results = [];
for (i = 0, len = object.length; i < len; i++) {
value = object[i];
results.push("" + indent_str_children + (this.json_body_object(value, indent + 1)));
}
return results;
}).call(this)).join(',\n') + ("\n" + indent_str + "]");
} else {
s = "{\n" + ((function() {
var results;
results = [];
for (key in object) {
value = object[key];
results.push(indent_str_children + "\"" + (addslashes(key)) + "\" => " + (this.json_body_object(value, indent + 1)));
}
return results;
}).call(this)).join(',\n') + ("\n" + indent_str + "}");
}
}
return s;
};
this.get_path = function(request) {
return request.getUrl(true).getComponentAtIndex(2);
};
// in order to render correct casted values
this.add_handlebars_helper = function(h) {
h.registerHelper("cast_item_with_action", function(object, key) {
cast = ".must_equal ";
if (object === null) {
cast = "null";
} else if (typeof object === 'string') {
var filters = (
object == 'id' ||
key.includes('access_token') ||
key.includes('refresh_token') ||
key.includes('created') ||
key.includes('updated')
);
if (filters) {
cast = ".wont_be_nil";
} else {
cast += "'" + object + "'";
}
} else if (typeof object === 'number') {
var filters = (
key.includes('created') ||
key.includes('updated')
);
if (filters) {
cast = ".wont_be_nil";
} else {
cast += object;
}
} else if (typeof object === 'boolean') {
cast += (object ? "true" : "false");
} else if (typeof object === 'object') {
cast += "JSON.parse <<-JSON";
cast += "\n " + JSON.stringify(object);
cast += "\n JSON"
}
return cast;
});
};
// implement the generate() method to generate code
this.generate = function(context, requests, options) {
var request = context.getCurrentRequest();
var response = request.getLastExchange();
var generated = "";
// import the Handlebars and init the template with helpers
var Handlebars = require("handlebars-v4.7.9.js");
this.add_handlebars_helper(Handlebars);
var template = Handlebars.compile(readFile("spec.handlebars"));
// we expect the response body to be JSON
var response_body = null;
if (response !== undefined) {
response_body = JSON.parse(response.responseBody);
};
context = {
spec_name: request.name,
spec_description: request.description,
headers: this.headers(request),
request_method: request.method.toLowerCase(),
body: this.body(request),
path: this.get_path(request),
response_body: response_body
};
// render the template with the context
generated += template(context);
return generated
}
}
// set the extension identifier (must be same as the directory name)
miniracktest_codegenerator.identifier = "com.marianposaceanu.miniracktestcodegenerator";
// give a display name to your Code Generator
miniracktest_codegenerator.title = "Minitest, rack-test spec generator (Ruby)";
// call to register function is required
registerCodeGenerator(miniracktest_codegenerator)