Skip to content

Commit 17e2408

Browse files
committed
Test client and API backend implementation
1 parent b0e2544 commit 17e2408

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/Simplify.Web.Multipart.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Simplify.Web.Multipart", "S
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestServer", "TestServer\TestServer.csproj", "{4EF29FB7-2D85-4473-840B-02515F5E7CE9}"
99
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestClient", "TestClient\TestClient.csproj", "{BDC0BD04-F8FD-49C1-8D07-9325D79F5CAF}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{4EF29FB7-2D85-4473-840B-02515F5E7CE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{4EF29FB7-2D85-4473-840B-02515F5E7CE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{4EF29FB7-2D85-4473-840B-02515F5E7CE9}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{BDC0BD04-F8FD-49C1-8D07-9325D79F5CAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{BDC0BD04-F8FD-49C1-8D07-9325D79F5CAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{BDC0BD04-F8FD-49C1-8D07-9325D79F5CAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{BDC0BD04-F8FD-49C1-8D07-9325D79F5CAF}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

src/TestClient/Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using RestSharp;
3+
using Simplify.Extensions;
4+
5+
namespace TestClient
6+
{
7+
internal class Program
8+
{
9+
private static void Main()
10+
{
11+
var client = new RestClient("http://localhost:5000/");
12+
13+
var request = new RestRequest("api/v1/testIn", Method.POST)
14+
{
15+
AlwaysMultipartFormData = true,
16+
Files = { FileParameter.Create("test file", "Hello world!!!".ToBytesArray(), "MyFile.txt") }
17+
};
18+
19+
var result = client.Execute(request);
20+
21+
if (result.IsSuccessful != true)
22+
throw new InvalidOperationException("Error sending file");
23+
24+
Console.WriteLine("HTTP status: " + result.StatusCode);
25+
Console.ReadLine();
26+
}
27+
}
28+
}

src/TestClient/TestClient.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<Version>0.1</Version>
5+
<OutputType>Exe</OutputType>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="RestSharp" Version="106.10.1" />
9+
<PackageReference Include="Simplify.Extensions" Version="1.1.1" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<None Update="TestData.json">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</None>
15+
</ItemGroup>
16+
</Project>

src/TestServer/Controllers/Api/v1/TestInController.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,24 @@ public class TestInController : AsyncController<MultipartViewModel>
1414
{
1515
public override async Task<ControllerResponse> Invoke()
1616
{
17-
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentNullException("No files in model");
17+
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentException("No files in model");
18+
using var stream = new StreamReader(file.Data);
19+
var fileData = await stream.ReadToEndAsync();
1820

1921
Trace.WriteLine($"Files count: {Model.Files}");
2022
Trace.WriteLine($"File name: {file.FileName}");
23+
Trace.WriteLine($"File content: {fileData}");
2124

22-
using var stream = new StreamReader(file.Data);
25+
// Assert
2326

24-
var fileData = await stream.ReadToEndAsync();
27+
if (file.FileName != "test file")
28+
throw new InvalidDataException($"Wrong name, actual: '{file.Name}'");
2529

26-
Trace.WriteLine($"File content: {fileData}");
30+
if (file.FileName != "MyFile.txt")
31+
throw new InvalidDataException($"Wrong file name, actual: '{file.FileName}'");
32+
33+
if (fileData != "Hello World!!!")
34+
throw new InvalidDataException($"Wrong file data, actual: '{fileData}'");
2735

2836
return NoContent();
2937
}

0 commit comments

Comments
 (0)