|
| 1 | +import { struct } from "@smithy/core/schema"; |
| 2 | +import { HttpRequest } from "@smithy/protocol-http"; |
| 3 | +import { toBase64 } from "@smithy/util-base64"; |
| 4 | +import { describe, expect, test as it } from "vitest"; |
| 5 | + |
| 6 | +import { cbor } from "./cbor"; |
| 7 | +import { dateToTag } from "./parseCborBody"; |
| 8 | +import { SmithyRpcV2CborProtocol } from "./SmithyRpcV2CborProtocol"; |
| 9 | + |
| 10 | +describe(SmithyRpcV2CborProtocol.name, () => { |
| 11 | + const bytes = (arr: number[]) => Buffer.from(arr); |
| 12 | + |
| 13 | + const testCases = [ |
| 14 | + { |
| 15 | + name: "document with timestamp and blob", |
| 16 | + schema: struct( |
| 17 | + "MyExtendedDocument", |
| 18 | + {}, |
| 19 | + { |
| 20 | + timestamp: [() => "time", {}], |
| 21 | + blob: [() => "blob", {}], |
| 22 | + } |
| 23 | + ), |
| 24 | + input: { |
| 25 | + bool: true, |
| 26 | + int: 5, |
| 27 | + float: -3.001, |
| 28 | + timestamp: new Date(1_000_000), |
| 29 | + blob: bytes([97, 98, 99, 100]), |
| 30 | + }, |
| 31 | + expected: { |
| 32 | + request: {}, |
| 33 | + body: { |
| 34 | + bool: true, |
| 35 | + int: 5, |
| 36 | + float: -3.001, |
| 37 | + timestamp: dateToTag(new Date(1_000_000)), |
| 38 | + blob: bytes([97, 98, 99, 100]), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "write to header and query", |
| 44 | + schema: struct( |
| 45 | + "MyExtendedDocument", |
| 46 | + {}, |
| 47 | + { |
| 48 | + bool: [, { httpQuery: "bool" }], |
| 49 | + timestamp: [ |
| 50 | + () => "time", |
| 51 | + { |
| 52 | + httpHeader: "timestamp", |
| 53 | + }, |
| 54 | + ], |
| 55 | + blob: [ |
| 56 | + () => "blob", |
| 57 | + { |
| 58 | + httpHeader: "blob", |
| 59 | + }, |
| 60 | + ], |
| 61 | + prefixHeaders: [, { httpPrefixHeaders: "anti-" }], |
| 62 | + searchParams: [, { httpQueryParams: {} }], |
| 63 | + } |
| 64 | + ), |
| 65 | + input: { |
| 66 | + bool: true, |
| 67 | + timestamp: new Date(1_000_000), |
| 68 | + blob: bytes([97, 98, 99, 100]), |
| 69 | + prefixHeaders: { |
| 70 | + pasto: "cheese dodecahedron", |
| 71 | + clockwise: "left", |
| 72 | + }, |
| 73 | + searchParams: { |
| 74 | + a: 1, |
| 75 | + b: 2, |
| 76 | + }, |
| 77 | + }, |
| 78 | + expected: { |
| 79 | + request: { |
| 80 | + headers: { |
| 81 | + timestamp: new Date(1_000_000).toISOString(), |
| 82 | + blob: toBase64(bytes([97, 98, 99, 100])), |
| 83 | + "anti-clockwise": "left", |
| 84 | + "anti-pasto": "cheese dodecahedron", |
| 85 | + }, |
| 86 | + query: { bool: "true", a: "1", b: "2" }, |
| 87 | + }, |
| 88 | + body: {}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "timestamp with header", |
| 93 | + schema: struct( |
| 94 | + "MyShape", |
| 95 | + {}, |
| 96 | + { |
| 97 | + myHeader: [ |
| 98 | + , |
| 99 | + { |
| 100 | + httpHeader: "my-header", |
| 101 | + }, |
| 102 | + ], |
| 103 | + myTimestamp: [() => "time", {}], |
| 104 | + } |
| 105 | + ), |
| 106 | + input: { |
| 107 | + myHeader: "header!", |
| 108 | + myTimestamp: new Date(0), |
| 109 | + }, |
| 110 | + expected: { |
| 111 | + request: { |
| 112 | + headers: { |
| 113 | + ["my-header"]: "header!", |
| 114 | + }, |
| 115 | + }, |
| 116 | + body: { |
| 117 | + myTimestamp: dateToTag(new Date(0)), |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + ]; |
| 122 | + |
| 123 | + for (const testCase of testCases) { |
| 124 | + it(`should serialize HTTP Requests: ${testCase.name}`, async () => { |
| 125 | + const protocol = new SmithyRpcV2CborProtocol(); |
| 126 | + expect(protocol).toBeDefined(); |
| 127 | + |
| 128 | + const httpRequest = await protocol.serializeRequest( |
| 129 | + { |
| 130 | + input: testCase.schema, |
| 131 | + output: void 0, |
| 132 | + traits: {}, |
| 133 | + }, |
| 134 | + testCase.input, |
| 135 | + { |
| 136 | + endpointV2: { |
| 137 | + url: new URL("https://example.com/"), |
| 138 | + }, |
| 139 | + } |
| 140 | + ); |
| 141 | + |
| 142 | + const body = httpRequest.body; |
| 143 | + httpRequest.body = void 0; |
| 144 | + |
| 145 | + expect(httpRequest).toEqual( |
| 146 | + new HttpRequest({ |
| 147 | + protocol: "https:", |
| 148 | + hostname: "example.com", |
| 149 | + ...testCase.expected.request, |
| 150 | + }) |
| 151 | + ); |
| 152 | + |
| 153 | + expect(cbor.deserialize(body)).toEqual(testCase.expected.body); |
| 154 | + }); |
| 155 | + } |
| 156 | +}); |
0 commit comments