Remove hardcodes and implement WebClient
This commit is contained in:
parent
f25ba8adcd
commit
c316ef0092
11 changed files with 2062 additions and 75 deletions
77
tests/GsDChat.Tests/ServerHistoryTests.cs
Normal file
77
tests/GsDChat.Tests/ServerHistoryTests.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using GsDChat.Client.State;
|
||||
using Xunit;
|
||||
|
||||
namespace GsDChat.Tests;
|
||||
|
||||
public class ServerHistoryTests : IDisposable
|
||||
{
|
||||
private readonly string _testFile;
|
||||
|
||||
public ServerHistoryTests()
|
||||
{
|
||||
_testFile = Path.Combine(Path.GetTempPath(), "gsdchat_servers_" + Guid.NewGuid().ToString("N") + ".json");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(_testFile))
|
||||
{
|
||||
File.Delete(_testFile);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("localhost:5000", "http://localhost:5000")]
|
||||
[InlineData("http://localhost:5000/", "http://localhost:5000")]
|
||||
[InlineData("https://chat.example.com", "https://chat.example.com")]
|
||||
[InlineData("chat.example.com:443", "https://chat.example.com:443")]
|
||||
[InlineData("192.168.1.100:8080", "http://192.168.1.100:8080")]
|
||||
public void NormalizeUrl_ShouldFormatProperly(string input, string expected)
|
||||
{
|
||||
string result = ServerHistoryManager.NormalizeUrl(input);
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveServer_ShouldPersistAndOrderDescending()
|
||||
{
|
||||
var manager = new ServerHistoryManager(_testFile);
|
||||
|
||||
manager.SaveServer("http://server1.local:5000", "Server 1", "User1");
|
||||
manager.SaveServer("http://server2.local:5000", "Server 2", "User2");
|
||||
|
||||
var list = manager.LoadServers();
|
||||
Assert.Equal(2, list.Count);
|
||||
Assert.Equal("http://server2.local:5000", list[0].Url);
|
||||
Assert.Equal("http://server1.local:5000", list[1].Url);
|
||||
|
||||
// Updating server1 should bump it to the top
|
||||
manager.SaveServer("http://server1.local:5000", "Server 1 Updated", "User1New");
|
||||
var updatedList = manager.LoadServers();
|
||||
Assert.Equal(2, updatedList.Count);
|
||||
Assert.Equal("http://server1.local:5000", updatedList[0].Url);
|
||||
Assert.Equal("Server 1 Updated", updatedList[0].Name);
|
||||
Assert.Equal("User1New", updatedList[0].LastUsername);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveServer_ShouldDeleteEntry()
|
||||
{
|
||||
var manager = new ServerHistoryManager(_testFile);
|
||||
|
||||
manager.SaveServer("http://server1.local:5000", "Server 1", "User1");
|
||||
manager.SaveServer("http://server2.local:5000", "Server 2", "User2");
|
||||
|
||||
manager.RemoveServer("http://server1.local:5000");
|
||||
|
||||
var list = manager.LoadServers();
|
||||
Assert.Single(list);
|
||||
Assert.Equal("http://server2.local:5000", list[0].Url);
|
||||
}
|
||||
}
|
||||
130
tests/GsDChat.Tests/WebClientStaticTests.cs
Normal file
130
tests/GsDChat.Tests/WebClientStaticTests.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using GsDChat.Server.Configuration;
|
||||
using GsDChat.Server.Data;
|
||||
using GsDChat.Server.Services;
|
||||
using GsDChat.Server.WebSockets;
|
||||
using Xunit;
|
||||
|
||||
namespace GsDChat.Tests;
|
||||
|
||||
public class WebClientStaticTests : IAsyncLifetime
|
||||
{
|
||||
private readonly string _testDir;
|
||||
private WebApplication? _app;
|
||||
private int _port;
|
||||
private string _serverUrl = string.Empty;
|
||||
private HttpClient? _httpClient;
|
||||
|
||||
public WebClientStaticTests()
|
||||
{
|
||||
_testDir = Path.Combine(Path.GetTempPath(), "gsdchat_webclient_" + Guid.NewGuid().ToString("N"));
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
using var listener = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
_port = ((System.Net.IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
|
||||
_serverUrl = $"http://127.0.0.1:{_port}";
|
||||
|
||||
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
|
||||
{
|
||||
ContentRootPath = Path.Combine(AppContext.BaseDirectory),
|
||||
WebRootPath = Path.Combine(AppContext.BaseDirectory, "wwwroot")
|
||||
});
|
||||
|
||||
builder.WebHost.UseUrls(_serverUrl);
|
||||
builder.Logging.ClearProviders();
|
||||
|
||||
var serverOptions = new ServerOptions
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
Port = _port,
|
||||
UseTls = false,
|
||||
DataDirectory = _testDir,
|
||||
ServerName = "WebClient Test Server"
|
||||
};
|
||||
|
||||
builder.Services.AddSingleton(serverOptions);
|
||||
builder.Services.AddSingleton(sp => new ChatDatabase(serverOptions.DataDirectory));
|
||||
builder.Services.AddSingleton<IAuthService, AuthService>();
|
||||
builder.Services.AddSingleton<FileStorageService>();
|
||||
builder.Services.AddSingleton<ChatWebSocketManager>();
|
||||
|
||||
_app = builder.Build();
|
||||
|
||||
_app.UseDefaultFiles();
|
||||
_app.UseStaticFiles();
|
||||
|
||||
await _app.StartAsync();
|
||||
_httpClient = new HttpClient { BaseAddress = new Uri(_serverUrl) };
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_httpClient?.Dispose();
|
||||
if (_app != null)
|
||||
{
|
||||
await _app.StopAsync();
|
||||
await _app.DisposeAsync();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(_testDir))
|
||||
{
|
||||
Directory.Delete(_testDir, true);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRoot_ShouldServeIndexHtml()
|
||||
{
|
||||
Assert.NotNull(_httpClient);
|
||||
var response = await _httpClient.GetAsync("/");
|
||||
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
string html = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.Contains("GsDChat - Web Client", html);
|
||||
Assert.Contains("id=\"app\"", html);
|
||||
Assert.Contains("id=\"authModal\"", html);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAppCss_ShouldServeStylesheet()
|
||||
{
|
||||
Assert.NotNull(_httpClient);
|
||||
var response = await _httpClient.GetAsync("/app.css");
|
||||
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
string css = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.Contains("--bg-main", css);
|
||||
Assert.Contains(".app-layout", css);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAppJs_ShouldServeJavaScript()
|
||||
{
|
||||
Assert.NotNull(_httpClient);
|
||||
var response = await _httpClient.GetAsync("/app.js");
|
||||
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
string js = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.Contains("connectWebSocket", js);
|
||||
Assert.Contains("handleNetworkEnvelope", js);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue