|
| 1 | +public class NameKeyedReadTests |
| 2 | +{ |
| 3 | + static readonly RemoteZipOptions rangedOptions = new() |
| 4 | + { |
| 5 | + TailLength = 1024 |
| 6 | + }; |
| 7 | + |
| 8 | + [Test] |
| 9 | + public async Task ReadByNames_MissingNamesAbsent_SingleCoalescedRequest() |
| 10 | + { |
| 11 | + var data = Zips.Padded(4096, ("docs/a.txt", "alpha"), ("docs/b.txt", "beta")); |
| 12 | + var (client, server) = Zips.Serve(data); |
| 13 | + using (client) |
| 14 | + { |
| 15 | + var zip = await RemoteZipArchive.Open(client, "https://example/archive.zip", rangedOptions); |
| 16 | + var contents = await zip.Read(["docs/a.txt", "docs/b.txt", "missing.txt"]); |
| 17 | + |
| 18 | + await Assert.That(contents.Count).IsEqualTo(2); |
| 19 | + await Assert.That(Encoding.UTF8.GetString(contents["docs/a.txt"])).IsEqualTo("alpha"); |
| 20 | + await Assert.That(Encoding.UTF8.GetString(contents["docs/b.txt"])).IsEqualTo("beta"); |
| 21 | + await Assert.That(contents.ContainsKey("missing.txt")).IsFalse(); |
| 22 | + await Assert.That(server.Requests).Count().IsEqualTo(2); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + public async Task ReadByNames_DuplicateNamesCollapse() |
| 28 | + { |
| 29 | + var data = Zips.Padded(4096, ("a.txt", "alpha")); |
| 30 | + var (client, _) = Zips.Serve(data); |
| 31 | + using (client) |
| 32 | + { |
| 33 | + var zip = await RemoteZipArchive.Open(client, "https://example/archive.zip", rangedOptions); |
| 34 | + var contents = await zip.Read(["a.txt", "a.txt"]); |
| 35 | + |
| 36 | + await Assert.That(contents.Count).IsEqualTo(1); |
| 37 | + await Assert.That(Encoding.UTF8.GetString(contents["a.txt"])).IsEqualTo("alpha"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public async Task ReadTextByNames_HonorsByteOrderMark() |
| 43 | + { |
| 44 | + var data = Zips.Padded(4096, ("bom.txt", "hi"), ("plain.txt", "there")); |
| 45 | + var (client, _) = Zips.Serve(data); |
| 46 | + using (client) |
| 47 | + { |
| 48 | + var zip = await RemoteZipArchive.Open(client, "https://example/archive.zip", rangedOptions); |
| 49 | + var texts = await zip.ReadText(["bom.txt", "plain.txt", "missing.txt"]); |
| 50 | + |
| 51 | + await Assert.That(texts.Count).IsEqualTo(2); |
| 52 | + await Assert.That(texts["bom.txt"]).IsEqualTo("hi"); |
| 53 | + await Assert.That(texts["plain.txt"]).IsEqualTo("there"); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public async Task ReadTextByEntries_KeyedByEntry() |
| 59 | + { |
| 60 | + var data = Zips.Padded(4096, ("a.txt", "alpha"), ("b.txt", "beta")); |
| 61 | + var (client, _) = Zips.Serve(data); |
| 62 | + using (client) |
| 63 | + { |
| 64 | + var zip = await RemoteZipArchive.Open(client, "https://example/archive.zip", rangedOptions); |
| 65 | + var a = zip.Find("a.txt")!; |
| 66 | + var b = zip.Find("b.txt")!; |
| 67 | + var texts = await zip.ReadText([a, b]); |
| 68 | + |
| 69 | + await Assert.That(texts[a]).IsEqualTo("alpha"); |
| 70 | + await Assert.That(texts[b]).IsEqualTo("beta"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + [Test] |
| 75 | + public async Task ReadByNames_EmptyInput_NoRequests() |
| 76 | + { |
| 77 | + var data = Zips.Padded(4096, ("a.txt", "alpha")); |
| 78 | + var (client, server) = Zips.Serve(data); |
| 79 | + using (client) |
| 80 | + { |
| 81 | + var zip = await RemoteZipArchive.Open(client, "https://example/archive.zip", rangedOptions); |
| 82 | + var contents = await zip.Read(Array.Empty<string>()); |
| 83 | + |
| 84 | + await Assert.That(contents.Count).IsEqualTo(0); |
| 85 | + await Assert.That(server.Requests).Count().IsEqualTo(1); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments