|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { readFile } from "fs/promises"; |
3 | | -import { parseXML } from "../../../source/index.js"; |
| 3 | +import { parseXML, type WebDAVEntityDecoderOptions } from "../../../source/index.js"; |
4 | 4 |
|
5 | 5 | describe("parseXML", function () { |
6 | 6 | it("keeps numeric-looking displaynames", async function () { |
@@ -149,4 +149,86 @@ describe("parseXML", function () { |
149 | 149 | } |
150 | 150 | ]); |
151 | 151 | }); |
| 152 | + |
| 153 | + describe("entityDecoder", function () { |
| 154 | + it("parses XML with entities when entityDecoder is not set", async function () { |
| 155 | + const xml = `<?xml version="1.0"?> |
| 156 | +<d:multistatus xmlns:d="DAV:"> |
| 157 | + <d:response> |
| 158 | + <d:href>/file.txt</d:href> |
| 159 | + <d:propstat> |
| 160 | + <d:prop> |
| 161 | + <displayname>A & B < C</displayname> |
| 162 | + </d:prop> |
| 163 | + <d:status>HTTP/1.1 200 OK</d:status> |
| 164 | + </d:propstat> |
| 165 | + </d:response> |
| 166 | +</d:multistatus>`; |
| 167 | + |
| 168 | + const parsed = await parseXML(xml); |
| 169 | + expect(parsed.multistatus.response).to.have.length(1); |
| 170 | + expect(parsed.multistatus.response[0].propstat.prop.displayname).to.equal("A & B < C"); |
| 171 | + }); |
| 172 | + |
| 173 | + it("parses XML with entities when entityDecoder limit is set", async function () { |
| 174 | + const decoderOptions: WebDAVEntityDecoderOptions = { |
| 175 | + limit: { |
| 176 | + maxTotalExpansions: 0, |
| 177 | + maxExpandedLength: 0 |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + const xml = `<?xml version="1.0"?> |
| 182 | +<d:multistatus xmlns:d="DAV:"> |
| 183 | + <d:response> |
| 184 | + <d:href>/file.txt</d:href> |
| 185 | + <d:propstat> |
| 186 | + <d:prop> |
| 187 | + <displayname>A & B < C</displayname> |
| 188 | + </d:prop> |
| 189 | + <d:status>HTTP/1.1 200 OK</d:status> |
| 190 | + </d:propstat> |
| 191 | + </d:response> |
| 192 | +</d:multistatus>`; |
| 193 | + |
| 194 | + const parsed = await parseXML(xml, { |
| 195 | + attributeNamePrefix: "@", |
| 196 | + attributeParsers: [], |
| 197 | + entityDecoder: decoderOptions, |
| 198 | + tagParsers: [] |
| 199 | + }); |
| 200 | + expect(parsed.multistatus.response).to.have.length(1); |
| 201 | + expect(parsed.multistatus.response[0].propstat.prop.displayname).to.equal("A & B < C"); |
| 202 | + }); |
| 203 | + |
| 204 | + it("applies maxTotalExpansions limit when set", async function () { |
| 205 | + const decoderOptions: WebDAVEntityDecoderOptions = { |
| 206 | + limit: { |
| 207 | + maxTotalExpansions: 1 |
| 208 | + } |
| 209 | + }; |
| 210 | + |
| 211 | + const xml = `<?xml version="1.0"?> |
| 212 | +<d:multistatus xmlns:d="DAV:"> |
| 213 | + <d:response> |
| 214 | + <d:href>/file.txt</d:href> |
| 215 | + <d:propstat> |
| 216 | + <d:prop> |
| 217 | + <displayname>A & B</displayname> |
| 218 | + </d:prop> |
| 219 | + <d:status>HTTP/1.1 200 OK</d:status> |
| 220 | + </d:propstat> |
| 221 | + </d:response> |
| 222 | +</d:multistatus>`; |
| 223 | + |
| 224 | + const parsed = await parseXML(xml, { |
| 225 | + attributeNamePrefix: "@", |
| 226 | + attributeParsers: [], |
| 227 | + entityDecoder: decoderOptions, |
| 228 | + tagParsers: [] |
| 229 | + }); |
| 230 | + expect(parsed.multistatus.response).to.have.length(1); |
| 231 | + expect(parsed.multistatus.response[0].propstat.prop.displayname).to.equal("A & B"); |
| 232 | + }); |
| 233 | + }); |
152 | 234 | }); |
0 commit comments