1
1
{# @pebvariable name="imports" type="java.util.List<java.util.Map<String, String>>" #}
2
2
{# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #}
3
- {# @pebvariable name="authMethods" type="java.util.ArrayList<org.openapitools.codegen.CodegenSecurity>" -#}
4
3
import { {{operations.classname}} } from "../../api";
5
4
6
5
{% for import in imports -%}
7
6
import { {{import.classname}} } from '../{{import.filename}}';
8
7
{% endfor %}
9
8
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";
13
11
14
12
const pkg = require("../../../../package.json");
15
13
16
14
const channel_access_token = "test_channel_access_token";
17
15
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" #}
68
18
// {{ param.paramName }}: {{ param.dataType }}
69
19
{% if param.isFile -%}
70
20
new Blob([]), // paramName={{ param.paramName }}
@@ -83,13 +33,82 @@ describe("{{operations.classname}}", () => {
83
33
{% else -%}
84
34
// UNKNOWN TYPE: paramName={{param.paramName}} {{ param.dataType }}
85
35
{% 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) }}
86
107
{% endfor %}
87
108
);
88
- {% if op.isResponseFile %}
89
- res.destroy();
90
- {% endif %}
91
109
92
110
equal(requestCount, 1);
111
+ server.close();
93
112
});
94
113
95
114
{% endfor %}{# op #}
0 commit comments