Skip to content

Commit 3d58b5f

Browse files
author
Siddharth Srivastava
committed
fix(middleware-sdk-s3): refactoring tests and getting result for status codes< 200 and >= 300
1 parent fee02d5 commit 3d58b5f

File tree

2 files changed

+151
-129
lines changed

2 files changed

+151
-129
lines changed

packages/middleware-sdk-s3/src/throw-200-exceptions.spec.ts

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
66
const mockNextHandler = jest.fn();
77
const mockStreamCollector = jest.fn();
88
const mockUtf8Encoder = jest.fn();
9+
const mockResponse = jest.fn();
910
const mockConfig = {
1011
streamCollector: mockStreamCollector,
1112
utf8Encoder: mockUtf8Encoder,
@@ -15,13 +16,34 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
1516
jest.clearAllMocks();
1617
});
1718

18-
describe("exceptions for code < 200 and >= 300", () => {
19+
describe("tests for statusCode < 200 and >= 300", () => {
1920
mockStreamCollector.mockResolvedValue(Buffer.from(""));
2021
mockUtf8Encoder.mockReturnValue("");
21-
it("throws an exception if code is less than 200", async () => {
22+
23+
it.each([199, 300])("results for statusCode %i", async (statusCode) => {
24+
mockNextHandler.mockReturnValue({
25+
response: mockResponse,
26+
statusCode,
27+
// headers: {},
28+
// body: "",
29+
});
30+
});
31+
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
32+
const result = await handler({
33+
input: {},
34+
request: new HttpRequest({
35+
hostname: "s3.us-east-1.amazonaws.com",
36+
}),
37+
});
38+
expect(result.response).toBe(mockResponse);
39+
40+
it("should throw if response body is empty", async () => {
41+
expect.assertions(3);
42+
mockStreamCollector.mockResolvedValue(Buffer.from(""));
43+
mockUtf8Encoder.mockReturnValue("");
2244
mockNextHandler.mockReturnValue({
2345
response: new HttpResponse({
24-
statusCode: 199,
46+
statusCode: 200,
2547
headers: {},
2648
body: "",
2749
}),
@@ -40,81 +62,34 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
4062
expect(e.message).toEqual("S3 aborted request");
4163
}
4264
});
43-
it("throws an exception if code is greater than or equal to 300", async () => {
65+
66+
it("should throw if response body contains Error tag", async () => {
67+
const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
68+
<Error>
69+
<Code>InternalError</Code>
70+
<Message>We encountered an internal error. Please try again.</Message>
71+
<RequestId>656c76696e6727732072657175657374</RequestId>
72+
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
73+
</Error>`;
74+
mockStreamCollector.mockResolvedValue(Buffer.from(errorBody));
75+
mockUtf8Encoder.mockReturnValue(errorBody);
4476
mockNextHandler.mockReturnValue({
4577
response: new HttpResponse({
46-
statusCode: 300,
78+
statusCode: 200,
4779
headers: {},
4880
body: "",
4981
}),
5082
});
5183
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
52-
try {
53-
await handler({
54-
input: {},
55-
request: new HttpRequest({
56-
hostname: "s3.us-east-1.amazonaws.com",
57-
}),
58-
});
59-
} catch (e) {
60-
expect(e).toBeDefined();
61-
expect(e.name).toEqual("InternalError");
62-
expect(e.message).toEqual("S3 aborted request");
63-
}
64-
});
65-
});
66-
it("should throw if response body is empty", async () => {
67-
expect.assertions(3);
68-
mockStreamCollector.mockResolvedValue(Buffer.from(""));
69-
mockUtf8Encoder.mockReturnValue("");
70-
mockNextHandler.mockReturnValue({
71-
response: new HttpResponse({
72-
statusCode: 200,
73-
headers: {},
74-
body: "",
75-
}),
76-
});
77-
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
78-
try {
79-
await handler({
84+
const { response } = await handler({
8085
input: {},
8186
request: new HttpRequest({
8287
hostname: "s3.us-east-1.amazonaws.com",
8388
}),
8489
});
85-
} catch (e) {
86-
expect(e).toBeDefined();
87-
expect(e.name).toEqual("InternalError");
88-
expect(e.message).toEqual("S3 aborted request");
89-
}
90-
});
91-
92-
it("should throw if response body contains Error tag", async () => {
93-
const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
94-
<Error>
95-
<Code>InternalError</Code>
96-
<Message>We encountered an internal error. Please try again.</Message>
97-
<RequestId>656c76696e6727732072657175657374</RequestId>
98-
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
99-
</Error>`;
100-
mockStreamCollector.mockResolvedValue(Buffer.from(errorBody));
101-
mockUtf8Encoder.mockReturnValue(errorBody);
102-
mockNextHandler.mockReturnValue({
103-
response: new HttpResponse({
104-
statusCode: 200,
105-
headers: {},
106-
body: "",
107-
}),
108-
});
109-
const handler = throw200ExceptionsMiddleware(mockConfig)(mockNextHandler, {} as any);
110-
const { response } = await handler({
111-
input: {},
112-
request: new HttpRequest({
113-
hostname: "s3.us-east-1.amazonaws.com",
114-
}),
90+
expect(HttpResponse.isInstance(response)).toBe(true);
91+
// @ts-ignore
92+
expect(response.statusCode).toBeGreaterThanOrEqual(400);
11593
});
116-
expect(HttpResponse.isInstance(response)).toBe(true);
117-
// @ts-ignore
118-
expect(response.statusCode).toBeGreaterThanOrEqual(400);
11994
});
12095
});

0 commit comments

Comments
 (0)