Skip to content

Commit

Permalink
fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Jun 5, 2024
1 parent 8227a91 commit 576bcb6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 85 deletions.
45 changes: 26 additions & 19 deletions test/FluentRest.Tests/FluentEchoTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyInjection;

using Xunit;
using Xunit.Abstractions;

namespace FluentRest.Tests;

public class FluentEchoTests
public class FluentEchoTests : HostTestBase
{
public FluentEchoTests(ITestOutputHelper output, HostFixture fixture)
: base(output, fixture)
{
}

[Fact]
public async Task EchoGet()
{
Expand All @@ -29,7 +35,7 @@ public async Task EchoGet()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/get?page=1&size=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/get?page=1&size=10", result.Url);
Assert.Equal("1", result.QueryString["page"]);
Assert.Equal("10", result.QueryString["size"]);
}
Expand All @@ -47,7 +53,7 @@ public async Task EchoGetBearer()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/get?page=1&size=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/get?page=1&size=10", result.Url);
Assert.Equal("1", result.QueryString["page"]);
Assert.Equal("10", result.QueryString["size"]);

Expand Down Expand Up @@ -104,7 +110,7 @@ public async Task EchoGetAcceptMultiple()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/get?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/get?page=10", result.Url);
Assert.Equal("text/xml, application/bson, application/json", result.Headers[HttpRequestHeaders.Accept]);
Assert.Equal("testing header", result.Headers["X-Blah"]);
}
Expand All @@ -122,7 +128,7 @@ public async Task EchoPost()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/post?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post?page=10", result.Url);
Assert.Equal("Value", result.Form["Test"]);
Assert.Equal("value", result.Form["key"]);
}
Expand All @@ -145,7 +151,7 @@ public async Task EchoPostResponse()
var result = await response.DeserializeAsync<EchoResult>();

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/post?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post?page=10", result.Url);
Assert.Equal("Value", result.Form["Test"]);
Assert.Equal("value", result.Form["key"]);
}
Expand All @@ -163,7 +169,7 @@ public async Task EchoPatch()

Assert.NotNull(response);

Assert.Equal("https://httpbin.org/patch?page=10", response.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/patch?page=10", response.Url);
Assert.Equal("Value", response.Form["Test"]);
}

Expand All @@ -184,7 +190,7 @@ public async Task EchoPatchResponse()
var result = await response.DeserializeAsync<EchoResult>();

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/patch?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/patch?page=10", result.Url);
Assert.Equal("Value", result.Form["Test"]);
}

Expand All @@ -201,7 +207,7 @@ public async Task EchoPut()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/put?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/put?page=10", result.Url);
Assert.Equal("Value", result.Form["Test"]);
Assert.Equal("value", result.Form["key"]);
}
Expand Down Expand Up @@ -238,7 +244,7 @@ public async Task EchoPostData()
Assert.True(result.Headers.ContainsKey("Content-Length"));
int contentLength = Int32.Parse(result.Headers["Content-Length"]);
Assert.True(contentLength > 0);
Assert.Equal("https://httpbin.org/post?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post?page=10", result.Url);
Assert.Equal("application/json; charset=utf-8", result.Headers[HttpRequestHeaders.ContentType]);
Assert.True(result.Headers.ContainsKey("Content-Type"));
var contentType = result.Headers["Content-Type"];
Expand Down Expand Up @@ -359,7 +365,7 @@ public async Task EchoPostDataCustomCompressedContent()
Assert.True(result.Headers.ContainsKey("Content-Length"));
int contentLength = Int32.Parse(result.Headers["Content-Length"]);
Assert.True(contentLength > 0);
Assert.Equal("https://httpbin.org/post?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post?page=10", result.Url);
Assert.Equal("application/json; charset=utf-8", result.Headers[HttpRequestHeaders.ContentType]);
Assert.Equal("gzip", result.Headers[HttpRequestHeaders.ContentEncoding]);
}
Expand Down Expand Up @@ -416,7 +422,7 @@ public async Task DefaultPost()
);

Assert.NotNull(result);
Assert.Equal("https://httpbin.org/post?page=10", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post?page=10", result.Url);
Assert.Equal("Value", result.Form["Test"]);
Assert.Equal("value", result.Form["key"]);
Assert.Equal("Token abc-def-123", result.Headers["Authorization"]);
Expand All @@ -434,15 +440,16 @@ public async Task SendRequest()

Assert.NotNull(result);
Assert.True(result.Headers.ContainsKey("Content-Length"));
Assert.Equal("https://httpbin.org/post", result.Url);
Assert.Equal($"{Fixture.HttpBinUrl}/post", result.Url);
}

private static IFluentClient CreateClient()
private IFluentClient CreateClient()
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://httpbin.org/", UriKind.Absolute);
var httpClientFactory = Services.GetService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("HttpBin");

var fluentClient = new FluentClient(httpClient);
var serializer = Services.GetService<IContentSerializer>();
var fluentClient = new FluentClient(httpClient, serializer);

return fluentClient;
}
Expand Down
2 changes: 2 additions & 0 deletions test/FluentRest.Tests/FluentRest.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Testcontainers" Version="3.8.0" />
<PackageReference Include="XUnit.Hosting" Version="1.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
29 changes: 6 additions & 23 deletions test/FluentRest.Tests/GitHub/GitHubFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

using FluentRest.Tests.GitHub.Models;

using Microsoft.Extensions.DependencyInjection;

using Xunit;
using Xunit.Abstractions;

namespace FluentRest.Tests.GitHub;

