|
| 1 | +// @jest-environment jsdom |
| 2 | +import { streamCollector } from "@aws-sdk/fetch-http-handler"; |
| 3 | +import { SdkStreamMixin } from "@aws-sdk/types"; |
| 4 | +import { toBase64 } from "@aws-sdk/util-base64-browser"; |
| 5 | +import { toHex } from "@aws-sdk/util-hex-encoding"; |
| 6 | +import { toUtf8 } from "@aws-sdk/util-utf8-browser"; |
| 7 | + |
| 8 | +import { sdkStreamMixin } from "./sdk-stream-mixin"; |
| 9 | + |
| 10 | +jest.mock("@aws-sdk/fetch-http-handler"); |
| 11 | +jest.mock("@aws-sdk/util-base64-browser"); |
| 12 | +jest.mock("@aws-sdk/util-hex-encoding"); |
| 13 | +jest.mock("@aws-sdk/util-utf8-browser"); |
| 14 | + |
| 15 | +const mockStreamCollectorReturn = Uint8Array.from([117, 112, 113]); |
| 16 | +(streamCollector as jest.Mock).mockReturnValue(mockStreamCollectorReturn); |
| 17 | + |
| 18 | +describe(sdkStreamMixin.name, () => { |
| 19 | + const expectAllTransformsToFail = async (sdkStream: SdkStreamMixin) => { |
| 20 | + const transformMethods: Array<keyof SdkStreamMixin> = [ |
| 21 | + "transformToByteArray", |
| 22 | + "transformToString", |
| 23 | + "transformToWebStream", |
| 24 | + ]; |
| 25 | + for (const method of transformMethods) { |
| 26 | + try { |
| 27 | + await sdkStream[method](); |
| 28 | + fail(new Error("expect subsequent tranform to fail")); |
| 29 | + } catch (error) { |
| 30 | + expect(error.message).toContain("The stream has already been transformed"); |
| 31 | + } |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + let originalReadableStreamCtr = global.ReadableStream; |
| 36 | + const mockReadableStream = jest.fn(); |
| 37 | + class ReadableStream { |
| 38 | + constructor() { |
| 39 | + mockReadableStream(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + let payloadStream: ReadableStream; |
| 44 | + |
| 45 | + beforeAll(() => { |
| 46 | + global.ReadableStream = ReadableStream as any; |
| 47 | + }); |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + originalReadableStreamCtr = global.ReadableStream; |
| 51 | + jest.clearAllMocks(); |
| 52 | + payloadStream = new ReadableStream(); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + global.ReadableStream = originalReadableStreamCtr; |
| 57 | + }); |
| 58 | + |
| 59 | + it("should throw if input stream is not a Blob or Web Stream instance", () => { |
| 60 | + const originalBlobCtr = global.Blob; |
| 61 | + global.Blob = undefined; |
| 62 | + global.ReadableStream = undefined; |
| 63 | + try { |
| 64 | + sdkStreamMixin({}); |
| 65 | + fail("expect unexpected stream to fail"); |
| 66 | + } catch (e) { |
| 67 | + expect(e.message).toContain("nexpected stream implementation"); |
| 68 | + global.Blob = originalBlobCtr; |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + describe("transformToByteArray", () => { |
| 73 | + it("should transform binary stream to byte array", async () => { |
| 74 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 75 | + const byteArray = await sdkStream.transformToByteArray(); |
| 76 | + expect(streamCollector as jest.Mock).toBeCalledWith(payloadStream); |
| 77 | + expect(byteArray).toEqual(mockStreamCollectorReturn); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should fail any subsequent tranform calls", async () => { |
| 81 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 82 | + await sdkStream.transformToByteArray(); |
| 83 | + await expectAllTransformsToFail(sdkStream); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("transformToString", () => { |
| 88 | + let originalTextDecoder = global.TextDecoder; |
| 89 | + const mockDecode = jest.fn(); |
| 90 | + global.TextDecoder = jest.fn().mockImplementation(function () { |
| 91 | + return { decode: mockDecode }; |
| 92 | + }); |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + originalTextDecoder = global.TextDecoder; |
| 96 | + jest.clearAllMocks(); |
| 97 | + }); |
| 98 | + |
| 99 | + afterEach(() => { |
| 100 | + global.TextDecoder = originalTextDecoder; |
| 101 | + }); |
| 102 | + |
| 103 | + it.each([ |
| 104 | + [undefined, toUtf8], |
| 105 | + ["utf8", toUtf8], |
| 106 | + ["utf-8", toUtf8], |
| 107 | + ["base64", toBase64], |
| 108 | + ["hex", toHex], |
| 109 | + ])("should transform to string with %s encoding", async (encoding, encodingFn) => { |
| 110 | + const mockEncodedStringValue = `a string with ${encoding} encoding`; |
| 111 | + (encodingFn as jest.Mock).mockReturnValueOnce(mockEncodedStringValue); |
| 112 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 113 | + const str = await sdkStream.transformToString(encoding); |
| 114 | + expect(streamCollector).toBeCalled(); |
| 115 | + expect(encodingFn).toBeCalledWith(mockStreamCollectorReturn); |
| 116 | + expect(str).toEqual(mockEncodedStringValue); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should use TexDecoder to handle other encodings", async () => { |
| 120 | + const utfLabel = "windows-1251"; |
| 121 | + mockDecode.mockReturnValue(`a string with ${utfLabel} encoding`); |
| 122 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 123 | + const str = await sdkStream.transformToString(utfLabel); |
| 124 | + expect(global.TextDecoder).toBeCalledWith(utfLabel); |
| 125 | + expect(str).toEqual(`a string with ${utfLabel} encoding`); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should throw if TextDecoder is not available", async () => { |
| 129 | + global.TextDecoder = null; |
| 130 | + const utfLabel = "windows-1251"; |
| 131 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 132 | + try { |
| 133 | + await sdkStream.transformToString(utfLabel); |
| 134 | + fail("expect transformToString to throw when TextDecoder is not available"); |
| 135 | + } catch (error) { |
| 136 | + expect(error.message).toContain("TextDecoder is not available"); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it("should fail any subsequent tranform calls", async () => { |
| 141 | + const sdkStream = sdkStreamMixin(payloadStream); |
| 142 | + await sdkStream.transformToString(); |
| 143 | + await expectAllTransformsToFail(sdkStream); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe("transformToWebStream with ReadableStream payload", () => { |
| 148 | + it("should return the payload if it is Web Stream instance", () => { |
| 149 | + const payloadStream = new ReadableStream(); |
| 150 | + const sdkStream = sdkStreamMixin(payloadStream as any); |
| 151 | + const transformed = sdkStream.transformToWebStream(); |
| 152 | + expect(transformed).toBe(payloadStream); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should fail any subsequent tranform calls", async () => { |
| 156 | + const payloadStream = new ReadableStream(); |
| 157 | + const sdkStream = sdkStreamMixin(payloadStream as any); |
| 158 | + sdkStream.transformToWebStream(); |
| 159 | + await expectAllTransformsToFail(sdkStream); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe("transformToWebStream with Blob payload", () => { |
| 164 | + let originalBlobCtr = global.Blob; |
| 165 | + const mockBlob = jest.fn(); |
| 166 | + const mockBlobStream = jest.fn(); |
| 167 | + class Blob { |
| 168 | + constructor() { |
| 169 | + mockBlob(); |
| 170 | + } |
| 171 | + |
| 172 | + stream() { |
| 173 | + return mockBlobStream(); |
| 174 | + } |
| 175 | + } |
| 176 | + global.Blob = Blob as any; |
| 177 | + |
| 178 | + beforeEach(() => { |
| 179 | + global.ReadableStream = undefined; |
| 180 | + originalBlobCtr = global.Blob; |
| 181 | + jest.clearAllMocks(); |
| 182 | + }); |
| 183 | + |
| 184 | + afterEach(() => { |
| 185 | + global.Blob = originalBlobCtr; |
| 186 | + }); |
| 187 | + |
| 188 | + it("should transform blob to web stream with Blob.stream()", () => { |
| 189 | + mockBlobStream.mockReturnValue("transformed"); |
| 190 | + const payloadStream = new Blob(); |
| 191 | + const sdkStream = sdkStreamMixin(payloadStream as any); |
| 192 | + const transformed = sdkStream.transformToWebStream(); |
| 193 | + expect(transformed).toBe("transformed"); |
| 194 | + expect(mockBlobStream).toBeCalled(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("should fail if Blob.stream() is not available", async () => { |
| 198 | + class Blob { |
| 199 | + constructor() { |
| 200 | + mockBlob(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + global.Blob = Blob as any; |
| 205 | + const payloadStream = new Blob(); |
| 206 | + const sdkStream = sdkStreamMixin(payloadStream as any); |
| 207 | + try { |
| 208 | + sdkStream.transformToWebStream(); |
| 209 | + fail("expect to fail"); |
| 210 | + } catch (e) { |
| 211 | + expect(e.message).toContain("Please make sure the Blob.stream() is polyfilled"); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + it("should fail any subsequent tranform calls", async () => { |
| 216 | + const payloadStream = new Blob(); |
| 217 | + const sdkStream = sdkStreamMixin(payloadStream as any); |
| 218 | + sdkStream.transformToWebStream(); |
| 219 | + await expectAllTransformsToFail(sdkStream); |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
0 commit comments