Skip to content

Added OoklaSpeedtest unit tests #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

namespace NetPace.Tests;
namespace NetPace.Console.Tests;

public static class VerifyConfiguration
{
Expand Down
4 changes: 2 additions & 2 deletions src/NetPace.Console/NetPace.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -24,7 +24,7 @@
<None Update="slant.flf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<InternalsVisibleTo Include="NetPace.Tests" />
<InternalsVisibleTo Include="NetPace.Console.Tests" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/NetPace.Core.Tests/NetPace.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.0-preview-25107-01" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="Verify" Version="30.1.0-beta.2" />
<PackageReference Include="Verify.Xunit" Version="30.1.0-beta.2" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetPace.Core\NetPace.Core.csproj" />
</ItemGroup>

</Project>
76 changes: 76 additions & 0 deletions src/NetPace.Core.Tests/OoklaSpeedtestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using NetPace.Core.Clients.Ookla;
using RichardSzalay.MockHttp;

namespace NetPace.Core.Tests;

public class OoklaSpeedtestTests
{
[Fact]
public async Task GetServersAsync_ShouldReturnSingleServer_WhenResponseHasOneServer()
{
// Given
var fakeXml = """
<settings>
<servers>
<server id="1" url="http://testurl.com" lat="0" lon="0" name="TestLocation" country="TestCountry" sponsor="TestSponsor"/>
</servers>
</settings>
""";

var mockHttp = new MockHttpMessageHandler();
mockHttp.When("http://*")
.Respond("application/xml", fakeXml);

var httpClient = mockHttp.ToHttpClient();
var speedtest = new OoklaSpeedtest(httpClientOverride: httpClient);

// When
var servers = await speedtest.GetServersAsync();

// Then
Assert.Single(servers);
Assert.Equal("TestLocation", servers[0].Location);
Assert.Equal("TestSponsor", servers[0].Sponsor);
Assert.Equal("http://testurl.com", servers[0].Url);
}

[Fact]
public async Task GetServersAsync_ShouldReturnMultipleServers_WhenResponseHasMultipleServers()
{
// Given
var fakeXml = """
<settings>
<servers>
<server id="1" url="http://testurl1.com" lat="0" lon="0" name="Location1" country="Country1" sponsor="Sponsor1"/>
<server id="2" url="http://testurl2.com" lat="1" lon="1" name="Location2" country="Country2" sponsor="Sponsor2"/>
</servers>
</settings>
""";

var mockHttp = new MockHttpMessageHandler();
mockHttp.When("http://*")
.Respond("application/xml", fakeXml);

var httpClient = mockHttp.ToHttpClient();
var speedtest = new OoklaSpeedtest(httpClientOverride: httpClient);

// When
var servers = await speedtest.GetServersAsync();

// Then
Assert.Equal(2, servers.Length);
Assert.Collection(servers,
s =>
{
Assert.Equal("Location1", s.Location);
Assert.Equal("Sponsor1", s.Sponsor);
Assert.Equal("http://testurl1.com", s.Url);
},
s =>
{
Assert.Equal("Location2", s.Location);
Assert.Equal("Sponsor2", s.Sponsor);
Assert.Equal("http://testurl2.com", s.Url);
});
}
}
3 changes: 3 additions & 0 deletions src/NetPace.Core.Tests/Properties/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using NetPace.Core;
global using NetPace.Core.Clients.Testing;
global using Xunit;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NetPace.Tests.Unit;
namespace NetPace.Core.Tests;

public class SpeedTestResultTests
{
Expand Down
10 changes: 8 additions & 2 deletions src/NetPace.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
Expand All @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetPace.Console", "NetPace.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetPace.Core", "NetPace.Core\NetPace.Core.csproj", "{30331A29-2F59-4E1C-A239-7D82A5A865E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetPace.Tests", "NetPace.Tests\NetPace.Tests.csproj", "{D19D41F3-C266-4A09-97C8-02363A5B1BAA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetPace.Console.Tests", "NetPace.Console.Tests\NetPace.Console.Tests.csproj", "{D19D41F3-C266-4A09-97C8-02363A5B1BAA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8EC462FD-D22E-90A8-E5CE-7E832BA40C5D}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{5B
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "..\examples\ConsoleApp\ConsoleApp.csproj", "{3C069F6A-0D91-4B46-B710-45A2F2883759}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetPace.Core.Tests", "NetPace.Core.Tests\NetPace.Core.Tests.csproj", "{ED348238-47F5-41B5-B0F5-F256D3750FD1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{3C069F6A-0D91-4B46-B710-45A2F2883759}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C069F6A-0D91-4B46-B710-45A2F2883759}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C069F6A-0D91-4B46-B710-45A2F2883759}.Release|Any CPU.Build.0 = Release|Any CPU
{ED348238-47F5-41B5-B0F5-F256D3750FD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED348238-47F5-41B5-B0F5-F256D3750FD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED348238-47F5-41B5-B0F5-F256D3750FD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED348238-47F5-41B5-B0F5-F256D3750FD1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down