67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
using XWorld.Server.Gateway;
|
|
|
|
namespace XWorld.Server.Gateway.Tests
|
|
{
|
|
public class StaticFileServerTests
|
|
{
|
|
[Fact]
|
|
public async Task ServesExistingFile_200_WithExactBytes()
|
|
{
|
|
string root = Path.Combine(Path.GetTempPath(), "xw-cdn-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(Path.Combine(root, "minigame", "rps", "2"));
|
|
byte[] payload = new byte[] { 1, 2, 3, 4, 250, 99, 0, 7 };
|
|
File.WriteAllBytes(Path.Combine(root, "minigame", "rps", "2", "files.txt"), payload);
|
|
|
|
var cdn = new StaticFileServer(root, port: 0);
|
|
await cdn.StartAsync();
|
|
try
|
|
{
|
|
using var http = new HttpClient();
|
|
HttpResponseMessage res = await http.GetAsync($"{cdn.HttpBaseUrl}/minigame/rps/2/files.txt");
|
|
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
|
|
byte[] got = await res.Content.ReadAsByteArrayAsync();
|
|
Assert.Equal(payload, got);
|
|
}
|
|
finally
|
|
{
|
|
await cdn.StopAsync();
|
|
try { Directory.Delete(root, true); } catch { }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task MissingFile_404()
|
|
{
|
|
string root = Path.Combine(Path.GetTempPath(), "xw-cdn-" + Guid.NewGuid().ToString("N"));
|
|
var cdn = new StaticFileServer(root, port: 0);
|
|
await cdn.StartAsync();
|
|
try
|
|
{
|
|
using var http = new HttpClient();
|
|
HttpResponseMessage res = await http.GetAsync($"{cdn.HttpBaseUrl}/minigame/nope/1/game.json");
|
|
Assert.Equal(HttpStatusCode.NotFound, res.StatusCode);
|
|
}
|
|
finally
|
|
{
|
|
await cdn.StopAsync();
|
|
try { Directory.Delete(root, true); } catch { }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void LanIp_ReturnsNonLoopbackIPv4_OrFallback()
|
|
{
|
|
string ip = LanIp.Resolve();
|
|
Assert.False(string.IsNullOrWhiteSpace(ip));
|
|
Assert.True(System.Net.IPAddress.TryParse(ip, out var parsed));
|
|
Assert.Equal(System.Net.Sockets.AddressFamily.InterNetwork, parsed.AddressFamily);
|
|
}
|
|
}
|
|
}
|