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