-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathapi_test.pebble
More file actions
225 lines (191 loc) · 7.94 KB
/
api_test.pebble
File metadata and controls
225 lines (191 loc) · 7.94 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
{# @pebvariable name="imports" type="java.util.List<java.util.Map<String, String>>" #}
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #}
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
import { {{operations.classname}} } from "../../api.js";
{% for import in imports -%}
import { {{import.classname}} } from '../{{import.filename}}.js';
{% endfor %}
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";
import { describe, it } from "vitest";
const channel_access_token = "test_channel_access_token";
// This is not a perfect multipart/form-data parser,
// but it works for the purpose of this test.
function parseForm(
arrayBuffer: ArrayBuffer,
): Record<string, string | Blob> {
const uint8Array = new Uint8Array(arrayBuffer);
const text = new TextDecoder().decode(uint8Array);
const boundary = text.match(/^--[^\r\n]+/)![0];
// split to parts, and drop first and last empty parts
const parts = text.split(new RegExp(boundary + "(?:\\r\\n|--)")).slice(1, -1);
const result: Record<string, string | Blob> = {};
for (const part of parts) {
const headerEnd = part.indexOf("\r\n\r\n");
if (headerEnd === -1) continue;
const headers = part.slice(0, headerEnd);
const content = part.slice(headerEnd + 4);
const nameMatch = headers.match(/name="([^"]+)"/);
const fileNameMatch = headers.match(/filename="([^"]+)"/);
if (nameMatch) {
const name = nameMatch[1];
if (fileNameMatch) {
// it's a file
const contentTypeMatch = headers.match(/Content-Type:\s*(\S+)/i);
const contentType = contentTypeMatch
? contentTypeMatch[1]
: "application/octet-stream";
result[name] = new Blob([content.replace(/\r\n$/, "")], {
type: contentType,
});
} else {
// basic field
const value = content.trim();
result[name] = value;
}
}
}
return result;
}
{% macro paramDummyValue(param) %}
{# @pebvariable name="param" type="org.openapitools.codegen.CodegenParameter" #}
// {{ param.paramName }}: {{ param.dataType }}
{% if param.isFile -%}
new Blob([]), // paramName={{ param.paramName }}
{% elseif param.isModel -%}
{} as unknown as {{ param.dataType }}, // paramName={{ param.paramName }}
{% elseif param.isArray -%}
new Set(["DUMMY1", "DUMMY2"]) as unknown as {{ param.dataType }},
{% elseif param.isEnum -%}
"DUMMY" as unknown as {{ param.dataType }}, // paramName={{ param.paramName }}(enum)
{% elseif param.isExplode -%}
"DUMMY" as unknown as {{ param.dataType}}, // paramName={{param.paramName}}(enum)
{% elseif param.isString -%}
"DUMMY", // {{param.paramName}}(string)
{% elseif param.isNumber or param.isInteger or param.isLong -%}
0, // paramName={{param.paramName}}(number or int or long)
{% elseif param.isBoolean -%}
true, // paramName={{param.paramName}}
{% else -%}
// UNKNOWN TYPE: paramName={{param.paramName}} {{ param.dataType }}
{% endif -%}
{% endmacro %}
{% macro paramDummyValueString(param) %}
{# @pebvariable name="param" type="org.openapitools.codegen.CodegenParameter" #}
// {{ param.paramName }}: {{ param.dataType }}
{% if param.isFile -%}
new Blob([]), // paramName={{ param.paramName }}
{% elseif param.isModel -%}
{} as unknown as {{ param.dataType }}, // paramName={{ param.paramName }}
{% elseif param.isArray -%}
"DUMMY1,DUMMY2" as unknown as {{ param.dataType }},
{% elseif param.isEnum -%}
"DUMMY" as unknown as {{ param.dataType }}, // paramName={{ param.paramName }}(enum)
{% elseif param.isExplode -%}
"DUMMY" as unknown as {{ param.dataType}}, // paramName={{param.paramName}}(enum)
{% elseif param.isString -%}
"DUMMY", // {{param.paramName}}(string)
{% elseif param.isNumber or param.isInteger or param.isLong -%}
0, // paramName={{param.paramName}}(number or int or long)
{% elseif param.isBoolean -%}
true, // paramName={{param.paramName}}
{% else -%}
// UNKNOWN TYPE: paramName={{param.paramName}} {{ param.dataType }}
{% endif -%}
{% endmacro %}
{% macro renderTest(operations, op, authMethods, withHttpInfo) %}
{# @pebvariable name="withHttpInfo" type="java.lang.Boolean" #}
{# @pebvariable name="op" type="org.openapitools.codegen.CodegenOperation" #}
it("{{op.nickname}}{% if withHttpInfo %}WithHttpInfo{% endif %}", async () => {
let requestCount = 0;
const server = createServer((req, res) => {
requestCount++;
equal(req.method, "{{ op.httpMethod }}");
ok(req.url)
const reqUrl = new URL(req.url, "http://localhost/");
equal(reqUrl.pathname, "{{ op.path }}"
{% for param in op.allParams -%}
{% if param.isNumber or param.isInteger or param.isLong -%}
.replace("{{ "{" + param.paramName + "}" }}", "0") // number
{% elseif param.isString -%}
.replace("{{ "{" + param.paramName + "}" }}", "DUMMY") // string
{% endif -%}
{% endfor %}{# allParams #}
);
{% if op.hasQueryParams %}
// Query parameters
const queryParams = new URLSearchParams(reqUrl.search);
{% for param in op.queryParams -%}
equal(queryParams.get("{{param.baseName}}"), String({{ paramDummyValueString(param) }}));
{% endfor %}
{% endif %}
{% if authMethods != null -%}
equal(
req.headers["authorization"],
`Bearer ${channel_access_token}`,
);
{% endif -%}
equal(
req.headers["user-agent"],
"@line/bot-sdk/1.0.0-test",
);
{% if op.isMultipart %}
const contentType = req.headers["content-type"];
ok(contentType);
ok(
contentType
.startsWith(`multipart/form-data; boundary=`),
);
{% endif %}
{% if op.hasFormParams and op.isMultipart %}
let data: Buffer[] = [];
req.on('data', chunk => {
data.push(chunk);
});
req.on('end', () => {
// Combine the data chunks into a single Buffer
const buffer = Buffer.concat(data);
// Convert Buffer to ArrayBuffer
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
// Form parameters
const formData = parseForm(arrayBuffer);
{% for param in op.formParams -%}
equal(formData["{{param.paramName}}"], String({{ paramDummyValueString(param) }}));
{% endfor %}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
});
{% else %}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
{% endif %}
});
await new Promise((resolve) => {
server.listen(0);
server.on('listening', resolve);
});
const serverAddress = server.address();
if (typeof serverAddress === "string" || serverAddress === null) {
throw new Error("Unexpected server address: " + serverAddress);
}
const client = new {{operations.classname}}({
{% if authMethods != null -%}
channelAccessToken: channel_access_token,
{% endif -%}
baseURL: `http://localhost:${String(serverAddress.port)}/`
});
const res = await client.{{op.nickname}}{% if withHttpInfo %}WithHttpInfo{% endif %}(
{% for param in op.allParams -%}
{{ paramDummyValue(param) }}
{% endfor %}
);
equal(requestCount, 1);
server.close();
});
{% endmacro %}
describe("{{operations.classname}}", () => {
{% for op in operations.operation %}
{{ renderTest(operations, op, authMethods, true) }}
{{ renderTest(operations, op, authMethods, false) }}
{% endfor %}{# op #}
});