|
1 | 1 | /// <reference types="mocha" /> |
2 | | -import { expect } from "chai"; |
3 | | -import { S3 } from "./S3"; |
4 | | -import { SerializeMiddleware, BuildMiddleware } from "@aws-sdk/types"; |
5 | 2 | import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 3 | +import { BuildMiddleware, SerializeMiddleware } from "@aws-sdk/types"; |
| 4 | +import chai from "chai"; |
| 5 | +import chaiAsPromised from "chai-as-promised"; |
| 6 | +import { PassThrough } from "stream"; |
| 7 | + |
| 8 | +import { S3 } from "./S3"; |
| 9 | + |
| 10 | +chai.use(chaiAsPromised); |
| 11 | +const { expect } = chai; |
6 | 12 |
|
7 | 13 | describe("endpoint", () => { |
8 | 14 | it("users can override endpoint from client.", async () => { |
@@ -93,3 +99,75 @@ describe("Accesspoint ARN", async () => { |
93 | 99 | ); |
94 | 100 | }); |
95 | 101 | }); |
| 102 | + |
| 103 | +describe("Throw 200 response", () => { |
| 104 | + const response = { |
| 105 | + statusCode: 200, |
| 106 | + headers: {}, |
| 107 | + body: new PassThrough(), |
| 108 | + }; |
| 109 | + const client = new S3({ |
| 110 | + region: "us-west-2", |
| 111 | + requestHandler: { |
| 112 | + handle: async () => ({ |
| 113 | + response, |
| 114 | + }), |
| 115 | + }, |
| 116 | + }); |
| 117 | + const errorBody = `<?xml version="1.0" encoding="UTF-8"?> |
| 118 | + <Error> |
| 119 | + <Code>InternalError</Code> |
| 120 | + <Message>We encountered an internal error. Please try again.</Message> |
| 121 | + <RequestId>656c76696e6727732072657175657374</RequestId> |
| 122 | + <HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId> |
| 123 | + </Error>`; |
| 124 | + const params = { |
| 125 | + Bucket: "bucket", |
| 126 | + Key: "key", |
| 127 | + CopySource: "source", |
| 128 | + }; |
| 129 | + |
| 130 | + beforeEach(() => { |
| 131 | + response.body = new PassThrough(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should throw if CopyObject() return with 200 and empty payload", async () => { |
| 135 | + response.body.end(""); |
| 136 | + return expect(client.copyObject(params)).to.eventually.be.rejectedWith("S3 aborted request"); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should throw if CopyObject() return with 200 and error preamble", async () => { |
| 140 | + response.body.end(errorBody); |
| 141 | + return expect(client.copyObject(params)).to.eventually.be.rejectedWith( |
| 142 | + "We encountered an internal error. Please try again." |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should throw if UploadPartCopy() return with 200 and empty payload", async () => { |
| 147 | + response.body.end(""); |
| 148 | + return expect(client.uploadPartCopy({ ...params, UploadId: "id", PartNumber: 1 })).to.eventually.be.rejectedWith( |
| 149 | + "S3 aborted request" |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should throw if UploadPartCopy() return with 200 and error preamble", async () => { |
| 154 | + response.body.end(errorBody); |
| 155 | + return expect(client.uploadPartCopy({ ...params, UploadId: "id", PartNumber: 1 })).to.eventually.be.rejectedWith( |
| 156 | + "We encountered an internal error. Please try again." |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should throw if CompleteMultipartUpload() return with 200 and empty payload", async () => { |
| 161 | + response.body.end(""); |
| 162 | + return expect(client.completeMultipartUpload({ ...params, UploadId: "id" })).to.eventually.be.rejectedWith( |
| 163 | + "S3 aborted request" |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should throw if CompleteMultipartUpload() return with 200 and error preamble", async () => { |
| 168 | + response.body.end(errorBody); |
| 169 | + return expect(client.completeMultipartUpload({ ...params, UploadId: "id" })).to.eventually.be.rejectedWith( |
| 170 | + "We encountered an internal error. Please try again." |
| 171 | + ); |
| 172 | + }); |
| 173 | +}); |
0 commit comments