Skip to content

Commit

Permalink
fixed the wrong http request format and added a unit test for custom …
Browse files Browse the repository at this point in the history
…http header item
  • Loading branch information
kerryjiang committed Jul 30, 2024
1 parent 9962eb9 commit b7e86d1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/WebSocket4Net/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private void WriteHandshakeRequest(PipeWriter writer, string secKey)
writer.Write($"{WebSocketConstant.SecWebSocketProtocol}: {strSubProtocols}\r\n", _asciiEncoding);
}

writer.Write($"{WebSocketConstant.SecWebSocketVersion}: 13\r\n\r\n", _asciiEncoding);
writer.Write($"{WebSocketConstant.SecWebSocketVersion}: 13\r\n", _asciiEncoding);

// Write extra headers
foreach (var header in Headers)
Expand Down
78 changes: 78 additions & 0 deletions test/WebSocket4Net.Tests/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -89,6 +90,83 @@ public async Task TestHandshake(Type hostConfiguratorType)
}
}

[Theory]
[InlineData(typeof(RegularHostConfigurator))]
[InlineData(typeof(SecureHostConfigurator))]
public async Task TestCustomHeaderItems(Type hostConfiguratorType)
{
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);

var serverSessionPath = string.Empty;
var connected = false;

var httpHeaderFromServer = default(NameValueCollection);

var serverConnectedTaskSource = new TaskCompletionSource();

using (var server = CreateWebSocketSocketServerBuilder(builder =>
{
builder.UseSessionHandler(async (s) =>
{
httpHeaderFromServer = (s as WebSocketSession).HttpHeader.Items;
connected = true;
serverConnectedTaskSource.SetResult();
await Task.CompletedTask;
},
async (s, e) =>
{
connected = false;
await Task.CompletedTask;
});

return builder;
}, hostConfigurator: hostConfigurator)
.BuildAsServer())
{
Assert.True(await server.StartAsync());
OutputHelper.WriteLine("Server started.");

var url = $"{hostConfigurator.WebSocketSchema}://{_loopbackIP}:{hostConfigurator.Listener.Port}";

var websocket = new WebSocket(url);

var userId = Guid.NewGuid().ToString();

websocket.Headers["UserId"] = userId;

hostConfigurator.ConfigureClient(websocket);

Assert.Equal(WebSocketState.None, websocket.State);

Assert.True(await websocket.OpenAsync(), "Failed to connect");

Assert.Equal(WebSocketState.Open, websocket.State);

await serverConnectedTaskSource.Task.WaitAsync(TimeSpan.FromSeconds(30));

Assert.True(connected);

Assert.NotNull(httpHeaderFromServer);

var userIdFromHttpHeader = httpHeaderFromServer.Get("UserId");

Assert.NotNull(userIdFromHttpHeader);
Assert.Equal(userId, userIdFromHttpHeader);

await websocket.CloseAsync();

Assert.NotNull(websocket.CloseStatus);
Assert.Equal(CloseReason.NormalClosure, websocket.CloseStatus.Reason);

await Task.Delay(1 * 1000);

Assert.Equal(WebSocketState.Closed, websocket.State);
Assert.False(connected);

await server.StopAsync();
}
}


[Theory]
[InlineData(typeof(RegularHostConfigurator), 10)]
Expand Down

0 comments on commit b7e86d1

Please sign in to comment.