Skip to content

Commit d522eea

Browse files
authored
api-test: use nodejs' http server instead of msw (#729)
Simplify the test cases
1 parent 63429f1 commit d522eea

File tree

14 files changed

+3446
-1853
lines changed

14 files changed

+3446
-1853
lines changed

generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,20 @@
11
{# @pebvariable name="imports" type="java.util.List<java.util.Map<String, String>>" #}
22
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #}
3-
{# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
43
import { {{operations.classname}} } from "../../api";
54

65
{% for import in imports -%}
76
import { {{import.classname}} } from '../{{import.filename}}';
87
{% endfor %}
98

10-
import { http, HttpResponse } from "msw";
11-
import { setupServer } from "msw/node";
12-
import { deepEqual, equal } from "assert";
9+
import { createServer } from "http";
10+
import { deepEqual, equal, ok } from "assert";
1311

1412
const pkg = require("../../../../package.json");
1513

1614
const channel_access_token = "test_channel_access_token";
1715

18-
describe("{{operations.classname}}", () => {
19-
const server = setupServer();
20-
before(() => { server.listen() });
21-
after(() => { server.close() });
22-
afterEach(() => { server.resetHandlers() })
23-
24-
const client = new {{operations.classname}}({
25-
{% if authMethods != null -%}
26-
channelAccessToken: channel_access_token,
27-
{% endif -%}
28-
});
29-
30-
{% for op in operations.operation %}
31-
it("{{op.nickname}}", async () => {
32-
let requestCount = 0;
33-
34-
const endpoint = "{{endpoint(operations.classname)}}{{op.path}}"
35-
{% for param in op.allParams -%}
36-
{% if param.isNumber or param.isInteger or param.isLong -%}
37-
.replace("{{ "{" + param.paramName + "}" }}", "0") // number
38-
{% elseif param.isString -%}
39-
.replace("{{ "{" + param.paramName + "}" }}", "DUMMY") // string
40-
{% endif -%}
41-
{% endfor %}{# allParams #}
42-
;
43-
44-
server.use(
45-
http.{{ op.httpMethod|lower }}(
46-
endpoint,
47-
({ request, params, cookies }) => {
48-
requestCount++;
49-
50-
{% if authMethods != null -%}
51-
equal(
52-
request.headers.get("Authorization"),
53-
`Bearer ${channel_access_token}`,
54-
);
55-
{% endif -%}
56-
equal(
57-
request.headers.get("User-Agent"),
58-
`${pkg.name}/${pkg.version}`,
59-
);
60-
61-
return HttpResponse.json({});
62-
},
63-
)
64-
);
65-
66-
const res = await client.{{op.nickname}}(
67-
{% for param in op.allParams -%}
16+
{% macro paramDummyValue(param) %}
17+
{# @pebvariable name="param" type="org.openapitools.codegen.CodegenParameter" #}
6818
// {{ param.paramName }}: {{ param.dataType }}
6919
{% if param.isFile -%}
7020
new Blob([]), // paramName={{ param.paramName }}
@@ -83,13 +33,82 @@ describe("{{operations.classname}}", () => {
8333
{% else -%}
8434
// UNKNOWN TYPE: paramName={{param.paramName}} {{ param.dataType }}
8535
{% endif -%}
36+
{% endmacro %}
37+
38+
39+
describe("{{operations.classname}}", () => {
40+
{% for op in operations.operation %}
41+
it("{{op.nickname}}", async () => {
42+
let requestCount = 0;
43+
44+
const server = createServer((req, res) => {
45+
requestCount++;
46+
47+
equal(req.method, "{{ op.httpMethod }}");
48+
const reqUrl = new URL(req.url, "http://localhost/");
49+
equal(reqUrl.pathname, "{{ op.path }}"
50+
{% for param in op.allParams -%}
51+
{% if param.isNumber or param.isInteger or param.isLong -%}
52+
.replace("{{ "{" + param.paramName + "}" }}", "0") // number
53+
{% elseif param.isString -%}
54+
.replace("{{ "{" + param.paramName + "}" }}", "DUMMY") // string
55+
{% endif -%}
56+
{% endfor %}{# allParams #}
57+
);
58+
59+
60+
{% if op.hasQueryParams %}
61+
// Query parameters
62+
const queryParams = new URLSearchParams(reqUrl.search);
63+
{% for param in op.queryParams -%}
64+
equal(queryParams.get("{{param.paramName}}"), String({{ paramDummyValue(param) }}));
65+
{% endfor %}
66+
{% endif %}
67+
{% if authMethods != null -%}
68+
equal(
69+
req.headers["authorization"],
70+
`Bearer ${channel_access_token}`,
71+
);
72+
{% endif -%}
73+
equal(
74+
req.headers["user-agent"],
75+
`${pkg.name}/${pkg.version}`,
76+
);
77+
{% if op.isMultipart %}
78+
ok(
79+
req.headers["content-type"]
80+
.startsWith(`multipart/form-data; boundary=`),
81+
);
82+
{% endif %}
83+
84+
res.writeHead(200, { "Content-Type": "application/json" });
85+
res.end(JSON.stringify({}));
86+
});
87+
await new Promise((resolve) => {
88+
server.listen(0);
89+
server.on('listening', resolve);
90+
});
91+
92+
const serverAddress = server.address();
93+
if (typeof serverAddress === "string" || serverAddress === null) {
94+
throw new Error("Unexpected server address: " + serverAddress);
95+
}
96+
97+
const client = new {{operations.classname}}({
98+
{% if authMethods != null -%}
99+
channelAccessToken: channel_access_token,
100+
{% endif -%}
101+
baseURL: `http://localhost:${String(serverAddress.port)}/`
102+
});
103+
104+
const res = await client.{{op.nickname}}(
105+
{% for param in op.allParams -%}
106+
{{ paramDummyValue(param) }}
86107
{% endfor %}
87108
);
88-
{% if op.isResponseFile %}
89-
res.destroy();
90-
{% endif %}
91109

92110
equal(requestCount, 1);
111+
server.close();
93112
});
94113

95114
{% endfor %}{# op #}

0 commit comments

Comments
 (0)