|
| 1 | +import { HttpResponse } from "@smithy/protocol-http"; |
1 | 2 | import { EndpointBearer, SerdeFunctions } from "@smithy/types";
|
2 | 3 | import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
|
3 | 4 |
|
@@ -39,6 +40,12 @@ describe("deserializerMiddleware", () => {
|
39 | 40 | statusCode: 200,
|
40 | 41 | headers: {},
|
41 | 42 | },
|
| 43 | + $metadata: { |
| 44 | + httpStatusCode: 200, |
| 45 | + requestId: undefined, |
| 46 | + extendedRequestId: undefined, |
| 47 | + cfId: undefined, |
| 48 | + }, |
42 | 49 | };
|
43 | 50 |
|
44 | 51 | const mockResponse = {
|
@@ -139,4 +146,66 @@ describe("deserializerMiddleware", () => {
|
139 | 146 | expect(e.$response.body).toEqual("oh no");
|
140 | 147 | }
|
141 | 148 | });
|
| 149 | + |
| 150 | + describe("metadata", () => { |
| 151 | + it("assigns metadata from the response in the event of a deserializer failure", async () => { |
| 152 | + const midware = deserializerMiddleware(mockOptions, async () => { |
| 153 | + JSON.parse(`this isn't JSON`); |
| 154 | + }); |
| 155 | + const handler = midware( |
| 156 | + async () => ({ |
| 157 | + response: new HttpResponse({ |
| 158 | + headers: { |
| 159 | + "x-namespace-requestid": "requestid", |
| 160 | + "x-namespace-id-2": "id2", |
| 161 | + "x-namespace-cf-id": "cf", |
| 162 | + }, |
| 163 | + statusCode: 503, |
| 164 | + }), |
| 165 | + }), |
| 166 | + {} |
| 167 | + ); |
| 168 | + try { |
| 169 | + await handler(mockArgs); |
| 170 | + fail("DeserializerMiddleware should throw"); |
| 171 | + } catch (e) { |
| 172 | + expect(e.$metadata).toEqual({ |
| 173 | + httpStatusCode: 503, |
| 174 | + requestId: "requestid", |
| 175 | + extendedRequestId: "id2", |
| 176 | + cfId: "cf", |
| 177 | + }); |
| 178 | + } |
| 179 | + expect.assertions(1); |
| 180 | + }); |
| 181 | + |
| 182 | + it("assigns any available metadata from the response in the event of a deserializer failure", async () => { |
| 183 | + const midware = deserializerMiddleware(mockOptions, async () => { |
| 184 | + JSON.parse(`this isn't JSON`); |
| 185 | + }); |
| 186 | + const handler = midware( |
| 187 | + async () => ({ |
| 188 | + response: new HttpResponse({ |
| 189 | + statusCode: 301, |
| 190 | + headers: { |
| 191 | + "x-namespace-requestid": "requestid", |
| 192 | + }, |
| 193 | + }), |
| 194 | + }), |
| 195 | + {} |
| 196 | + ); |
| 197 | + try { |
| 198 | + await handler(mockArgs); |
| 199 | + fail("DeserializerMiddleware should throw"); |
| 200 | + } catch (e) { |
| 201 | + expect(e.$metadata).toEqual({ |
| 202 | + httpStatusCode: 301, |
| 203 | + requestId: "requestid", |
| 204 | + extendedRequestId: undefined, |
| 205 | + cfId: undefined, |
| 206 | + }); |
| 207 | + } |
| 208 | + expect.assertions(1); |
| 209 | + }); |
| 210 | + }); |
142 | 211 | });
|
0 commit comments