|
| 1 | +import type { SerdeContext } from "@smithy/types"; |
| 2 | +import { toUtf8 } from "@smithy/util-utf8"; |
| 3 | + |
| 4 | +import { parseXmlBody } from "./parseXmlBody"; |
| 5 | + |
| 6 | +describe(parseXmlBody.name, () => { |
| 7 | + const context = { |
| 8 | + utf8Encoder: toUtf8, |
| 9 | + }; |
| 10 | + it("should parse xml", async () => { |
| 11 | + const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?> |
| 12 | +<ListAllMyBucketsResult> |
| 13 | + <Buckets> |
| 14 | + <Bucket> |
| 15 | + <CreationDate>timestamp</CreationDate> |
| 16 | + <Name>string</Name> |
| 17 | + </Bucket> |
| 18 | + </Buckets> |
| 19 | + <Owner> |
| 20 | + <DisplayName>string</DisplayName> |
| 21 | + <ID>string</ID> |
| 22 | + </Owner> |
| 23 | +</ListAllMyBucketsResult> |
| 24 | +`); |
| 25 | + const parsed = await parseXmlBody(xml, context as any as SerdeContext); |
| 26 | + expect(parsed).toEqual({ |
| 27 | + Buckets: { Bucket: { CreationDate: "timestamp", Name: "string" } }, |
| 28 | + Owner: { DisplayName: "string", ID: "string" }, |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should throw on incomplete xml", async () => { |
| 33 | + const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?> |
| 34 | +<ListAllMyBucketsResult> |
| 35 | + <Buckets> |
| 36 | + <Bucket> |
| 37 | + <CreationDate>timestamp</CreationDate> |
| 38 | + <Name>string</Name> |
| 39 | + </Bucket> |
| 40 | + </Buckets> |
| 41 | + <Owner> |
| 42 | + <DisplayName>string</DisplayName> |
| 43 | + <ID>string</ID> |
| 44 | + </Owner> |
| 45 | +`); |
| 46 | + const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _); |
| 47 | + expect(parsed.toString()).toEqual(`Error: Unclosed tag 'ListAllMyBucketsResult'.:2:1`); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should throw on incomplete xml", async () => { |
| 51 | + const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?> |
| 52 | +<ListAllMyBucketsResult> |
| 53 | + <Buckets> |
| 54 | + <Bucket> |
| 55 | + <CreationDate>timestamp</Creatio |
| 56 | +`); |
| 57 | + const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _); |
| 58 | + expect(parsed.toString()).toEqual(`Error: Closing tag 'Creatio' doesn't have proper closing.:6:1`); |
| 59 | + }); |
| 60 | +}); |
0 commit comments