-
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.
* Add integration tests and fix some bugs that emerged * more integration tests
- Loading branch information
1 parent
af1af0e
commit 6fbca82
Showing
9 changed files
with
168 additions
and
25 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
SimpleCDN.Tests.Integration/CustomWebApplicationFactory.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,30 @@ | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using SimpleCDN.Configuration; | ||
|
||
namespace SimpleCDN.Tests.Integration | ||
{ | ||
public class CustomWebApplicationFactory : WebApplicationFactory<Program> | ||
{ | ||
public const string GENERATED_INDEX_ID = "!GENERATED!INDEX!"; | ||
|
||
public string DataRoot => _rootDirectory.FullName; | ||
private readonly DirectoryInfo _rootDirectory = Directory.CreateTempSubdirectory("SimpleCDN"); | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
builder.ConfigureServices(services => | ||
{ | ||
services.Configure<CDNConfiguration>(config => | ||
{ | ||
config.Footer = GENERATED_INDEX_ID; | ||
config.DataRoot = DataRoot; | ||
}); | ||
}); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
Directory.Delete(DataRoot, true); | ||
} | ||
} | ||
} |
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,64 @@ | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
|
||
namespace SimpleCDN.Tests.Integration | ||
{ | ||
public class EndpointTests : IClassFixture<CustomWebApplicationFactory> | ||
{ | ||
private readonly CustomWebApplicationFactory _webApplicationFactory; | ||
|
||
const string TEXT_FILE_NAME = "test.txt"; | ||
const string TEXT_FILE_CONTENT = "Hello, World!"; | ||
|
||
const string JSON_FILE_NAME = "data/test.json"; | ||
const string JSON_FILE_CONTENT = """{"key": "value"}"""; | ||
|
||
public EndpointTests(CustomWebApplicationFactory webApplicationFactory) | ||
{ | ||
_webApplicationFactory = webApplicationFactory; | ||
|
||
var dataRoot = _webApplicationFactory.DataRoot; | ||
|
||
Directory.CreateDirectory(Path.Combine(dataRoot, "data")); | ||
|
||
// create some files | ||
File.WriteAllText(Path.Combine(_webApplicationFactory.DataRoot, TEXT_FILE_NAME), TEXT_FILE_CONTENT); | ||
File.WriteAllText(Path.Combine(_webApplicationFactory.DataRoot, JSON_FILE_NAME), JSON_FILE_CONTENT); | ||
} | ||
|
||
[Fact] | ||
public async Task Test_Accesible() | ||
{ | ||
var client = _webApplicationFactory.CreateClient(); | ||
|
||
var response = await client.GetAsync("/"); | ||
|
||
Assert.True(response.IsSuccessStatusCode, response.StatusCode.ToString()); | ||
} | ||
|
||
[Fact] | ||
public async Task Test_AutoGeneratedIndex() | ||
{ | ||
var client = _webApplicationFactory.CreateClient(); | ||
var response = await client.GetAsync("/"); | ||
var content = await response.Content.ReadAsStringAsync(); | ||
Assert.Contains(CustomWebApplicationFactory.GENERATED_INDEX_ID, content); | ||
} | ||
|
||
[Theory] | ||
[InlineData("/" + TEXT_FILE_NAME, TEXT_FILE_CONTENT)] | ||
[InlineData("/../" + TEXT_FILE_NAME, TEXT_FILE_CONTENT)] | ||
[InlineData("/data/../" + TEXT_FILE_NAME, TEXT_FILE_CONTENT)] | ||
[InlineData("/" + JSON_FILE_NAME, JSON_FILE_CONTENT)] | ||
[InlineData("/data/../" + JSON_FILE_NAME, JSON_FILE_CONTENT)] | ||
public async Task Test_FileExists_WithContent(string endpoint, string expectedText) | ||
{ | ||
var client = _webApplicationFactory.CreateClient(); | ||
var response = await client.GetAsync(endpoint); | ||
|
||
Assert.True(response.IsSuccessStatusCode, response.StatusCode.ToString()); | ||
|
||
var content = await response.Content.ReadAsStringAsync(); | ||
Assert.Contains(expectedText, content); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
SimpleCDN.Tests.Integration/Properties/launchSettings.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,12 @@ | ||
{ | ||
"profiles": { | ||
"SimpleCDN.Tests.Integration": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:54467;http://localhost:54468" | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
SimpleCDN.Tests.Integration/SimpleCDN.Tests.Integration.csproj
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="xunit" Version="2.9.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SimpleCDN\SimpleCDN.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("SimpleCDN.Tests")] | ||
[assembly: InternalsVisibleTo("SimpleCDN.Tests")] | ||
[assembly: InternalsVisibleTo("SimpleCDN.Tests.Integration")] |
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
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