public class GitHubFactoryTests
public class GitHubFactoryTests : HostTestBase
{
public IServiceProvider ServiceProvider { get; }

public GitHubFactoryTests()
public GitHubFactoryTests(ITestOutputHelper output, HostFixture fixture) : base(output, fixture)
{
var services = new ServiceCollection();

services.AddSingleton<IContentSerializer>(sp => new JsonContentSerializer(new JsonSerializerOptions(JsonSerializerDefaults.Web)));

services.AddHttpClient<GithubClient>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");

c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
c.DefaultRequestHeaders.Add("User-Agent", "GitHubClient");
})
.AddHttpMessageHandler(() => new RetryHandler());

ServiceProvider = services.BuildServiceProvider();
}

[Fact]
public async Task GetRepo()
{
var client = ServiceProvider.GetService<GithubClient>();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand All @@ -50,7 +33,7 @@ public async Task GetRepo()
[Fact]
public async Task GetRepoIssues()
{
var client = ServiceProvider.GetService<GithubClient>();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<List<Issue>>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand All @@ -64,7 +47,7 @@ public async Task GetRepoIssues()
[Fact]
public async Task GetFirstIssue()
{
var client = ServiceProvider.GetService<GithubClient>();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<Issue>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand Down
29 changes: 11 additions & 18 deletions test/FluentRest.Tests/GitHub/GitHubTests.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

using FluentRest.Tests.GitHub.Models;

using Microsoft.Extensions.DependencyInjection;

using Xunit;
using Xunit.Abstractions;

namespace FluentRest.Tests.GitHub;

public class GitHubTests
public class GitHubTests : HostTestBase
{
public GitHubTests(ITestOutputHelper output, HostFixture fixture) : base(output, fixture)
{
}

[Fact]
public async Task GetRepo()
{
var client = CreateClient();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<Repository>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand All @@ -28,7 +33,7 @@ public async Task GetRepo()
[Fact]
public async Task GetRepoIssues()
{
var client = CreateClient();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<List<Issue>>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand All @@ -42,7 +47,7 @@ public async Task GetRepoIssues()
[Fact]
public async Task GetFirstIssue()
{
var client = CreateClient();
var client = Services.GetService<GithubClient>();
var result = await client.GetAsync<Issue>(b => b
.AppendPath("repos")
.AppendPath("loresoft")
Expand All @@ -53,16 +58,4 @@ public async Task GetFirstIssue()

Assert.NotNull(result);
}

private static FluentClient CreateClient()
{
var contentSerializer = new JsonContentSerializer();

var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://api.github.com/", UriKind.Absolute);

var fluentClient = new FluentClient(httpClient, contentSerializer);
return fluentClient;
}

}
1 change: 0 additions & 1 deletion test/FluentRest.Tests/GitHub/GithubClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Net.Http;

namespace FluentRest.Tests.GitHub;
Expand Down
2 changes: 1 addition & 1 deletion test/FluentRest.Tests/Google/Maps/GoogleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

using Xunit;

namespace FluentRest.Tests;
namespace FluentRest.Tests.Google.Maps;

public class GoogleMapsTests
{
Expand Down
9 changes: 9 additions & 0 deletions test/FluentRest.Tests/HostCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Xunit;

namespace FluentRest.Tests;

[CollectionDefinition(CollectionName)]
public class HostCollection : ICollectionFixture<HostFixture>
{
public const string CollectionName = nameof(HostCollection);
}
65 changes: 65 additions & 0 deletions test/FluentRest.Tests/HostFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Threading.Tasks;

using DotNet.Testcontainers.Builders;

using FluentRest.Tests.GitHub;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

using Xunit;

using XUnit.Hosting;

using IContainer = DotNet.Testcontainers.Containers.IContainer;

namespace FluentRest.Tests;

public class HostFixture : TestApplicationFixture, IAsyncLifetime
{
private readonly IContainer _container = new ContainerBuilder()
.WithImage("kennethreitz/httpbin:latest")
.WithPortBinding(80, true)
.Build();

public async Task InitializeAsync()
{
await _container.StartAsync();

// get container url
HttpBinUrl = $"http://{_container.Hostname}:{_container.GetMappedPublicPort(80)}";
}

public async Task DisposeAsync()
{
await _container.DisposeAsync();
}

public string HttpBinUrl { get; private set; } = "https://httpbin.org/";

protected override void ConfigureApplication(HostApplicationBuilder builder)
{
base.ConfigureApplication(builder);

builder.Services
.TryAddSingleton<IContentSerializer, JsonContentSerializer>();

builder.Services
.AddHttpClient<GithubClient>(c =>
{
c.BaseAddress = new Uri("https://api.github.com/");

c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
c.DefaultRequestHeaders.Add("User-Agent", "GitHubClient");
})
.AddHttpMessageHandler(() => new RetryHandler());

builder.Services
.AddHttpClient("GoogleMaps", client => client.BaseAddress = new Uri("https://maps.googleapis.com/maps/api/", UriKind.Absolute));

builder.Services
.AddHttpClient("HttpBin", client => client.BaseAddress = new Uri(HttpBinUrl, UriKind.Absolute));
}
}
15 changes: 15 additions & 0 deletions test/FluentRest.Tests/HostTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;
using Xunit.Abstractions;

using XUnit.Hosting;

namespace FluentRest.Tests;

[Collection(HostCollection.CollectionName)]
public abstract class HostTestBase : TestHostBase<HostFixture>
{
protected HostTestBase(ITestOutputHelper output, HostFixture fixture)
: base(output, fixture)
{
}
}
Loading

0 comments on commit 576bcb6

Please sign in to comment.