Initial commit: Client Doc docs Server Tools
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using XWorld.Server.Auth;
|
||||
using XWorld.Server.Gateway;
|
||||
|
||||
namespace XWorld.Server.Gateway.Tests
|
||||
{
|
||||
public class AuthWsTests
|
||||
{
|
||||
static string GamesRoot() => System.IO.Path.Combine(AppContext.BaseDirectory, "TestGames");
|
||||
|
||||
[Fact]
|
||||
public async Task Ws_with_valid_token_connects()
|
||||
{
|
||||
var key = new byte[32];
|
||||
var host = new GameServerHost();
|
||||
await host.StartAsync(GamesRoot(), 100, authSecret: key,
|
||||
authDbPath: "Data Source=wsok_" + Guid.NewGuid().ToString("N") + ";Mode=Memory;Cache=Shared");
|
||||
try
|
||||
{
|
||||
string token = new TokenService(key).Issue(1234, TimeSpan.FromHours(1));
|
||||
using var ws = new ClientWebSocket();
|
||||
var uri = new Uri(host.WsBaseUrl + "/ws?token=" + token);
|
||||
await ws.ConnectAsync(uri, CancellationToken.None); // 不抛即握手成功
|
||||
Assert.Equal(WebSocketState.Open, ws.State);
|
||||
}
|
||||
finally { await host.StopAsync(); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ws_with_invalid_token_rejected()
|
||||
{
|
||||
var key = new byte[32];
|
||||
var host = new GameServerHost();
|
||||
await host.StartAsync(GamesRoot(), 100, authSecret: key,
|
||||
authDbPath: "Data Source=wsbad_" + Guid.NewGuid().ToString("N") + ";Mode=Memory;Cache=Shared");
|
||||
try
|
||||
{
|
||||
using var ws = new ClientWebSocket();
|
||||
var uri = new Uri(host.WsBaseUrl + "/ws?token=garbage");
|
||||
await Assert.ThrowsAnyAsync<WebSocketException>(() => ws.ConnectAsync(uri, CancellationToken.None));
|
||||
}
|
||||
finally { await host.StopAsync(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user