11using System ;
2+ using System . Text ;
23using System . Collections . Generic ;
34using System . Reactive . Linq ;
45using NEventSocket . FreeSwitch ;
6+ using NEventSocket . Logging ;
57using NEventSocket . Sockets ;
68using NEventSocket . Tests . Properties ;
79using NEventSocket . Tests . TestSupport ;
810using NEventSocket . Util ;
911using Xunit ;
1012
13+ using Microsoft . Extensions . Logging ;
14+
1115namespace NEventSocket . Tests . Sockets
1216{
1317 public class MessageParsingTests
1418 {
15- [ Theory , MemberData ( nameof ( ExampleMessages ) ) ]
19+ public MessageParsingTests ( )
20+ {
21+ PreventThreadPoolStarvation . Init ( ) ;
22+ Logger . Configure ( LoggerFactory . Create ( builder =>
23+ {
24+ builder
25+ . AddFilter ( "Microsoft" , LogLevel . Warning )
26+ . AddFilter ( "System" , LogLevel . Warning )
27+ . AddFilter ( "LoggingConsoleApp.Program" , LogLevel . Debug )
28+ . AddConsole ( ) ;
29+ } ) ) ;
30+ }
31+
32+ [ Theory , MemberData ( nameof ( ExampleMessages ) ) ]
1633 public void it_should_parse_the_expected_messages_from_a_stream ( int expectedMessageCount , string exampleInput )
1734 {
1835 int parsedMessageCount = 0 ;
19-
20- exampleInput . ToObservable ( )
21- . AggregateUntil ( ( ) => new Parser ( ) , ( builder , ch ) => builder . Append ( ch ) , builder => builder . Completed )
36+ byte [ ] exampleByteInput = Encoding . UTF8 . GetBytes ( exampleInput ) ;
37+ exampleByteInput . ToObservable ( )
38+ . AggregateUntil ( ( ) => new Parser ( ) , ( builder , b ) => builder . Append ( b ) , builder => builder . Completed )
2239 . Select ( parser => parser . ExtractMessage ( ) )
2340 . Subscribe ( _ => parsedMessageCount ++ ) ;
2441
@@ -31,14 +48,17 @@ public void it_should_parse_the_expected_messages_from_a_stream(int expectedMess
3148 [ InlineData ( TestMessages . ConnectEvent ) ]
3249 [ InlineData ( TestMessages . DisconnectEvent ) ]
3350 [ InlineData ( TestMessages . PlaybackComplete ) ]
51+ [ InlineData ( TestMessages . DetectedSpeech ) ]
52+ [ InlineData ( TestMessages . DetectedSpeechEnglish ) ]
3453 public void can_parse_test_messages ( string input )
3554 {
3655 var parser = new Parser ( ) ;
3756 var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
57+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
3858
39- foreach ( char c in rawInput )
59+ foreach ( byte b in byteInput )
4060 {
41- parser . Append ( c ) ;
61+ parser . Append ( b ) ;
4262 }
4363
4464 Assert . True ( parser . Completed ) ;
@@ -51,35 +71,43 @@ public void can_parse_test_messages(string input)
5171 [ Theory ]
5272 [ InlineData ( TestMessages . BackgroundJob ) ]
5373 [ InlineData ( TestMessages . CallState ) ]
74+ [ InlineData ( TestMessages . DetectedSpeech ) ]
75+ [ InlineData ( TestMessages . DetectedSpeechEnglish ) ]
5476 public void it_should_extract_the_body_from_a_message ( string input )
5577 {
5678 var parser = new Parser ( ) ;
5779 var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
58- foreach ( char c in rawInput )
80+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
81+
82+ foreach ( byte b in byteInput )
5983 {
60- parser . Append ( c ) ;
84+ parser . Append ( b ) ;
6185 }
6286
6387 Assert . True ( parser . Completed ) ;
6488
6589 BasicMessage payload = parser . ExtractMessage ( ) ;
6690 Assert . Equal ( ContentTypes . EventPlain , payload . ContentType ) ;
6791 Assert . NotNull ( payload . BodyText ) ;
68- Assert . Equal ( payload . ContentLength , payload . BodyText . Length ) ;
92+ Assert . Equal ( payload . ContentLength , payload . BodyBytes . Length ) ;
6993
7094 Console . WriteLine ( payload . ToString ( ) ) ;
7195 }
7296
7397 [ Theory ]
7498 [ InlineData ( TestMessages . BackgroundJob , EventName . BackgroundJob ) ]
7599 [ InlineData ( TestMessages . CallState , EventName . ChannelCallstate ) ]
100+ [ InlineData ( TestMessages . DetectedSpeech , EventName . DetectedSpeech ) ]
101+ [ InlineData ( TestMessages . DetectedSpeechEnglish , EventName . DetectedSpeech ) ]
76102 public void it_should_parse_event_messages ( string input , EventName eventName )
77103 {
78104 var parser = new Parser ( ) ;
79105 var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
80- foreach ( char c in rawInput )
106+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
107+
108+ foreach ( byte b in byteInput )
81109 {
82- parser . Append ( c ) ;
110+ parser . Append ( b ) ;
83111 }
84112
85113 Assert . True ( parser . Completed ) ;
@@ -91,16 +119,44 @@ public void it_should_parse_event_messages(string input, EventName eventName)
91119 Console . WriteLine ( eventMessage . ToString ( ) ) ;
92120 }
93121
122+ [ Theory ]
123+ [ InlineData ( TestMessages . DetectedSpeech , EventName . DetectedSpeech ) ]
124+ [ InlineData ( TestMessages . DetectedSpeechEnglish , EventName . DetectedSpeech ) ]
125+ public void it_should_parse_event_messages_and_extract_body_payload ( string input , EventName eventName )
126+ {
127+ var parser = new Parser ( ) ;
128+ var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
129+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
130+
131+ foreach ( byte b in byteInput )
132+ {
133+ parser . Append ( b ) ;
134+ }
135+
136+ Assert . True ( parser . Completed ) ;
137+
138+ var eventMessage = new EventMessage ( parser . ExtractMessage ( ) ) ;
139+ Assert . NotNull ( eventMessage ) ;
140+ Assert . Equal ( eventName , eventMessage . EventName ) ;
141+
142+ var contentLength = int . Parse ( eventMessage . Headers [ HeaderNames . ContentLength ] ) ;
143+
144+ Assert . Equal ( contentLength , Encoding . UTF8 . GetByteCount ( eventMessage . BodyText ) ) ;
145+
146+ Console . WriteLine ( eventMessage . ToString ( ) ) ;
147+ }
148+
94149 [ Fact ]
95150 public void it_should_parse_BackgroundJobResult_OK ( )
96151 {
97152 var input = TestMessages . BackgroundJob ;
98153 var parser = new Parser ( ) ;
99154 var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
155+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
100156
101- foreach ( char c in rawInput )
157+ foreach ( byte b in byteInput )
102158 {
103- parser . Append ( c ) ;
159+ parser . Append ( b ) ;
104160 }
105161
106162 Assert . True ( parser . Completed ) ;
@@ -118,10 +174,11 @@ public void it_should_parse_BackgroundJobResult_ERR()
118174 var input = TestMessages . BackgroundJobError ;
119175 var parser = new Parser ( ) ;
120176 var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
177+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
121178
122- foreach ( char c in rawInput )
179+ foreach ( byte b in byteInput )
123180 {
124- parser . Append ( c ) ;
181+ parser . Append ( b ) ;
125182 }
126183
127184 Assert . True ( parser . Completed ) ;
@@ -139,10 +196,11 @@ public void it_should_parse_Command_Reply_OK()
139196 {
140197 var parser = new Parser ( ) ;
141198 var rawInput = "Content-Type: command/reply\n Reply-Text: +OK\n \n " ;
199+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
142200
143- foreach ( char c in rawInput )
201+ foreach ( byte b in byteInput )
144202 {
145- parser . Append ( c ) ;
203+ parser . Append ( b ) ;
146204 }
147205
148206 Assert . True ( parser . Completed ) ;
@@ -159,10 +217,11 @@ public void it_should_parse_Command_Reply_ERR()
159217 {
160218 var parser = new Parser ( ) ;
161219 var rawInput = "Content-Type: command/reply\n Reply-Text: -ERR Error\n \n " ;
220+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
162221
163- foreach ( char c in rawInput )
222+ foreach ( byte b in byteInput )
164223 {
165- parser . Append ( c ) ;
224+ parser . Append ( b ) ;
166225 }
167226
168227 Assert . True ( parser . Completed ) ;
@@ -180,10 +239,11 @@ public void it_should_parse_Api_Response_OK()
180239 {
181240 var parser = new Parser ( ) ;
182241 var rawInput = "Content-Type: api/response\n Content-Length: 3\n \n +OK" ;
242+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
183243
184- foreach ( char c in rawInput )
244+ foreach ( byte b in byteInput )
185245 {
186- parser . Append ( c ) ;
246+ parser . Append ( b ) ;
187247 }
188248
189249 Assert . True ( parser . Completed ) ;
@@ -200,10 +260,11 @@ public void it_should_parse_Api_Response_ERR()
200260 {
201261 var parser = new Parser ( ) ;
202262 var rawInput = "Content-Type: api/response\n Content-Length: 10\n \n -ERR Error" ;
263+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
203264
204- foreach ( char c in rawInput )
265+ foreach ( byte b in byteInput )
205266 {
206- parser . Append ( c ) ;
267+ parser . Append ( b ) ;
207268 }
208269
209270 Assert . True ( parser . Completed ) ;
@@ -221,10 +282,11 @@ public void it_should_treat_Api_Response_ERR_no_reply_as_Success()
221282 {
222283 var parser = new Parser ( ) ;
223284 var rawInput = "Content-Type: api/response\n Content-Length: 13\n \n -ERR no reply" ;
285+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
224286
225- foreach ( char c in rawInput )
287+ foreach ( byte b in byteInput )
226288 {
227- parser . Append ( c ) ;
289+ parser . Append ( b ) ;
228290 }
229291
230292 Assert . True ( parser . Completed ) ;
@@ -242,10 +304,11 @@ public void it_should_trim_new_lines_from__the_end_of_ApiResponse_Body_text()
242304 {
243305 var parser = new Parser ( ) ;
244306 var rawInput = "Content-Type: api/response\n Content-Length: 14\n \n -ERR no reply\n " ;
307+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
245308
246- foreach ( char c in rawInput )
309+ foreach ( byte b in byteInput )
247310 {
248- parser . Append ( c ) ;
311+ parser . Append ( b ) ;
249312 }
250313
251314 Assert . True ( parser . Completed ) ;
@@ -269,9 +332,10 @@ public void Can_parse_example_sessions_to_completion(string input)
269332 }
270333
271334 bool gotDisconnectNotice = false ;
335+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( input ) ;
272336
273- input . ToObservable ( )
274- . AggregateUntil ( ( ) => new Parser ( ) , ( builder , ch ) => builder . Append ( ch ) , builder => builder . Completed )
337+ byteInput . ToObservable ( )
338+ . AggregateUntil ( ( ) => new Parser ( ) , ( builder , b ) => builder . Append ( b ) , builder => builder . Completed )
275339 . Select ( parser => parser . ExtractMessage ( ) )
276340 . Subscribe (
277341 m =>
@@ -297,8 +361,9 @@ public void Can_parse_disconnect_notice()
297361Disconnected, goodbye.
298362See you at ClueCon! http://www.cluecon.com/
299363" ;
300- msg . ToObservable ( )
301- . AggregateUntil ( ( ) => new Parser ( ) , ( builder , ch ) => builder . Append ( ch ) , builder => builder . Completed )
364+ byte [ ] byteMsg = Encoding . UTF8 . GetBytes ( msg ) ;
365+ byteMsg . ToObservable ( )
366+ . AggregateUntil ( ( ) => new Parser ( ) , ( builder , b ) => builder . Append ( b ) , builder => builder . Completed )
302367 . Select ( parser => parser . ExtractMessage ( ) )
303368 . Subscribe (
304369 Console . WriteLine ) ;
0 commit comments