Skip to content

Commit

Permalink
test: add test for bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
doghappy committed Jan 11, 2021
1 parent 9a56079 commit 45f9ff2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/SocketIOClient.Test/SocketIOTests/V3EmitTest.cs
@@ -1,5 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SocketIOClient.Test.Models;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SocketIOClient.Test.SocketIOTests
Expand Down Expand Up @@ -45,7 +47,7 @@ public async Task NspHiTest()
EIO = 4,
Query = new Dictionary<string, string>
{
{ "token", "v4" }
{ "token", "v3" }
}
});
client.On("hi", response =>
Expand All @@ -62,5 +64,41 @@ public async Task NspHiTest()

Assert.AreEqual("nsp: socket.io v3", result);
}

[TestMethod]
public async Task BinaryEventTest()
{
ByteResponse result = null;
var client = new SocketIO(ConnectAsyncTest.V4_URL, new SocketIOOptions
{
Reconnection = false,
EIO = 4,
Query = new Dictionary<string, string>
{
{ "token", "v3" }
}
});
client.On("bytes", response => result = response.GetValue<ByteResponse>());

const string dotNetCore = ".net core";
const string client001 = "client001";
const string name = "unit test";

client.OnConnected += async (sender, e) =>
{
await client.EmitAsync("bytes", name, new
{
source = client001,
bytes = Encoding.UTF8.GetBytes(dotNetCore)
});
};
await client.ConnectAsync();
await Task.Delay(200);
await client.DisconnectAsync();

Assert.AreEqual("client001", result.ClientSource);
Assert.AreEqual("server", result.Source);
Assert.AreEqual($"{dotNetCore} - server - {name}", Encoding.UTF8.GetString(result.Buffer));
}
}
}
9 changes: 9 additions & 0 deletions src/socket.io-server-v3/app.js
Expand Up @@ -24,6 +24,15 @@ io.on('connection', socket => {
io.emit('hi', 'io: ' + msg);
});

socket.on("bytes", (name, data) => {
const bytes = Buffer.from(data.bytes.toString() + " - server - " + name, "utf-8");
socket.emit("bytes", {
clientSource: data.source,
source: "server",
bytes
});
});

socket.on("sever disconnect", close => {
socket.disconnect(close)
});
Expand Down

0 comments on commit 45f9ff2

Please sign in to comment.