Skip to content

Commit 8807455

Browse files
authored
test(core/protocols): add Query error deserialization unit test (#7535)
1 parent c44350d commit 8807455

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

packages/core/src/submodules/protocols/query/AwsQueryProtocol.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
import { TypeRegistry } from "@smithy/core/schema";
12
import { HttpResponse } from "@smithy/protocol-http";
3+
import { ServiceException, ServiceExceptionOptions } from "@smithy/smithy-client";
4+
import { StaticErrorSchema } from "@smithy/types";
25
import { describe, expect, test as it } from "vitest";
36

47
import { context } from "../test-schema.spec";
58
import { AwsQueryProtocol } from "./AwsQueryProtocol";
69

710
describe(AwsQueryProtocol.name, () => {
11+
class GiraffeServiceException extends ServiceException {
12+
constructor(options: ServiceExceptionOptions) {
13+
super(options);
14+
Object.setPrototypeOf(this, GiraffeServiceException.prototype);
15+
}
16+
}
17+
18+
const syntheticNamespace = "smithy.ts.sdk.synthetic.com.amazonaws.giraffes";
19+
TypeRegistry.for(syntheticNamespace).registerError(
20+
[-3, syntheticNamespace, "GiraffeServiceException", 0, [], []] satisfies StaticErrorSchema,
21+
GiraffeServiceException
22+
);
23+
824
it("decorates service exceptions with unmodeled fields", async () => {
925
const httpResponse = new HttpResponse({
1026
statusCode: 400,
@@ -41,4 +57,72 @@ describe(AwsQueryProtocol.name, () => {
4157
},
4258
});
4359
});
60+
61+
it("should copy Error.Code to error.name and Error.Message to error.message", async () => {
62+
const httpResponse = new HttpResponse({
63+
statusCode: 400,
64+
headers: {},
65+
body: Buffer.from(
66+
"<Exception>" +
67+
"<Error>" +
68+
"<Type>Sender</Type>" +
69+
"<Code>GiraffeServiceException</Code>" +
70+
"<Message>A giraffe has eaten your umbrella</Message>" +
71+
"</Error>" +
72+
"</Exception>"
73+
),
74+
});
75+
76+
const protocol = new AwsQueryProtocol({
77+
version: "1999-12-31",
78+
defaultNamespace: "com.amazonaws.giraffes",
79+
xmlNamespace: "ns",
80+
});
81+
82+
const actual = await protocol
83+
.deserializeResponse(
84+
{
85+
namespace: "ns",
86+
name: "Empty",
87+
traits: 0,
88+
input: "unit" as const,
89+
output: [3, "ns", "EmptyOutput", 0, [], []],
90+
},
91+
context,
92+
httpResponse
93+
)
94+
.catch((e) => {
95+
return e;
96+
});
97+
98+
const expected = Object.assign(
99+
new GiraffeServiceException({
100+
name: "GiraffeServiceException",
101+
$fault: "client",
102+
message: "A giraffe has eaten your umbrella",
103+
$metadata: {
104+
cfId: undefined,
105+
extendedRequestId: undefined,
106+
httpStatusCode: 400,
107+
requestId: undefined,
108+
},
109+
}),
110+
{
111+
Type: "Sender",
112+
Code: "GiraffeServiceException",
113+
Error: {
114+
Code: "GiraffeServiceException",
115+
Message: "A giraffe has eaten your umbrella",
116+
Type: "Sender",
117+
},
118+
}
119+
);
120+
121+
expect(actual.constructor).toBe(expected.constructor);
122+
expect(actual.name).toEqual(expected.name);
123+
expect(actual.message).toEqual(expected.message);
124+
expect(actual.Type).toEqual(expected.Type);
125+
expect(actual.Code).toEqual(expected.Code);
126+
expect(actual.Error).toEqual(expected.Error);
127+
});
44128
});

0 commit comments

Comments
 (0)