-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathProtocolLib.spec.ts
More file actions
180 lines (146 loc) · 5.35 KB
/
ProtocolLib.spec.ts
File metadata and controls
180 lines (146 loc) · 5.35 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { ServiceException } from "@smithy/smithy-client";
import { type HttpResponse as IHttpResponse } from "@smithy/types";
import { describe, expect, test as it } from "vitest";
import { ProtocolLib } from "./ProtocolLib";
describe("ProtocolLib", () => {
interface QueryCompatibleErrorShape {
RequestId?: string;
Error?: {
Code?: string;
Type?: string;
Message?: string;
};
}
class AwsQueryIncompatibleError extends ServiceException implements QueryCompatibleErrorShape {
public RequestId?: string;
public Error?: {
Code?: string;
Type?: string;
Message?: string;
};
public constructor(opts: any) {
super({
name: "AwsQueryIncompatibleError",
$fault: "client",
...opts,
});
this.name = "AwsQueryIncompatibleError";
}
}
describe("decorateServiceException", () => {
describe("queryCompat mode", () => {
it("should handle missing error.Error gracefully when x-amzn-query-error header is absent", () => {
const lib = new ProtocolLib(true);
const error = new AwsQueryIncompatibleError({
$metadata: {
httpStatusCode: 403,
requestId: "test-request-id",
},
});
const additions = {
message: "User is not authorized to perform this action",
// Note: no Error property since x-amzn-query-error header was absent
};
const result = lib.decorateServiceException(error, additions);
expect(result.$fault).toBe("client");
expect(result.$metadata.requestId).toBe("test-request-id");
expect(result.RequestId).toBe("test-request-id");
expect(result.Error).toBeDefined();
expect(result.Error.Type).toBeUndefined();
expect(result.Error.Code).toBeUndefined();
});
it("should properly populate error.Error when it exists on the unmodeled error response structure", () => {
const lib = new ProtocolLib(true);
const error = new AwsQueryIncompatibleError({
$metadata: {
httpStatusCode: 403,
requestId: "test-request-id",
},
});
const additions = {
Error: {
Code: "SomeOtherException",
Type: "Sender",
Message: "I forgot my umbrella.",
},
};
const result = lib.decorateServiceException(error, additions);
expect(result.Error).toBeDefined();
expect(result.Error.Code).toBe("SomeOtherException");
expect(result.Error.Type).toBe("Sender");
expect(result.Error.Message).toBe("I forgot my umbrella.");
});
it("should use Message from additions when error.Error is missing", () => {
const lib = new ProtocolLib(true);
const error = new AwsQueryIncompatibleError({
$fault: "client" as const,
$metadata: {
httpStatusCode: 400,
requestId: "test-request-id-2",
},
Message: "To shreds you say?",
});
const additions = {
Message: "Since this is a spaceship, between zero and one.",
};
const result = lib.decorateServiceException(error, additions);
// The message property is set from Message
expect(result.message).toBe("Since this is a spaceship, between zero and one.");
expect(result.Error.Message).toBe("Since this is a spaceship, between zero and one.");
});
});
describe("non-queryCompat mode", () => {
it("should delegate to decorateServiceException without modification", () => {
const lib = new ProtocolLib(false);
const exception = new AwsQueryIncompatibleError({
$fault: "server" as const,
$metadata: {
httpStatusCode: 500,
},
});
const additions = {
message: "load shedding",
};
const result = lib.decorateServiceException(exception, additions);
// In non-queryCompat mode, Error property should not be set
expect((result as any).Error).toBeUndefined();
expect((result as any).RequestId).toBeUndefined();
expect(result.message).toEqual("load shedding");
});
});
});
describe("setQueryCompatError", () => {
it("should set Error property when x-amzn-query-error header is present", () => {
const lib = new ProtocolLib(true);
const output: Record<string, any> = {
message: "Access denied",
};
const response = {
statusCode: 403,
headers: {
"x-amzn-query-error": "AccessDeniedException;Sender",
},
body: undefined,
} as IHttpResponse;
lib.setQueryCompatError(output, response);
expect(output.Error).toBeDefined();
expect(output.Error.Code).toBe("AccessDeniedException");
expect(output.Error.Type).toBe("Sender");
expect(output.Error.Message).toBe("Access denied");
});
it("should not set Error property when x-amzn-query-error header is absent", () => {
const lib = new ProtocolLib(true);
const output: Record<string, any> = {
message: "Access denied",
};
const response = {
statusCode: 403,
headers: {},
body: undefined,
};
lib.setQueryCompatError(output, response);
expect(output.Error).toBeUndefined();
expect(output.message).toBe("Access denied");
});
});
});