|
| 1 | +import { JsonBuilder } from "../"; |
| 2 | +import { Member } from "@aws/types"; |
| 3 | + |
| 4 | +describe("JsonBuilder", () => { |
| 5 | + describe("structures", () => { |
| 6 | + const structure: Member = { |
| 7 | + shape: { |
| 8 | + type: "structure", |
| 9 | + required: [], |
| 10 | + members: { |
| 11 | + foo: { shape: { type: "string" } }, |
| 12 | + bar: { shape: { type: "string" } }, |
| 13 | + baz: { |
| 14 | + shape: { type: "string" }, |
| 15 | + locationName: "quux" |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + }; |
| 20 | + const jsonBody = new JsonBuilder(jest.fn(), jest.fn()); |
| 21 | + |
| 22 | + it("should serialize known properties of a structure", () => { |
| 23 | + const toSerialize = { foo: "fizz", bar: "buzz" }; |
| 24 | + expect(jsonBody.build(structure, toSerialize)).toBe( |
| 25 | + JSON.stringify(toSerialize) |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should ignore unknown properties", () => { |
| 30 | + const toSerialize = { foo: "fizz", bar: "buzz" }; |
| 31 | + expect(jsonBody.build(structure, { ...toSerialize, pop: "weasel" })).toBe( |
| 32 | + JSON.stringify(toSerialize) |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should serialize properties to the locationNames", () => { |
| 37 | + expect(jsonBody.build(structure, { baz: "value" })).toEqual( |
| 38 | + JSON.stringify({ quux: "value" }) |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should throw if a scalar value is provided", () => { |
| 43 | + for (let scalar of ["string", 123, true, null, void 0]) { |
| 44 | + expect(() => jsonBody.build(structure, scalar)).toThrow(); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("lists", () => { |
| 50 | + const listShape: Member = { |
| 51 | + shape: { |
| 52 | + type: "list", |
| 53 | + member: { shape: { type: "string" } } |
| 54 | + } |
| 55 | + }; |
| 56 | + const jsonBody = new JsonBuilder(jest.fn(), jest.fn()); |
| 57 | + |
| 58 | + it("should serialize arrays", () => { |
| 59 | + expect(jsonBody.build(listShape, ["foo", "bar", "baz"])).toEqual( |
| 60 | + JSON.stringify(["foo", "bar", "baz"]) |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should serialize iterators", () => { |
| 65 | + const iterator = function*() { |
| 66 | + yield "foo"; |
| 67 | + yield "bar"; |
| 68 | + yield "baz"; |
| 69 | + }; |
| 70 | + |
| 71 | + expect(jsonBody.build(listShape, iterator())).toEqual( |
| 72 | + JSON.stringify(["foo", "bar", "baz"]) |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should throw if a non-iterable value is provided", () => { |
| 77 | + for (let nonIterable of [{}, 123, true, null, void 0]) { |
| 78 | + expect(() => jsonBody.build(listShape, nonIterable)).toThrow(); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("maps", () => { |
| 84 | + const mapShape: Member = { |
| 85 | + shape: { |
| 86 | + type: "map", |
| 87 | + key: { shape: { type: "string" } }, |
| 88 | + value: { shape: { type: "number" } } |
| 89 | + } |
| 90 | + }; |
| 91 | + const jsonBody = new JsonBuilder(jest.fn(), jest.fn()); |
| 92 | + |
| 93 | + it("should serialize objects", () => { |
| 94 | + const object = { |
| 95 | + foo: 0, |
| 96 | + bar: 1, |
| 97 | + baz: 2 |
| 98 | + }; |
| 99 | + |
| 100 | + expect(jsonBody.build(mapShape, object)).toEqual(JSON.stringify(object)); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should serialize [key, value] iterables (like ES6 maps)", () => { |
| 104 | + const iterator = function*() { |
| 105 | + yield ["foo", 0]; |
| 106 | + yield ["bar", 1]; |
| 107 | + yield ["baz", 2]; |
| 108 | + }; |
| 109 | + |
| 110 | + expect(jsonBody.build(mapShape, iterator())).toEqual( |
| 111 | + JSON.stringify({ |
| 112 | + foo: 0, |
| 113 | + bar: 1, |
| 114 | + baz: 2 |
| 115 | + }) |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should throw if a non-iterable and non-object value is provided", () => { |
| 120 | + for (let nonIterable of [123, true, null, void 0]) { |
| 121 | + expect(() => jsonBody.build(mapShape, nonIterable)).toThrow(); |
| 122 | + } |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe("blobs", () => { |
| 127 | + const blobShape: Member = { shape: { type: "blob" } }; |
| 128 | + const base64Encode = jest.fn(arg => arg); |
| 129 | + const utf8Decode = jest.fn(arg => arg); |
| 130 | + const jsonBody = new JsonBuilder(base64Encode, utf8Decode); |
| 131 | + |
| 132 | + beforeEach(() => { |
| 133 | + base64Encode.mockClear(); |
| 134 | + utf8Decode.mockClear(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should base64 encode ArrayBuffers", () => { |
| 138 | + jsonBody.build(blobShape, new ArrayBuffer(2)); |
| 139 | + |
| 140 | + expect(base64Encode.mock.calls.length).toBe(1); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should base64 encode ArrayBufferViews", () => { |
| 144 | + jsonBody.build(blobShape, Uint8Array.from([0])); |
| 145 | + |
| 146 | + expect(base64Encode.mock.calls.length).toBe(1); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should utf8 decode and base64 encode strings", () => { |
| 150 | + jsonBody.build(blobShape, "foo" as any); |
| 151 | + |
| 152 | + expect(base64Encode.mock.calls.length).toBe(1); |
| 153 | + expect(utf8Decode.mock.calls.length).toBe(1); |
| 154 | + }); |
| 155 | + |
| 156 | + it("should throw if a non-binary value is provided", () => { |
| 157 | + for (let nonBinary of [[], {}, 123, true, null, void 0]) { |
| 158 | + expect(() => jsonBody.build(blobShape, nonBinary)).toThrow(); |
| 159 | + } |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe("timestamps", () => { |
| 164 | + const timestampShape: Member = { shape: { type: "timestamp" } }; |
| 165 | + const date = new Date("2017-05-22T19:33:14.175Z"); |
| 166 | + const timestamp = 1495481594; |
| 167 | + const jsonBody = new JsonBuilder(jest.fn(), jest.fn()); |
| 168 | + |
| 169 | + it("should convert Date objects to epoch timestamps", () => { |
| 170 | + expect(jsonBody.build(timestampShape, date)).toBe( |
| 171 | + JSON.stringify(timestamp) |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it("should convert date strings to epoch timestamps", () => { |
| 176 | + expect(jsonBody.build(timestampShape, date.toUTCString() as any)).toBe( |
| 177 | + JSON.stringify(timestamp) |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it("should preserve numbers as epoch timestamps", () => { |
| 182 | + expect(jsonBody.build(timestampShape, timestamp as any)).toBe( |
| 183 | + JSON.stringify(timestamp) |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should throw if a value that cannot be converted to a time object is provided", () => { |
| 188 | + for (let nonTime of [[], {}, true, null, void 0, new ArrayBuffer(0)]) { |
| 189 | + expect(() => jsonBody.build(timestampShape, nonTime)).toThrow(); |
| 190 | + } |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe("scalars", () => { |
| 195 | + it("should echo back scalars in their JSON-ified form", () => { |
| 196 | + const cases: Iterable<[Member, any]> = [ |
| 197 | + [{ shape: { type: "string" } }, "string"], |
| 198 | + [{ shape: { type: "number" } }, 1], |
| 199 | + [{ shape: { type: "boolean" } }, true] |
| 200 | + ]; |
| 201 | + const jsonBody = new JsonBuilder(jest.fn(), jest.fn()); |
| 202 | + |
| 203 | + for (let [shape, scalar] of cases) { |
| 204 | + expect(jsonBody.build(shape, scalar)).toBe(JSON.stringify(scalar)); |
| 205 | + } |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments