-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SIANXSVC-1193: dotnet-e5e does not handle binary requests as expected
Closes SIANXSVC-1193
- Loading branch information
1 parent
7bd1753
commit e6a1c32
Showing
15 changed files
with
398 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/Anexia.E5E.Tests/Integration/BinaryRequestIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// -------------------------------------------------------------------------------------------- | ||
// <copyright file = "BinaryRequestIntegrationTests.cs" company = "ANEXIA® Internetdienstleistungs GmbH"> | ||
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading.Tasks; | ||
|
||
using Anexia.E5E.Exceptions; | ||
using Anexia.E5E.Functions; | ||
using Anexia.E5E.Tests.TestHelpers; | ||
|
||
using static Anexia.E5E.Tests.TestData.TestData; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Anexia.E5E.Tests.Integration; | ||
|
||
public sealed class BinaryRequestIntegrationTests(ITestOutputHelper outputHelper) : IntegrationTestBase(outputHelper) | ||
{ | ||
[Fact] | ||
public async Task DecodeToBytesThrowsForMultipleFiles() | ||
{ | ||
await Host.StartWithTestEntrypointAsync(request => | ||
{ | ||
Assert.Throws<E5EMultipleFilesInFormDataException>(() => request.Event.AsBytes()); | ||
return null!; | ||
}); | ||
await Host.WriteOnceAsync(BinaryRequestWithMultipleFiles); | ||
} | ||
|
||
[Fact] | ||
public async Task MultipleFilesAreProperlyDecoded() | ||
{ | ||
await Host.StartWithTestEntrypointAsync(request => | ||
{ | ||
var files = request.Event.AsFiles(); | ||
Assert.Collection(files, first => | ||
{ | ||
Assert.NotNull(first); | ||
Assert.Equal(first, new E5EFileData | ||
{ | ||
Base64Encoded = "SGVsbG8gd29ybGQh", | ||
FileSizeInBytes = 12, | ||
Filename = "my-file-1.name", | ||
ContentType = "application/my-content-type-1", | ||
Charset = "utf-8", | ||
}); | ||
}, second => | ||
{ | ||
Assert.NotNull(second); | ||
Assert.Equal(second, new E5EFileData | ||
{ | ||
Base64Encoded = "SGVsbG8gd29ybGQh", | ||
FileSizeInBytes = 12, | ||
Filename = "my-file-2.name", | ||
ContentType = "application/my-content-type-2", | ||
Charset = "utf-8", | ||
}); | ||
}); | ||
return null!; | ||
}); | ||
await Host.WriteOnceAsync(BinaryRequestWithMultipleFiles); | ||
} | ||
|
||
[Fact] | ||
public async Task UnknownContentType() | ||
{ | ||
await Host.StartWithTestEntrypointAsync(request => | ||
{ | ||
Assert.Equal("SGVsbG8gd29ybGQh"u8.ToArray(), request.Event.AsBytes()); | ||
return null!; | ||
}); | ||
await Host.WriteOnceAsync(BinaryRequestWithUnknownContentType); | ||
} | ||
|
||
[Fact] | ||
public async Task FallbackForByteArrayReturnsValidResponse() | ||
{ | ||
// act | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From("SGVsbG8gd29ybGQh"u8.ToArray())); | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
var response = await Host.WriteOnceAsync(x => x.WithData("test")); | ||
|
||
// assert | ||
const string expected = | ||
""" | ||
{"data":{"binary":"SGVsbG8gd29ybGQh","type":"binary","size":0,"name":"dotnet-e5e-binary-response.blob","content_type":"application/octet-stream","charset":"utf-8"},"type":"binary"} | ||
"""; | ||
Assert.Contains(expected, response.Stdout); | ||
} | ||
|
||
[Fact] | ||
public async Task FileDataReturnsValidResponse() | ||
{ | ||
// act | ||
await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From(new E5EFileData | ||
{ | ||
Base64Encoded = "SGVsbG8gd29ybGQh", | ||
Type = "binary", | ||
FileSizeInBytes = 16, | ||
Filename = "hello-world.txt", | ||
ContentType = "text/plain", | ||
Charset = "utf-8", | ||
})); | ||
var response = await Host.WriteOnceAsync(x => x.WithData("test")); | ||
|
||
// assert | ||
const string expected = | ||
""" | ||
{"data":{"binary":"SGVsbG8gd29ybGQh","type":"binary","size":16,"name":"hello-world.txt","content_type":"text/plain","charset":"utf-8"},"type":"binary"} | ||
"""; | ||
Assert.Contains(expected, response.Stdout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tionTests.RequestSerializationMatchesSnapshot_testName=simple binary request.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"type":"binary","data":"dGVzdA=="} | ||
{"type":"binary","data":{"binary":"aGVsbG8=","type":"binary","size":0,"name":null,"content_type":null,"charset":null}} |
2 changes: 1 addition & 1 deletion
2
...onTests.ResponseSerializationMatchesSnapshot_testName=simple binary response.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"data":"dGVzdA==","type":"binary"} | ||
{"data":{"binary":"aGVsbG8=","type":"binary","size":0,"name":"dotnet-e5e-binary-response.blob","content_type":"application/octet-stream","charset":"utf-8"},"type":"binary"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// -------------------------------------------------------------------------------------------- | ||
// <copyright file = "TestData.cs" company = "ANEXIA® Internetdienstleistungs GmbH"> | ||
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
using System.IO; | ||
|
||
namespace Anexia.E5E.Tests.TestData; | ||
|
||
internal static class TestData | ||
{ | ||
private static string ReadTestDataFile(string path) => File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "TestData", path)); | ||
|
||
internal static string BinaryRequestWithMultipleFiles => ReadTestDataFile("binary_request_with_multiple_files.json"); | ||
internal static string BinaryRequestWithUnknownContentType => ReadTestDataFile("binary_request_unknown_content_type.json"); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Anexia.E5E.Tests/TestData/binary_request_unknown_content_type.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"context": { | ||
"type": "integration-test", | ||
"async": true, | ||
"date": "2024-01-01T00:00:00Z" | ||
}, | ||
"event": { | ||
"type": "binary", | ||
"data": { | ||
"binary": "SGVsbG8gd29ybGQh", | ||
"type": "binary", | ||
"name": "my-file-1.name", | ||
"size": 12, | ||
"content_type": "application/my-content-type-1", | ||
"charset": "utf-8" | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Anexia.E5E.Tests/TestData/binary_request_with_multiple_files.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"context": { | ||
"type": "integration-test", | ||
"async": true, | ||
"date": "2024-01-01T00:00:00Z" | ||
}, | ||
"event": { | ||
"type": "binary", | ||
"data": [ | ||
{ | ||
"binary": "SGVsbG8gd29ybGQh", | ||
"type": "binary", | ||
"name": "my-file-1.name", | ||
"size": 12, | ||
"content_type": "application/my-content-type-1", | ||
"charset": "utf-8" | ||
}, | ||
{ | ||
"binary": "SGVsbG8gd29ybGQh", | ||
"type": "binary", | ||
"name": "my-file-2.name", | ||
"size": 12, | ||
"content_type": "application/my-content-type-2", | ||
"charset": "utf-8" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Anexia.E5E/Exceptions/E5EMultipleFilesInFormDataException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// -------------------------------------------------------------------------------------------- | ||
// <copyright file = "E5EMultipleFilesInFormDataException.cs" company = "ANEXIA® Internetdienstleistungs GmbH"> | ||
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
using Anexia.E5E.Functions; | ||
|
||
namespace Anexia.E5E.Exceptions; | ||
|
||
/// <summary> | ||
/// Exception that is thrown if the <see cref="E5EEvent.AsBytes"/> method is called, but there is more than one | ||
/// file attached to the request. | ||
/// </summary> | ||
public sealed class E5EMultipleFilesInFormDataException : E5EException | ||
{ | ||
internal E5EMultipleFilesInFormDataException() : base( | ||
$"There were multiple files attached to this request, so there's no unique binary data. Please use the {nameof(E5EEvent)}.{nameof(E5EEvent.AsFiles)} method instead.") | ||
{ | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Anexia.E5E/Exceptions/E5EObsoleteCodeInUseException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// -------------------------------------------------------------------------------------------- | ||
// <copyright file = "E5EObsoleteCodeInUseException.cs" company = "ANEXIA® Internetdienstleistungs GmbH"> | ||
// Copyright (c) ANEXIA® Internetdienstleistungs GmbH. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------- | ||
|
||
namespace Anexia.E5E.Exceptions; | ||
|
||
/// <summary> | ||
/// Exception that is thrown if obsolete code is in usage. | ||
/// Usually this shouldn't not happen, as the APIs are backwards compatible. If it gets thrown nonetheless, this indicates a bug in the library. | ||
/// </summary> | ||
public sealed class E5EObsoleteCodeInUseException : E5EException | ||
{ | ||
internal E5EObsoleteCodeInUseException(string message) : base(message) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.