|
| 1 | +// -------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file = "BinaryRequestIntegrationTests.cs" company = "ANEXIA® Internetdienstleistungs GmbH"> |
| 3 | +// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +// -------------------------------------------------------------------------------------------- |
| 6 | + |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +using Anexia.E5E.Exceptions; |
| 10 | +using Anexia.E5E.Functions; |
| 11 | +using Anexia.E5E.Tests.TestHelpers; |
| 12 | + |
| 13 | +using static Anexia.E5E.Tests.TestData.TestData; |
| 14 | + |
| 15 | +using Xunit; |
| 16 | +using Xunit.Abstractions; |
| 17 | + |
| 18 | +namespace Anexia.E5E.Tests.Integration; |
| 19 | + |
| 20 | +public sealed class BinaryRequestIntegrationTests(ITestOutputHelper outputHelper) : IntegrationTestBase(outputHelper) |
| 21 | +{ |
| 22 | + [Fact] |
| 23 | + public async Task DecodeToBytesThrowsForMultipleFiles() |
| 24 | + { |
| 25 | + await Host.StartWithTestEntrypointAsync(request => |
| 26 | + { |
| 27 | + Assert.Throws<E5EMultipleFilesInFormDataException>(() => request.Event.AsBytes()); |
| 28 | + return null!; |
| 29 | + }); |
| 30 | + await Host.WriteOnceAsync(BinaryRequestWithMultipleFiles); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task MultipleFilesAreProperlyDecoded() |
| 35 | + { |
| 36 | + await Host.StartWithTestEntrypointAsync(request => |
| 37 | + { |
| 38 | + var files = request.Event.AsFiles(); |
| 39 | + Assert.Collection(files, first => |
| 40 | + { |
| 41 | + Assert.NotNull(first); |
| 42 | + Assert.Equal(first, new E5EFileData |
| 43 | + { |
| 44 | + Base64Encoded = "SGVsbG8gd29ybGQh", |
| 45 | + FileSizeInBytes = 12, |
| 46 | + Filename = "my-file-1.name", |
| 47 | + ContentType = "application/my-content-type-1", |
| 48 | + Charset = "utf-8", |
| 49 | + }); |
| 50 | + }, second => |
| 51 | + { |
| 52 | + Assert.NotNull(second); |
| 53 | + Assert.Equal(second, new E5EFileData |
| 54 | + { |
| 55 | + Base64Encoded = "SGVsbG8gd29ybGQh", |
| 56 | + FileSizeInBytes = 12, |
| 57 | + Filename = "my-file-2.name", |
| 58 | + ContentType = "application/my-content-type-2", |
| 59 | + Charset = "utf-8", |
| 60 | + }); |
| 61 | + }); |
| 62 | + return null!; |
| 63 | + }); |
| 64 | + await Host.WriteOnceAsync(BinaryRequestWithMultipleFiles); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public async Task UnknownContentType() |
| 69 | + { |
| 70 | + await Host.StartWithTestEntrypointAsync(request => |
| 71 | + { |
| 72 | + Assert.Equal("SGVsbG8gd29ybGQh"u8.ToArray(), request.Event.AsBytes()); |
| 73 | + return null!; |
| 74 | + }); |
| 75 | + await Host.WriteOnceAsync(BinaryRequestWithUnknownContentType); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task FallbackForByteArrayReturnsValidResponse() |
| 80 | + { |
| 81 | + // act |
| 82 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 83 | + await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From("SGVsbG8gd29ybGQh"u8.ToArray())); |
| 84 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 85 | + var response = await Host.WriteOnceAsync(x => x.WithData("test")); |
| 86 | + |
| 87 | + // assert |
| 88 | + const string expected = |
| 89 | + """ |
| 90 | + {"data":{"binary":"SGVsbG8gd29ybGQh","type":"binary","size":0,"name":"dotnet-e5e-binary-response.blob","content_type":"application/octet-stream","charset":"utf-8"},"type":"binary"} |
| 91 | + """; |
| 92 | + Assert.Contains(expected, response.Stdout); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public async Task FileDataReturnsValidResponse() |
| 97 | + { |
| 98 | + // act |
| 99 | + await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From(new E5EFileData |
| 100 | + { |
| 101 | + Base64Encoded = "SGVsbG8gd29ybGQh", |
| 102 | + Type = "binary", |
| 103 | + FileSizeInBytes = 16, |
| 104 | + Filename = "hello-world.txt", |
| 105 | + ContentType = "text/plain", |
| 106 | + Charset = "utf-8", |
| 107 | + })); |
| 108 | + var response = await Host.WriteOnceAsync(x => x.WithData("test")); |
| 109 | + |
| 110 | + // assert |
| 111 | + const string expected = |
| 112 | + """ |
| 113 | + {"data":{"binary":"SGVsbG8gd29ybGQh","type":"binary","size":16,"name":"hello-world.txt","content_type":"text/plain","charset":"utf-8"},"type":"binary"} |
| 114 | + """; |
| 115 | + Assert.Contains(expected, response.Stdout); |
| 116 | + } |
| 117 | +} |
0 commit comments