xunit web server collection
This commit is contained in:
parent
282b0277ae
commit
3695c17429
38 changed files with 1033 additions and 140 deletions
|
|
@ -16,10 +16,9 @@ namespace isn.tests
|
|||
{
|
||||
public class Tests
|
||||
{
|
||||
private DataRow dataRow;
|
||||
|
||||
[Fact]
|
||||
public void HAveADefaultDataProtector()
|
||||
public void HaveADefaultDataProtector()
|
||||
{
|
||||
string pass = "a lame and big pass";
|
||||
isn.IDataProtector _protector = new isn.DefaultDataProtector();
|
||||
|
|
@ -31,24 +30,10 @@ namespace isn.tests
|
|||
Assert.True(protectedpass.Length > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test()
|
||||
{
|
||||
System.Data.DataTable dataTable = new System.Data.DataTable();
|
||||
dataTable.Columns.Add(new DataColumn("x"));
|
||||
dataTable.Columns.Add(new DataColumn("y"));
|
||||
dataRow = dataTable.NewRow();
|
||||
dataRow[0] = 1;
|
||||
dataRow[1] = 2;
|
||||
|
||||
dataTable.Rows.Add(dataRow);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TestHttpClient()
|
||||
{
|
||||
string url = "http://isn.pschneider.fr/" + ApiConfig.IndexDotJson;
|
||||
string url = "https://isn.pschneider.fr/" + ApiConfig.IndexDotJson;
|
||||
HttpClient client = new HttpClient();
|
||||
// var json = await client.GetStringAsync(new System.Uri(url));
|
||||
var response = await client.GetAsync(url);
|
||||
|
|
@ -67,9 +52,9 @@ namespace isn.tests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetServerResourcesUsingHttpClientAsyncTest()
|
||||
public void GetServerResourcesUsingHttpClientAsyncTest()
|
||||
{
|
||||
var model = SourceHelpers.GetServerResources("Http://isn.pschneider.fr/index.json");
|
||||
var model = SourceHelpers.GetServerResources("Https://isn.pschneider.fr/index.json");
|
||||
Console.WriteLine(JsonConvert.SerializeObject(model));
|
||||
Assert.NotNull(model.Resources);
|
||||
var pub = model.Resources.FirstOrDefault((r) => r.Type.StartsWith("PackagePublish/"));
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
using Xunit;
|
||||
|
||||
namespace isn.tests
|
||||
{
|
||||
public class xunitTest1
|
||||
{
|
||||
[Fact]
|
||||
void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,50 +5,56 @@ using Xunit;
|
|||
using isnd.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using isnd.tests;
|
||||
|
||||
namespace isnd.host.tests
|
||||
{
|
||||
public class UnitTestWebHost
|
||||
[Collection("Web server collection")]
|
||||
public class UnitTestWebHost : IClassFixture<WebServerFixture>
|
||||
{
|
||||
WebServerFixture server;
|
||||
public UnitTestWebHost(WebServerFixture server)
|
||||
{
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestHaveTestDbContextAndMigrate()
|
||||
{
|
||||
string envVar = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
||||
|
||||
IWebHost webhost = BuildWebHost(new string[0]);
|
||||
|
||||
using (var serviceScope = webhost.Services.CreateScope())
|
||||
using (var serviceScope = server.Host.Services.CreateScope())
|
||||
{
|
||||
var services = serviceScope.ServiceProvider;
|
||||
|
||||
try
|
||||
{
|
||||
var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
|
||||
var myDependency = services.GetRequiredService<ApplicationDbContext>();
|
||||
myDependency.Database.Migrate();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Failed " + envVar, ex);
|
||||
}
|
||||
var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
|
||||
var myDependency = services.GetRequiredService<ApplicationDbContext>();
|
||||
myDependency.Database.Migrate();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
void TestDropUser()
|
||||
{
|
||||
|
||||
using (var serviceScope = server.Host.Services.CreateScope())
|
||||
{
|
||||
var services = serviceScope.ServiceProvider;
|
||||
|
||||
var isnSettings = services.GetRequiredService<IOptions<isn.Settings>>().Value;
|
||||
var dbContext = services.GetRequiredService<ApplicationDbContext>();
|
||||
var paul = dbContext.Users.FirstOrDefaultAsync(u => u.UserName == "paul").Result;
|
||||
if (paul!=null)
|
||||
dbContext.Users.Remove(paul);
|
||||
}
|
||||
}
|
||||
|
||||
public static IWebHost BuildWebHost(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>().Build();
|
||||
|
||||
[Fact]
|
||||
|
||||
public async Task NugetInstallsTest()
|
||||
public void NugetInstallsTest()
|
||||
{
|
||||
ProcessStartInfo psi = new ProcessStartInfo("nuget");
|
||||
psi.ArgumentList.Add("install");
|
||||
|
|
@ -60,16 +66,5 @@ namespace isnd.host.tests
|
|||
p.WaitForExit();
|
||||
Assert.True(p.ExitCode == 0, "nuget install failed!");
|
||||
}
|
||||
|
||||
void PrintAddresses(IServiceProvider services)
|
||||
{
|
||||
Console.WriteLine("Checking addresses...");
|
||||
var server = services.GetRequiredService<IServer>();
|
||||
var addressFeature = server.Features.Get<IServerAddressesFeature>();
|
||||
foreach (var address in addressFeature.Addresses)
|
||||
{
|
||||
Console.WriteLine("Listing on address: " + address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
test/isnd.tests/WebServerCollection.cs
Normal file
11
test/isnd.tests/WebServerCollection.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using Xunit;
|
||||
|
||||
namespace isnd.tests
|
||||
{
|
||||
public class WebServerCollection : ICollectionFixture<WebServerFixture>
|
||||
{
|
||||
// This class has no code, and is never created. Its purpose is simply
|
||||
// to be the place to apply [CollectionDefinition] and all the
|
||||
// ICollectionFixture<> interfaces.
|
||||
}
|
||||
}
|
||||
60
test/isnd.tests/WebServerFixture.cs
Normal file
60
test/isnd.tests/WebServerFixture.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace isnd.tests
|
||||
{
|
||||
|
||||
[CollectionDefinition("Web server collection")]
|
||||
|
||||
|
||||
public class WebServerFixture : IDisposable
|
||||
{
|
||||
public IWebHost Host { get; private set;}
|
||||
public WebServerFixture()
|
||||
{
|
||||
SetupHost();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Host.StopAsync().Wait();
|
||||
Host.Dispose();
|
||||
}
|
||||
|
||||
public void SetupHost()
|
||||
{
|
||||
var webhostBuilder = new WebHostBuilder()
|
||||
.UseKestrel()
|
||||
.UseIISIntegration()
|
||||
.UseContentRoot("../../../../../src/isnd"
|
||||
)
|
||||
.UseStartup(typeof(Startup))
|
||||
.ConfigureAppConfiguration((builderContext, config) =>
|
||||
{
|
||||
config.AddJsonFile("appsettings.json", false);
|
||||
config.AddJsonFile("appsettings.Development.json", false);
|
||||
});
|
||||
|
||||
Host = webhostBuilder.Build();
|
||||
|
||||
Host.Start(); //Starts listening on the configured addresses.
|
||||
PrintAddresses(Host.Services);
|
||||
}
|
||||
|
||||
void PrintAddresses(IServiceProvider services)
|
||||
{
|
||||
Console.WriteLine("Checking addresses...");
|
||||
var server = services.GetRequiredService<IServer>();
|
||||
var addressFeature = server.Features.Get<IServerAddressesFeature>();
|
||||
foreach (var address in addressFeature.Addresses)
|
||||
{
|
||||
Console.WriteLine("Listing on address: " + address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
|
|
|
|||
0
test/isnd.tests/wwwroot/favicon.ico
Normal file
0
test/isnd.tests/wwwroot/favicon.ico
Normal file
Loading…
Add table
Add a link
Reference in a new issue