forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.test.ts
115 lines (102 loc) · 3.3 KB
/
responses.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
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
import { Model } from "@typespec/compiler";
import { expectDiagnosticEmpty, expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { describe, it } from "vitest";
import { compileOperations, getOperationsWithServiceNamespace } from "./test-host.js";
describe("http: responses", () => {
it("issues diagnostics for duplicate body decorator", async () => {
const [_, diagnostics] = await compileOperations(
`
model Foo {
foo: string;
}
model Bar {
bar: string;
}
@route("/")
namespace root {
@get
op read(): { @body body1: Foo, @body body2: Bar };
}
`
);
expectDiagnostics(diagnostics, [{ code: "@typespec/http/duplicate-body" }]);
});
it("issues diagnostics for invalid content types", async () => {
const [_, diagnostics] = await compileOperations(
`
model Foo {
foo: string;
}
model TextPlain {
contentType: "text/plain";
}
namespace root {
@route("/test1")
@get
op test1(): { @header contentType: string, @body body: Foo };
@route("/test2")
@get
op test2(): { @header contentType: 42, @body body: Foo };
@route("/test3")
@get
op test3(): { @header contentType: "application/json" | TextPlain, @body body: Foo };
}
`
);
expectDiagnostics(diagnostics, [
{ code: "@typespec/http/content-type-string" },
{ code: "@typespec/http/content-type-string" },
{ code: "@typespec/http/content-type-string" },
]);
});
it("supports any casing for string literal 'Content-Type' header properties.", async () => {
const [routes, diagnostics] = await getOperationsWithServiceNamespace(
`
model Foo {}
@test
namespace Test {
@route("/test1")
@get
op test1(): { @header "content-Type": "text/html", @body body: Foo };
@route("/test2")
@get
op test2(): { @header "CONTENT-type": "text/plain", @body body: Foo };
@route("/test3")
@get
op test3(): { @header "content-type": "application/json", @body body: Foo };
}
`
);
expectDiagnosticEmpty(diagnostics);
strictEqual(routes.length, 3);
deepStrictEqual(routes[0].responses[0].responses[0].body?.contentTypes, ["text/html"]);
deepStrictEqual(routes[1].responses[0].responses[0].body?.contentTypes, ["text/plain"]);
deepStrictEqual(routes[2].responses[0].responses[0].body?.contentTypes, ["application/json"]);
});
// Regression test for https://github.com/microsoft/typespec/issues/328
it("empty response model becomes body if it has children", async () => {
const [routes, diagnostics] = await getOperationsWithServiceNamespace(
`
@route("/") op read(): A;
model A {}
model B extends A {
foo: "B";
b: string;
}
model C extends A {
foo: "C";
c: string;
}
`
);
expectDiagnosticEmpty(diagnostics);
strictEqual(routes.length, 1);
const responses = routes[0].responses;
strictEqual(responses.length, 1);
const response = responses[0];
const body = response.responses[0].body;
ok(body);
strictEqual((body.type as Model).name, "A");
});
});