forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse-descriptions.test.ts
69 lines (64 loc) · 2.49 KB
/
response-descriptions.test.ts
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
import { expectDiagnosticEmpty } from "@typespec/compiler/testing";
import { strictEqual } from "assert";
import { describe, it } from "vitest";
import { getOperationsWithServiceNamespace } from "./test-host.js";
describe("http: response descriptions", () => {
async function getHttpOp(code: string) {
const [ops, diagnostics] = await getOperationsWithServiceNamespace(code);
expectDiagnosticEmpty(diagnostics);
strictEqual(ops.length, 1);
return ops[0];
}
it("use a default message by status code if not specified", async () => {
const op = await getHttpOp(
`
op read(): {@statusCode _: 200, content: string};
`
);
strictEqual(op.responses[0].description, "The request has succeeded.");
});
it("@returns set doc for all success responses", async () => {
const op = await getHttpOp(
`
@error model Error {}
@returnsDoc("A string")
op read(): { @statusCode _: 200, content: string } | { @statusCode _: 201, content: string } | Error;
`
);
strictEqual(op.responses[0].description, "A string");
strictEqual(op.responses[1].description, "A string");
strictEqual(op.responses[2].description, undefined);
});
it("@errors set doc for all success responses", async () => {
const op = await getHttpOp(
`
@error model Error {}
@errorsDoc("Generic error")
op read(): { @statusCode _: 200, content: string } | { @statusCode _: 201, content: string } | Error;
`
);
strictEqual(op.responses[0].description, "The request has succeeded.");
strictEqual(
op.responses[1].description,
"The request has succeeded and a new resource has been created as a result."
);
strictEqual(op.responses[2].description, "Generic error");
});
it("@doc explicitly on a response override the operation returns doc", async () => {
const op = await getHttpOp(
`
@error model Error {}
@error @doc("Not found model") model NotFound {@statusCode _: 404}
@errorsDoc("Generic error")
op read(): { @statusCode _: 200, content: string } | { @statusCode _: 201, content: string } | NotFound | Error ;
`
);
strictEqual(op.responses[0].description, "The request has succeeded.");
strictEqual(
op.responses[1].description,
"The request has succeeded and a new resource has been created as a result."
);
strictEqual(op.responses[2].description, "Not found model");
strictEqual(op.responses[3].description, "Generic error");
});
});