Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse messages and events based on byte arrays instead of strings #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 96 additions & 31 deletions NEventSocket.Tests/Sockets/MessageParsingTests.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Reactive.Linq;
using NEventSocket.FreeSwitch;
using NEventSocket.Logging;
using NEventSocket.Sockets;
using NEventSocket.Tests.Properties;
using NEventSocket.Tests.TestSupport;
using NEventSocket.Util;
using Xunit;

using Microsoft.Extensions.Logging;

namespace NEventSocket.Tests.Sockets
{
public class MessageParsingTests
{
[Theory, MemberData(nameof(ExampleMessages))]
public MessageParsingTests()
{
PreventThreadPoolStarvation.Init();
Logger.Configure(LoggerFactory.Create(builder =>
{
builder
.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("LoggingConsoleApp.Program", LogLevel.Debug)
.AddConsole();
}));
}

[Theory, MemberData(nameof(ExampleMessages))]
public void it_should_parse_the_expected_messages_from_a_stream(int expectedMessageCount, string exampleInput)
{
int parsedMessageCount = 0;

exampleInput.ToObservable()
.AggregateUntil(() => new Parser(), (builder, ch) => builder.Append(ch), builder => builder.Completed)
byte[] exampleByteInput = Encoding.UTF8.GetBytes(exampleInput);
exampleByteInput.ToObservable()
.AggregateUntil(() => new Parser(), (builder, b) => builder.Append(b), builder => builder.Completed)
.Select(parser => parser.ExtractMessage())
.Subscribe(_ => parsedMessageCount++);

Expand All @@ -31,14 +48,17 @@ public void it_should_parse_the_expected_messages_from_a_stream(int expectedMess
[InlineData(TestMessages.ConnectEvent)]
[InlineData(TestMessages.DisconnectEvent)]
[InlineData(TestMessages.PlaybackComplete)]
[InlineData(TestMessages.DetectedSpeech)]
[InlineData(TestMessages.DetectedSpeechEnglish)]
public void can_parse_test_messages(string input)
{
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -51,35 +71,43 @@ public void can_parse_test_messages(string input)
[Theory]
[InlineData(TestMessages.BackgroundJob)]
[InlineData(TestMessages.CallState)]
[InlineData(TestMessages.DetectedSpeech)]
[InlineData(TestMessages.DetectedSpeechEnglish)]
public void it_should_extract_the_body_from_a_message(string input)
{
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
foreach (char c in rawInput)
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);

BasicMessage payload = parser.ExtractMessage();
Assert.Equal(ContentTypes.EventPlain, payload.ContentType);
Assert.NotNull(payload.BodyText);
Assert.Equal(payload.ContentLength, payload.BodyText.Length);
Assert.Equal(payload.ContentLength, payload.BodyBytes.Length);

Console.WriteLine(payload.ToString());
}

[Theory]
[InlineData(TestMessages.BackgroundJob, EventName.BackgroundJob)]
[InlineData(TestMessages.CallState, EventName.ChannelCallstate)]
[InlineData(TestMessages.DetectedSpeech, EventName.DetectedSpeech)]
[InlineData(TestMessages.DetectedSpeechEnglish, EventName.DetectedSpeech)]
public void it_should_parse_event_messages(string input, EventName eventName)
{
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
foreach (char c in rawInput)
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -91,16 +119,44 @@ public void it_should_parse_event_messages(string input, EventName eventName)
Console.WriteLine(eventMessage.ToString());
}

[Theory]
[InlineData(TestMessages.DetectedSpeech, EventName.DetectedSpeech)]
[InlineData(TestMessages.DetectedSpeechEnglish, EventName.DetectedSpeech)]
public void it_should_parse_event_messages_and_extract_body_payload(string input, EventName eventName)
{
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (byte b in byteInput)
{
parser.Append(b);
}

Assert.True(parser.Completed);

var eventMessage = new EventMessage(parser.ExtractMessage());
Assert.NotNull(eventMessage);
Assert.Equal(eventName, eventMessage.EventName);

var contentLength = int.Parse(eventMessage.Headers[HeaderNames.ContentLength]);

Assert.Equal(contentLength, Encoding.UTF8.GetByteCount(eventMessage.BodyText));

Console.WriteLine(eventMessage.ToString());
}

[Fact]
public void it_should_parse_BackgroundJobResult_OK()
{
var input = TestMessages.BackgroundJob;
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -118,10 +174,11 @@ public void it_should_parse_BackgroundJobResult_ERR()
var input = TestMessages.BackgroundJobError;
var parser = new Parser();
var rawInput = input.Replace("\r\n", "\n") + "\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -139,10 +196,11 @@ public void it_should_parse_Command_Reply_OK()
{
var parser = new Parser();
var rawInput = "Content-Type: command/reply\nReply-Text: +OK\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -159,10 +217,11 @@ public void it_should_parse_Command_Reply_ERR()
{
var parser = new Parser();
var rawInput = "Content-Type: command/reply\nReply-Text: -ERR Error\n\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -180,10 +239,11 @@ public void it_should_parse_Api_Response_OK()
{
var parser = new Parser();
var rawInput = "Content-Type: api/response\nContent-Length: 3\n\n+OK";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -200,10 +260,11 @@ public void it_should_parse_Api_Response_ERR()
{
var parser = new Parser();
var rawInput = "Content-Type: api/response\nContent-Length: 10\n\n-ERR Error";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -221,10 +282,11 @@ public void it_should_treat_Api_Response_ERR_no_reply_as_Success()
{
var parser = new Parser();
var rawInput = "Content-Type: api/response\nContent-Length: 13\n\n-ERR no reply";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -242,10 +304,11 @@ public void it_should_trim_new_lines_from__the_end_of_ApiResponse_Body_text()
{
var parser = new Parser();
var rawInput = "Content-Type: api/response\nContent-Length: 14\n\n-ERR no reply\n";
byte[] byteInput = Encoding.UTF8.GetBytes(rawInput);

foreach (char c in rawInput)
foreach (byte b in byteInput)
{
parser.Append(c);
parser.Append(b);
}

Assert.True(parser.Completed);
Expand All @@ -269,9 +332,10 @@ public void Can_parse_example_sessions_to_completion(string input)
}

bool gotDisconnectNotice = false;
byte[] byteInput = Encoding.UTF8.GetBytes(input);

input.ToObservable()
.AggregateUntil(() => new Parser(), (builder, ch) => builder.Append(ch), builder => builder.Completed)
byteInput.ToObservable()
.AggregateUntil(() => new Parser(), (builder, b) => builder.Append(b), builder => builder.Completed)
.Select(parser => parser.ExtractMessage())
.Subscribe(
m =>
Expand All @@ -297,8 +361,9 @@ public void Can_parse_disconnect_notice()
Disconnected, goodbye.
See you at ClueCon! http://www.cluecon.com/
";
msg.ToObservable()
.AggregateUntil(() => new Parser(), (builder, ch) => builder.Append(ch), builder => builder.Completed)
byte[] byteMsg = Encoding.UTF8.GetBytes(msg);
byteMsg.ToObservable()
.AggregateUntil(() => new Parser(), (builder, b) => builder.Append(b), builder => builder.Completed)
.Select(parser => parser.ExtractMessage())
.Subscribe(
Console.WriteLine);
Expand Down
Loading