|
| 1 | +import type { MessageHeaders } from "@smithy/types"; |
| 2 | + |
| 3 | +import { HeaderFormatter, Int64 } from "./HeaderFormatter"; |
| 4 | + |
| 5 | +/** |
| 6 | + * TODO: this test duplicates a test in HeaderMarshaller in eventstream-codec. |
| 7 | + * TODO: when submodules are implemented this should be reunified/deduped. |
| 8 | + */ |
| 9 | +describe("HeaderFormatter", () => { |
| 10 | + const marshaller = new HeaderFormatter(); |
| 11 | + const name = [0x04, 0xf0, 0x9f, 0xa6, 0x84]; |
| 12 | + |
| 13 | + const testCases: Array<[string, Uint8Array, MessageHeaders]> = [ |
| 14 | + [ |
| 15 | + "boolean true headers", |
| 16 | + Uint8Array.from([...name, 0]), |
| 17 | + { |
| 18 | + "🦄": { |
| 19 | + type: "boolean", |
| 20 | + value: true, |
| 21 | + }, |
| 22 | + }, |
| 23 | + ], |
| 24 | + [ |
| 25 | + "boolean false headers", |
| 26 | + Uint8Array.from([...name, 1]), |
| 27 | + { |
| 28 | + "🦄": { |
| 29 | + type: "boolean", |
| 30 | + value: false, |
| 31 | + }, |
| 32 | + }, |
| 33 | + ], |
| 34 | + [ |
| 35 | + "byte headers", |
| 36 | + Uint8Array.from([...name, 2, 0x7f]), |
| 37 | + { |
| 38 | + "🦄": { |
| 39 | + type: "byte", |
| 40 | + value: 127, |
| 41 | + }, |
| 42 | + }, |
| 43 | + ], |
| 44 | + [ |
| 45 | + "short headers", |
| 46 | + Uint8Array.from([...name, 3, 0x7f, 0xff]), |
| 47 | + { |
| 48 | + "🦄": { |
| 49 | + type: "short", |
| 50 | + value: 32767, |
| 51 | + }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + [ |
| 55 | + "integer headers", |
| 56 | + Uint8Array.from([...name, 4, 0x7f, 0xff, 0xff, 0xff]), |
| 57 | + { |
| 58 | + "🦄": { |
| 59 | + type: "integer", |
| 60 | + value: 2147483647, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + [ |
| 65 | + "long headers", |
| 66 | + Uint8Array.from([...name, 5, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), |
| 67 | + { |
| 68 | + "🦄": { |
| 69 | + type: "long", |
| 70 | + value: new Int64(Uint8Array.from([0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])), |
| 71 | + }, |
| 72 | + }, |
| 73 | + ], |
| 74 | + [ |
| 75 | + "binary headers", |
| 76 | + Uint8Array.from([...name, 6, 0x00, 0x08, 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe]), |
| 77 | + { |
| 78 | + "🦄": { |
| 79 | + type: "binary", |
| 80 | + value: Uint8Array.from([0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe]), |
| 81 | + }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + [ |
| 85 | + "string headers", |
| 86 | + Uint8Array.from([ |
| 87 | + ...name, |
| 88 | + 7, |
| 89 | + 0x00, |
| 90 | + 0x2e, |
| 91 | + 0xd8, |
| 92 | + 0xaf, |
| 93 | + 0xd8, |
| 94 | + 0xb3, |
| 95 | + 0xd8, |
| 96 | + 0xaa, |
| 97 | + 0xe2, |
| 98 | + 0x80, |
| 99 | + 0x8c, |
| 100 | + 0xd9, |
| 101 | + 0x86, |
| 102 | + 0xd9, |
| 103 | + 0x88, |
| 104 | + 0xd8, |
| 105 | + 0xb4, |
| 106 | + 0xd8, |
| 107 | + 0xaa, |
| 108 | + 0xd9, |
| 109 | + 0x87, |
| 110 | + 0xe2, |
| 111 | + 0x80, |
| 112 | + 0x8c, |
| 113 | + 0xd9, |
| 114 | + 0x87, |
| 115 | + 0xd8, |
| 116 | + 0xa7, |
| 117 | + 0x20, |
| 118 | + 0xd9, |
| 119 | + 0x86, |
| 120 | + 0xd9, |
| 121 | + 0x85, |
| 122 | + 0xdb, |
| 123 | + 0x8c, |
| 124 | + 0xe2, |
| 125 | + 0x80, |
| 126 | + 0x8c, |
| 127 | + 0xd8, |
| 128 | + 0xb3, |
| 129 | + 0xd9, |
| 130 | + 0x88, |
| 131 | + 0xd8, |
| 132 | + 0xb2, |
| 133 | + 0xd9, |
| 134 | + 0x86, |
| 135 | + 0xd8, |
| 136 | + 0xaf, |
| 137 | + ]), |
| 138 | + { |
| 139 | + "🦄": { |
| 140 | + type: "string", |
| 141 | + value: "دستنوشتهها نمیسوزند", |
| 142 | + }, |
| 143 | + }, |
| 144 | + ], |
| 145 | + [ |
| 146 | + "timestamp headers", |
| 147 | + Uint8Array.from([...name, 8, 0x00, 0x00, 0x01, 0x61, 0x97, 0x16, 0xac, 0xc2]), |
| 148 | + { |
| 149 | + "🦄": { |
| 150 | + type: "timestamp", |
| 151 | + value: new Date(1518658301122), |
| 152 | + }, |
| 153 | + }, |
| 154 | + ], |
| 155 | + [ |
| 156 | + "UUID headers", |
| 157 | + Uint8Array.from([ |
| 158 | + ...name, |
| 159 | + 9, |
| 160 | + 0xff, |
| 161 | + 0xff, |
| 162 | + 0xff, |
| 163 | + 0xff, |
| 164 | + 0xff, |
| 165 | + 0xff, |
| 166 | + 0xff, |
| 167 | + 0xff, |
| 168 | + 0xff, |
| 169 | + 0xff, |
| 170 | + 0xff, |
| 171 | + 0xff, |
| 172 | + 0xff, |
| 173 | + 0xff, |
| 174 | + 0xff, |
| 175 | + 0xff, |
| 176 | + ]), |
| 177 | + { |
| 178 | + "🦄": { |
| 179 | + type: "uuid", |
| 180 | + value: "ffffffff-ffff-ffff-ffff-ffffffffffff", |
| 181 | + }, |
| 182 | + }, |
| 183 | + ], |
| 184 | + [ |
| 185 | + "a sequence of headers", |
| 186 | + Uint8Array.from([ |
| 187 | + 0x04, |
| 188 | + 0xf0, |
| 189 | + 0x9f, |
| 190 | + 0xa6, |
| 191 | + 0x84, |
| 192 | + 0x06, |
| 193 | + 0x00, |
| 194 | + 0x04, |
| 195 | + 0xde, |
| 196 | + 0xad, |
| 197 | + 0xbe, |
| 198 | + 0xef, |
| 199 | + 0x04, |
| 200 | + 0xf0, |
| 201 | + 0x9f, |
| 202 | + 0x8f, |
| 203 | + 0x87, |
| 204 | + 0x00, |
| 205 | + 0x04, |
| 206 | + 0xf0, |
| 207 | + 0x9f, |
| 208 | + 0x90, |
| 209 | + 0x8e, |
| 210 | + 0x07, |
| 211 | + 0x00, |
| 212 | + 0x07, |
| 213 | + 0xe2, |
| 214 | + 0x98, |
| 215 | + 0x83, |
| 216 | + 0xf0, |
| 217 | + 0x9f, |
| 218 | + 0x92, |
| 219 | + 0xa9, |
| 220 | + 0x04, |
| 221 | + 0xf0, |
| 222 | + 0x9f, |
| 223 | + 0x90, |
| 224 | + 0xb4, |
| 225 | + 0x01, |
| 226 | + ]), |
| 227 | + { |
| 228 | + "🦄": { |
| 229 | + type: "binary", |
| 230 | + value: Uint8Array.from([0xde, 0xad, 0xbe, 0xef]), |
| 231 | + }, |
| 232 | + "🏇": { |
| 233 | + type: "boolean", |
| 234 | + value: true, |
| 235 | + }, |
| 236 | + "🐎": { |
| 237 | + type: "string", |
| 238 | + value: "☃💩", |
| 239 | + }, |
| 240 | + "🐴": { |
| 241 | + type: "boolean", |
| 242 | + value: false, |
| 243 | + }, |
| 244 | + }, |
| 245 | + ], |
| 246 | + ]; |
| 247 | + |
| 248 | + describe("#format", () => { |
| 249 | + for (const [description, encoded, decoded] of testCases) { |
| 250 | + it(`should format ${description}`, () => { |
| 251 | + expect(marshaller.format(decoded)).toEqual(encoded); |
| 252 | + }); |
| 253 | + } |
| 254 | + |
| 255 | + it("should throw if it receives an invalid UUID", () => { |
| 256 | + expect(() => |
| 257 | + marshaller.format({ |
| 258 | + uuid: { |
| 259 | + type: "uuid", |
| 260 | + value: "foo", |
| 261 | + }, |
| 262 | + }) |
| 263 | + ).toThrowError("Invalid UUID received"); |
| 264 | + }); |
| 265 | + }); |
| 266 | +}); |
0 commit comments