-
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
1d955c9
commit 1ff4c15
Showing
15 changed files
with
372 additions
and
25 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
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
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 content = "Hello world!"u8.ToArray(); | ||
var files = request.Event.AsFiles(); | ||
Assert.Collection(files, first => | ||
{ | ||
Assert.NotNull(first); | ||
Assert.Equivalent(first, new E5EFileData(content) | ||
{ | ||
FileSizeInBytes = 12, | ||
Filename = "my-file-1.name", | ||
ContentType = "application/my-content-type-1", | ||
Charset = "utf-8", | ||
}); | ||
}, second => | ||
{ | ||
Assert.NotNull(second); | ||
Assert.Equivalent(second, new E5EFileData(content) | ||
{ | ||
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("Hello world!"u8.ToArray(), request.Event.AsBytes()); | ||
return null!; | ||
}); | ||
await Host.WriteOnceAsync(BinaryRequestWithUnknownContentType); | ||
} | ||
|
||
[Fact] | ||
public async Task FallbackForByteArrayReturnsValidResponse() | ||
{ | ||
// act | ||
await Host.StartWithTestEntrypointAsync(_ => E5EResponse.From("Hello world!"u8.ToArray())); | ||
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("Hello world!"u8.ToArray()) | ||
{ | ||
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
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":"utf-8"}} |
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,11 @@ | ||
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
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
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.") | ||
{ | ||
} | ||
} |
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.