|
| 1 | +import { describe, expect, test as it } from "vitest"; |
| 2 | + |
| 3 | +import { parseXML } from "./xml-parser"; |
| 4 | +import { parseXML as parseXMLBrowser } from "./xml-parser.browser"; |
| 5 | + |
| 6 | +describe("xml parsing", () => { |
| 7 | + for (const { name, parse } of [ |
| 8 | + { name: "fast-xml-parser", parse: parseXML }, |
| 9 | + { name: "DOMParser", parse: parseXMLBrowser }, |
| 10 | + ]) { |
| 11 | + describe(name, () => { |
| 12 | + it("should parse a valid xml string without xml header", () => { |
| 13 | + const xml = `<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/"> |
| 14 | +<AssumeRoleResult> |
| 15 | + <Credentials> |
| 16 | + <AccessKeyId>STS_AR_ACCESS_KEY_ID</AccessKeyId> |
| 17 | + <SecretAccessKey>STS_AR_SECRET_ACCESS_KEY</SecretAccessKey> |
| 18 | + <SessionToken>STS_AR_SESSION_TOKEN_us-west-2</SessionToken> |
| 19 | + <Expiration>3000-01-01T00:00:00.000Z</Expiration> |
| 20 | + </Credentials> |
| 21 | +</AssumeRoleResult> |
| 22 | +<ResponseMetadata> |
| 23 | + <RequestId>01234567-89ab-cdef-0123-456789abcdef</RequestId> |
| 24 | +</ResponseMetadata> |
| 25 | +</AssumeRoleResponse>`; |
| 26 | + const object = parse(xml); |
| 27 | + expect(object).toEqual({ |
| 28 | + AssumeRoleResponse: { |
| 29 | + AssumeRoleResult: { |
| 30 | + Credentials: { |
| 31 | + AccessKeyId: "STS_AR_ACCESS_KEY_ID", |
| 32 | + Expiration: "3000-01-01T00:00:00.000Z", |
| 33 | + SecretAccessKey: "STS_AR_SECRET_ACCESS_KEY", |
| 34 | + SessionToken: "STS_AR_SESSION_TOKEN_us-west-2", |
| 35 | + }, |
| 36 | + }, |
| 37 | + ResponseMetadata: { |
| 38 | + RequestId: "01234567-89ab-cdef-0123-456789abcdef", |
| 39 | + }, |
| 40 | + xmlns: "https://sts.amazonaws.com/doc/2011-06-15/", |
| 41 | + }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should parse ListBuckets response XML with xml header", () => { |
| 46 | + const xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 47 | +<ListAllMyBucketsResult> |
| 48 | + <Buckets> |
| 49 | + <Bucket> |
| 50 | + <BucketArn>string</BucketArn> |
| 51 | + <BucketRegion>string</BucketRegion> |
| 52 | + <CreationDate>timestamp</CreationDate> |
| 53 | + <Name>string</Name> |
| 54 | + </Bucket> |
| 55 | + </Buckets> |
| 56 | + <Owner> |
| 57 | + <DisplayName>string</DisplayName> |
| 58 | + <ID>string</ID> |
| 59 | + </Owner> |
| 60 | + <ContinuationToken>string</ContinuationToken> |
| 61 | + <Prefix>string</Prefix> |
| 62 | +</ListAllMyBucketsResult>`; |
| 63 | + const object = parse(xml); |
| 64 | + expect(object).toEqual({ |
| 65 | + ListAllMyBucketsResult: { |
| 66 | + Buckets: { |
| 67 | + Bucket: { |
| 68 | + BucketArn: "string", |
| 69 | + BucketRegion: "string", |
| 70 | + CreationDate: "timestamp", |
| 71 | + Name: "string", |
| 72 | + }, |
| 73 | + }, |
| 74 | + ContinuationToken: "string", |
| 75 | + Owner: { |
| 76 | + DisplayName: "string", |
| 77 | + ID: "string", |
| 78 | + }, |
| 79 | + Prefix: "string", |
| 80 | + }, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should parse xml (custom)", () => { |
| 85 | + const xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 86 | +<struct> |
| 87 | + <empty></empty> |
| 88 | + <text>abcdefg</text> |
| 89 | + <duplicate>dup1</duplicate> |
| 90 | + <duplicate>dup2</duplicate> |
| 91 | + <duplicate>dup3</duplicate> |
| 92 | + <spaced> s p a c e d </spaced> |
| 93 | + <nested> |
| 94 | + <empty></empty> |
| 95 | + <text>abcdefg</text> |
| 96 | + <duplicate>dup1</duplicate> |
| 97 | + <duplicate>dup2</duplicate> |
| 98 | + <duplicate>dup3</duplicate> |
| 99 | + <spaced> s p a c e d </spaced> |
| 100 | + </nested> |
| 101 | +</struct>`; |
| 102 | + const object = parse(xml); |
| 103 | + expect(object).toEqual({ |
| 104 | + struct: { |
| 105 | + empty: "", |
| 106 | + text: "abcdefg", |
| 107 | + duplicate: ["dup1", "dup2", "dup3"], |
| 108 | + spaced: " s p a c e d ", |
| 109 | + nested: { |
| 110 | + empty: "", |
| 111 | + text: "abcdefg", |
| 112 | + duplicate: ["dup1", "dup2", "dup3"], |
| 113 | + spaced: " s p a c e d ", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + } |
| 120 | +}); |
0 commit comments