I'm attempting to use Microsoft.AspNetCore.TestHost to test routes via TestServer. I believe the WebHost setup I have should be sufficient; however, whenever I run my test, I receive an error message.
Test method ID_Test.Controllers.Test_CloudPrintController_RouteTests.AppController_ShouldRouteCorrectly threw exception: System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:80) ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.
From my understanding, this is either related to firewall issues or incorrect appsettings; but if my appsettings work for all my other non-TestServer tests, and this uses the same Program.cs / Startup.cs, then I feel as if it should work for this as well.
Here is my test Class and the Mock Services I made.
public class MockAuthenticationService
{
public async Task SignInAsync(IServiceProvider serviceProvider)
{
// Mock implementation of signing in
// Contents removed for brevity
}
}
public class MockAuthenticationSchemeProvider : IAuthenticationSchemeProvider
{
// Contents removed for brevity
}
public class MockAuthenticationHandler : IAuthenticationHandler
{
// Contents removed for brevity
}
public class MockAuthenticationHandlerProvider : IAuthenticationHandlerProvider
{
// Contents removed for brevity
}
public class MockClaimsTransformation : IClaimsTransformation
{
// Contents removed for brevity
}
public class MockOptionsAuthentication : IOptions<AuthenticationOptions>
{
// Contents removed for brevity
}
[TestClass]
public class Test_CloudPrintController_RouteTests : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
// Register mock implementations
services.AddSingleton<IAuthenticationSchemeProvider, MockAuthenticationSchemeProvider>();
services.AddSingleton<IAuthenticationHandlerProvider, MockAuthenticationHandlerProvider>();
services.AddSingleton<IClaimsTransformation, MockClaimsTransformation>();
services.AddSingleton<IOptions<AuthenticationOptions>, MockOptionsAuthentication>();
services.AddSingleton<MockAuthenticationService>();
// Register AuthenticationService with correct dependencies
services.AddSingleton(sp =>
{
var mockAuthService = sp.GetRequiredService<MockAuthenticationService>();
var mockSchemeProvider = sp.GetRequiredService<IAuthenticationSchemeProvider>();
var mockHandlers = sp.GetRequiredService<IAuthenticationHandlerProvider>();
var mockTransform = sp.GetRequiredService<IClaimsTransformation>();
var mockOptions = sp.GetRequiredService<IOptions<AuthenticationOptions>>();
return new AuthenticationService(mockSchemeProvider, mockHandlers, mockTransform, mockOptions);
});
// Add JWT authentication for testing purposes
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false; // In test environment, set to false for simplicity
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false, // Set to false for testing
ValidateAudience = false, // Set to false for testing
ValidateLifetime = false, // Set to false for testing
ValidateIssuerSigningKey = false, // Set to false for testing
// Modify other parameters as needed for testing purposes
};
});
});
}
[TestMethod]
[DoNotParallelize]
public async Task AppController_ShouldRouteCorrectly()
{
// Arrange
HttpClient _client = CreateClient();
var cookieContainer = new CookieContainer();
cookieContainer.Add(_client.BaseAddress, new Cookie("Cookie", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEwMDE5IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjoiaW1leWVyc0B0aGluaXguY29tIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6ImltZXllcnNAdGhpbml4LmNvbSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2dpdmVubmFtZSI6IklzYWFjIE1leWVycyIsImlzbG9ja2Vkb3V0IjoiRmFsc2UiLCJpc2FwcHJvdmVkIjoiVHJ1ZSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6WyIqIiwibWVtYmVyIiwiZGV2ZWxvcGVyIiwic3VwcG9ydCJdLCJuYmYiOjE3MTE0ODg1NDAsImV4cCI6MTcxNDA4MDU0MCwiaXNzIjoiaHR0cDovL2lzdGF0dXMuY29tIiwiYXVkIjoiaHR0cDovL2lzdGF0dXMuY29tIn0.MalKNawXYB0fdfe1644Ut_4rjb1W2QaBw236WVJrHTM"));
var handler = new HttpClientHandler { CookieContainer = cookieContainer };
_client = new HttpClient(handler) { BaseAddress = _client.BaseAddress };
// Simulate authentication by adding mock authentication token/header
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEwMDE5IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjoiaW1leWVyc0B0aGluaXguY29tIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6ImltZXllcnNAdGhpbml4LmNvbSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2dpdmVubmFtZSI6IklzYWFjIE1leWVycyIsImlzbG9ja2Vkb3V0IjoiRmFsc2UiLCJpc2FwcHJvdmVkIjoiVHJ1ZSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6WyIqIiwibWVtYmVyIiwiZGV2ZWxvcGVyIiwic3VwcG9ydCJdLCJuYmYiOjE3MTE0ODg1NDAsImV4cCI6MTcxNDA4MDU0MCwiaXNzIjoiaHR0cDovL2lzdGF0dXMuY29tIiwiYXVkIjoiaHR0cDovL2lzdGF0dXMuY29tIn0.MalKNawXYB0fdfe1644Ut_4rjb1W2QaBw236WVJrHTM");
// Act
var devResponse = await _client.GetAsync("/developer"); // 100 is the test printer node
var response = await _client.GetAsync("/api/subscription-bundles"); // 100 is the test printer node
// Assert
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // This 404's, presumably cannot reach this endpoint through the test code
}
}
Thanks in advance for your help!