|
1 | | -import { map, SCHEMA, struct } from "@smithy/core/schema"; |
| 1 | +import { map, NormalizedSchema, SCHEMA, struct } from "@smithy/core/schema"; |
| 2 | +import { HttpResponse } from "@smithy/protocol-http"; |
2 | 3 | import { HandlerExecutionContext, HttpResponse as IHttpResponse, Schema, SerdeFunctions } from "@smithy/types"; |
3 | | -import { describe, expect, test as it } from "vitest"; |
| 4 | +import { toUtf8 } from "@smithy/util-utf8"; |
| 5 | +import { Readable } from "node:stream"; |
| 6 | +import { describe, expect, test as it, vi } from "vitest"; |
4 | 7 |
|
5 | 8 | import { HttpProtocol } from "./HttpProtocol"; |
6 | 9 | import { FromStringShapeDeserializer } from "./serde/FromStringShapeDeserializer"; |
| 10 | +import { ToStringShapeSerializer } from "./serde/ToStringShapeSerializer"; |
7 | 11 |
|
8 | 12 | describe(HttpProtocol.name, () => { |
9 | 13 | it("ignores http bindings (only HttpBindingProtocol uses them)", async () => { |
@@ -49,4 +53,149 @@ describe(HttpProtocol.name, () => { |
49 | 53 | // headers were ignored |
50 | 54 | }); |
51 | 55 | }); |
| 56 | + |
| 57 | + describe("event stream serde", () => { |
| 58 | + const impl = { |
| 59 | + serializer: new ToStringShapeSerializer({ |
| 60 | + timestampFormat: { default: 7, useTrait: true }, |
| 61 | + }), |
| 62 | + deserializer: new FromStringShapeDeserializer({ |
| 63 | + httpBindings: true, |
| 64 | + timestampFormat: { default: 7, useTrait: true }, |
| 65 | + }), |
| 66 | + getEventStreamMarshaller() { |
| 67 | + return this.serdeContext.eventStreamMarshaller; |
| 68 | + }, |
| 69 | + serdeContext: { |
| 70 | + eventStreamMarshaller: { |
| 71 | + serialize: vi.fn().mockImplementation((eventStream, eventStreamSerializationFn) => { |
| 72 | + return Readable.from({ |
| 73 | + async *[Symbol.asyncIterator]() { |
| 74 | + for await (const inputEvent of eventStream) { |
| 75 | + yield eventStreamSerializationFn(inputEvent); |
| 76 | + } |
| 77 | + }, |
| 78 | + }); |
| 79 | + }), |
| 80 | + deserialize: vi.fn().mockImplementation((body, eventStreamDeserializationFn) => { |
| 81 | + return { |
| 82 | + async *[Symbol.asyncIterator]() { |
| 83 | + for await (const outputEvent of body) { |
| 84 | + yield eventStreamDeserializationFn(outputEvent); |
| 85 | + } |
| 86 | + }, |
| 87 | + }; |
| 88 | + }), |
| 89 | + }, |
| 90 | + }, |
| 91 | + getDefaultContentType() { |
| 92 | + return "unit/test"; |
| 93 | + }, |
| 94 | + }; |
| 95 | + |
| 96 | + const serializeEventStream = (HttpProtocol.prototype as any).serializeEventStream.bind(impl); |
| 97 | + const deserializeEventStream = (HttpProtocol.prototype as any).deserializeEventStream.bind(impl); |
| 98 | + |
| 99 | + const eventStreamUnionSchema = struct( |
| 100 | + "ns", |
| 101 | + "EventStreamStructure", |
| 102 | + { streaming: 1 }, |
| 103 | + ["A", "B", "C"], |
| 104 | + [struct("ns", "A", 0, ["name"], [0]), struct("ns", "B", 0, ["name"], [0]), struct("ns", "C", 0, ["name"], [0])] |
| 105 | + ); |
| 106 | + |
| 107 | + it("serializes event streams", async () => { |
| 108 | + const eventStream = { |
| 109 | + async *[Symbol.asyncIterator]() { |
| 110 | + yield { A: { name: "a" } }; |
| 111 | + yield { B: { name: "b" } }; |
| 112 | + yield { C: { name: "c" } }; |
| 113 | + yield { $unknown: ["D", { name: "d" }] }; |
| 114 | + }, |
| 115 | + }; |
| 116 | + const unionSchema = NormalizedSchema.of(eventStreamUnionSchema); |
| 117 | + |
| 118 | + const requestBody = serializeEventStream({ |
| 119 | + eventStream, |
| 120 | + unionSchema, |
| 121 | + }); |
| 122 | + |
| 123 | + const collect = []; |
| 124 | + for await (const chunk of requestBody) { |
| 125 | + collect.push(chunk); |
| 126 | + } |
| 127 | + expect( |
| 128 | + collect.map((item) => { |
| 129 | + return { |
| 130 | + headers: item.headers, |
| 131 | + body: toUtf8(item.body).replace(/\s+/g, ""), |
| 132 | + }; |
| 133 | + }) |
| 134 | + ).toEqual([ |
| 135 | + { |
| 136 | + headers: { |
| 137 | + ":event-type": { type: "string", value: "A" }, |
| 138 | + ":message-type": { type: "string", value: "event" }, |
| 139 | + ":content-type": { type: "string", value: "unit/test" }, |
| 140 | + }, |
| 141 | + body: `{"name":"a"}`, |
| 142 | + }, |
| 143 | + { |
| 144 | + headers: { |
| 145 | + ":event-type": { type: "string", value: "B" }, |
| 146 | + ":message-type": { type: "string", value: "event" }, |
| 147 | + ":content-type": { type: "string", value: "unit/test" }, |
| 148 | + }, |
| 149 | + body: `{"name":"b"}`, |
| 150 | + }, |
| 151 | + { |
| 152 | + headers: { |
| 153 | + ":event-type": { type: "string", value: "C" }, |
| 154 | + ":message-type": { type: "string", value: "event" }, |
| 155 | + ":content-type": { type: "string", value: "unit/test" }, |
| 156 | + }, |
| 157 | + body: `{"name":"c"}`, |
| 158 | + }, |
| 159 | + { |
| 160 | + headers: { |
| 161 | + ":event-type": { type: "string", value: "D" }, |
| 162 | + ":message-type": { type: "string", value: "event" }, |
| 163 | + ":content-type": { type: "string", value: "unit/test" }, |
| 164 | + }, |
| 165 | + body: `{"name":"d"}`, |
| 166 | + }, |
| 167 | + ]); |
| 168 | + }); |
| 169 | + |
| 170 | + it("deserializes event streams", async () => { |
| 171 | + const response = new HttpResponse({ |
| 172 | + statusCode: 200, |
| 173 | + body: { |
| 174 | + async *[Symbol.asyncIterator]() { |
| 175 | + yield { A: { headers: {}, body: { name: "a" } } }; |
| 176 | + yield { B: { headers: {}, body: { name: "b" } } }; |
| 177 | + yield { C: { headers: {}, body: { name: "c" } } }; |
| 178 | + yield { D: { headers: {}, body: { name: "d" } } }; |
| 179 | + }, |
| 180 | + }, |
| 181 | + }); |
| 182 | + const unionSchema = NormalizedSchema.of(eventStreamUnionSchema); |
| 183 | + |
| 184 | + const asyncIterable = deserializeEventStream({ |
| 185 | + response, |
| 186 | + unionSchema, |
| 187 | + }); |
| 188 | + |
| 189 | + const collect = []; |
| 190 | + for await (const event of asyncIterable) { |
| 191 | + collect.push(event); |
| 192 | + } |
| 193 | + expect(collect).toEqual([ |
| 194 | + { A: { name: "a" } }, |
| 195 | + { B: { name: "b" } }, |
| 196 | + { C: { name: "c" } }, |
| 197 | + { $unknown: { D: { headers: {}, body: { name: "d" } } } }, |
| 198 | + ]); |
| 199 | + }); |
| 200 | + }); |
52 | 201 | }); |
0 commit comments