-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestServerFixture.cs
95 lines (80 loc) · 4.02 KB
/
TestServerFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MovieProject.IsolatedTests.ComponentTesting.Mock;
using MovieProject.Logic.Proxy;
using MovieProject.Web;
using System;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using SystemTestingTools;
using Xunit;
namespace IsolatedTests.ComponentTestings
{
public class TestServerFixture : IDisposable
{
public TestServer Server { get; private set; }
public string StubsFolder { get; private set; }
public static string MovieUrl = "http://www.omdbapi.com/?apikey=863d6589&type=movie";
public TestServerFixture()
{
StartServer();
SanityCheckServer();
}
private void StartServer()
{
StubsFolder = EnvironmentHelper.GetProjectFolder("ComponentTesting/Stubs");
var builder = Program.CreateWebHostBuilder(new string[0]) // use the exact same builder as the website, to test the wiring
.ConfigureAppConfiguration((hostingContext, config) =>
{
// make small changes to configuration, such as disabling caching
config.AddJsonFile("appsettings.tests.json", optional: false, reloadOnChange: true);
})
.InterceptHttpCallsBeforeSending()
.IntercepLogs(minimumLevelToIntercept: LogLevel.Information,
namespaceToIncludeStart: new[] { "MovieProject" },
namespaceToExcludeStart: new[] { "Microsoft" }) // redundand exclusion, just here to show the possible configuration
.UseEnvironment("Development")
.ConfigureTestServices(c => c.AddSingleton<IThriftServiceForCarManagement, MockThriftServiceForCarManagement>()); // replace a proxy that talks to a non http dependency
SetupGlobalStubs();
Server = new TestServer(builder);
}
private void SetupGlobalStubs()
{
// this will be the default response, returned if it can't find a match for the session
var response = ResponseFactory.FromFiddlerLikeResponseFile($"{StubsFolder}/OmdbApi/Fake_Responses/Happy/200_NoRunTime_NoPlot_YearTooOld.txt");
var comeAlongMovieUrl = $"{MovieUrl}&t=fantastika";
UnsessionedData.AppendGlobalHttpCallStub(HttpMethod.Get, new System.Uri(comeAlongMovieUrl), response);
}
/// <summary>
/// Run a quick sanity check, before running any tests
/// </summary>
/// <returns></returns>
private void SanityCheckServer()
{
HttpResponseMessage response = null;
using (var client = Server.CreateClient())
response = client.GetAsync("/healthcheck").Result; // we run the async method synchronously because it's called from a contructor, that can't be async
if (response.StatusCode != System.Net.HttpStatusCode.OK)
throw new ApplicationException("TestServer doesn't respond to basic request to /healthcheck");
if(UnsessionedData.UnsessionedLogs.Count != 1)
throw new ApplicationException($"Expected to find 1 log during startup, found {UnsessionedData.UnsessionedLogs.Count}");
var firstMessage = UnsessionedData.UnsessionedLogs.First()?.ToString();
if (firstMessage != "Information: Application is starting")
throw new ApplicationException($"First log was not the expected one: {firstMessage}");
}
public void Dispose()
{
Server.Dispose();
}
}
[CollectionDefinition("SharedServer collection")]
public class SharedServerCollection : ICollectionFixture<TestServerFixture>
{
// as suggested in https://xunit.github.io/docs/shared-context
// done so this collection is shared between many tests
}
}