1
1
using System ;
2
+ using System . Text ;
2
3
using System . Collections . Generic ;
3
4
using System . Reactive . Linq ;
4
5
using NEventSocket . FreeSwitch ;
6
+ using NEventSocket . Logging ;
5
7
using NEventSocket . Sockets ;
6
8
using NEventSocket . Tests . Properties ;
7
9
using NEventSocket . Tests . TestSupport ;
8
10
using NEventSocket . Util ;
9
11
using Xunit ;
10
12
13
+ using Microsoft . Extensions . Logging ;
14
+
11
15
namespace NEventSocket . Tests . Sockets
12
16
{
13
17
public class MessageParsingTests
14
18
{
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 ) ) ]
16
33
public void it_should_parse_the_expected_messages_from_a_stream ( int expectedMessageCount , string exampleInput )
17
34
{
18
35
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 )
22
39
. Select ( parser => parser . ExtractMessage ( ) )
23
40
. Subscribe ( _ => parsedMessageCount ++ ) ;
24
41
@@ -31,14 +48,17 @@ public void it_should_parse_the_expected_messages_from_a_stream(int expectedMess
31
48
[ InlineData ( TestMessages . ConnectEvent ) ]
32
49
[ InlineData ( TestMessages . DisconnectEvent ) ]
33
50
[ InlineData ( TestMessages . PlaybackComplete ) ]
51
+ [ InlineData ( TestMessages . DetectedSpeech ) ]
52
+ [ InlineData ( TestMessages . DetectedSpeechEnglish ) ]
34
53
public void can_parse_test_messages ( string input )
35
54
{
36
55
var parser = new Parser ( ) ;
37
56
var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
57
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
38
58
39
- foreach ( char c in rawInput )
59
+ foreach ( byte b in byteInput )
40
60
{
41
- parser . Append ( c ) ;
61
+ parser . Append ( b ) ;
42
62
}
43
63
44
64
Assert . True ( parser . Completed ) ;
@@ -51,35 +71,43 @@ public void can_parse_test_messages(string input)
51
71
[ Theory ]
52
72
[ InlineData ( TestMessages . BackgroundJob ) ]
53
73
[ InlineData ( TestMessages . CallState ) ]
74
+ [ InlineData ( TestMessages . DetectedSpeech ) ]
75
+ [ InlineData ( TestMessages . DetectedSpeechEnglish ) ]
54
76
public void it_should_extract_the_body_from_a_message ( string input )
55
77
{
56
78
var parser = new Parser ( ) ;
57
79
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 )
59
83
{
60
- parser . Append ( c ) ;
84
+ parser . Append ( b ) ;
61
85
}
62
86
63
87
Assert . True ( parser . Completed ) ;
64
88
65
89
BasicMessage payload = parser . ExtractMessage ( ) ;
66
90
Assert . Equal ( ContentTypes . EventPlain , payload . ContentType ) ;
67
91
Assert . NotNull ( payload . BodyText ) ;
68
- Assert . Equal ( payload . ContentLength , payload . BodyText . Length ) ;
92
+ Assert . Equal ( payload . ContentLength , payload . BodyBytes . Length ) ;
69
93
70
94
Console . WriteLine ( payload . ToString ( ) ) ;
71
95
}
72
96
73
97
[ Theory ]
74
98
[ InlineData ( TestMessages . BackgroundJob , EventName . BackgroundJob ) ]
75
99
[ InlineData ( TestMessages . CallState , EventName . ChannelCallstate ) ]
100
+ [ InlineData ( TestMessages . DetectedSpeech , EventName . DetectedSpeech ) ]
101
+ [ InlineData ( TestMessages . DetectedSpeechEnglish , EventName . DetectedSpeech ) ]
76
102
public void it_should_parse_event_messages ( string input , EventName eventName )
77
103
{
78
104
var parser = new Parser ( ) ;
79
105
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 )
81
109
{
82
- parser . Append ( c ) ;
110
+ parser . Append ( b ) ;
83
111
}
84
112
85
113
Assert . True ( parser . Completed ) ;
@@ -91,16 +119,44 @@ public void it_should_parse_event_messages(string input, EventName eventName)
91
119
Console . WriteLine ( eventMessage . ToString ( ) ) ;
92
120
}
93
121
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
+
94
149
[ Fact ]
95
150
public void it_should_parse_BackgroundJobResult_OK ( )
96
151
{
97
152
var input = TestMessages . BackgroundJob ;
98
153
var parser = new Parser ( ) ;
99
154
var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
155
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
100
156
101
- foreach ( char c in rawInput )
157
+ foreach ( byte b in byteInput )
102
158
{
103
- parser . Append ( c ) ;
159
+ parser . Append ( b ) ;
104
160
}
105
161
106
162
Assert . True ( parser . Completed ) ;
@@ -118,10 +174,11 @@ public void it_should_parse_BackgroundJobResult_ERR()
118
174
var input = TestMessages . BackgroundJobError ;
119
175
var parser = new Parser ( ) ;
120
176
var rawInput = input . Replace ( "\r \n " , "\n " ) + "\n \n " ;
177
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
121
178
122
- foreach ( char c in rawInput )
179
+ foreach ( byte b in byteInput )
123
180
{
124
- parser . Append ( c ) ;
181
+ parser . Append ( b ) ;
125
182
}
126
183
127
184
Assert . True ( parser . Completed ) ;
@@ -139,10 +196,11 @@ public void it_should_parse_Command_Reply_OK()
139
196
{
140
197
var parser = new Parser ( ) ;
141
198
var rawInput = "Content-Type: command/reply\n Reply-Text: +OK\n \n " ;
199
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
142
200
143
- foreach ( char c in rawInput )
201
+ foreach ( byte b in byteInput )
144
202
{
145
- parser . Append ( c ) ;
203
+ parser . Append ( b ) ;
146
204
}
147
205
148
206
Assert . True ( parser . Completed ) ;
@@ -159,10 +217,11 @@ public void it_should_parse_Command_Reply_ERR()
159
217
{
160
218
var parser = new Parser ( ) ;
161
219
var rawInput = "Content-Type: command/reply\n Reply-Text: -ERR Error\n \n " ;
220
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
162
221
163
- foreach ( char c in rawInput )
222
+ foreach ( byte b in byteInput )
164
223
{
165
- parser . Append ( c ) ;
224
+ parser . Append ( b ) ;
166
225
}
167
226
168
227
Assert . True ( parser . Completed ) ;
@@ -180,10 +239,11 @@ public void it_should_parse_Api_Response_OK()
180
239
{
181
240
var parser = new Parser ( ) ;
182
241
var rawInput = "Content-Type: api/response\n Content-Length: 3\n \n +OK" ;
242
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
183
243
184
- foreach ( char c in rawInput )
244
+ foreach ( byte b in byteInput )
185
245
{
186
- parser . Append ( c ) ;
246
+ parser . Append ( b ) ;
187
247
}
188
248
189
249
Assert . True ( parser . Completed ) ;
@@ -200,10 +260,11 @@ public void it_should_parse_Api_Response_ERR()
200
260
{
201
261
var parser = new Parser ( ) ;
202
262
var rawInput = "Content-Type: api/response\n Content-Length: 10\n \n -ERR Error" ;
263
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
203
264
204
- foreach ( char c in rawInput )
265
+ foreach ( byte b in byteInput )
205
266
{
206
- parser . Append ( c ) ;
267
+ parser . Append ( b ) ;
207
268
}
208
269
209
270
Assert . True ( parser . Completed ) ;
@@ -221,10 +282,11 @@ public void it_should_treat_Api_Response_ERR_no_reply_as_Success()
221
282
{
222
283
var parser = new Parser ( ) ;
223
284
var rawInput = "Content-Type: api/response\n Content-Length: 13\n \n -ERR no reply" ;
285
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
224
286
225
- foreach ( char c in rawInput )
287
+ foreach ( byte b in byteInput )
226
288
{
227
- parser . Append ( c ) ;
289
+ parser . Append ( b ) ;
228
290
}
229
291
230
292
Assert . True ( parser . Completed ) ;
@@ -242,10 +304,11 @@ public void it_should_trim_new_lines_from__the_end_of_ApiResponse_Body_text()
242
304
{
243
305
var parser = new Parser ( ) ;
244
306
var rawInput = "Content-Type: api/response\n Content-Length: 14\n \n -ERR no reply\n " ;
307
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( rawInput ) ;
245
308
246
- foreach ( char c in rawInput )
309
+ foreach ( byte b in byteInput )
247
310
{
248
- parser . Append ( c ) ;
311
+ parser . Append ( b ) ;
249
312
}
250
313
251
314
Assert . True ( parser . Completed ) ;
@@ -269,9 +332,10 @@ public void Can_parse_example_sessions_to_completion(string input)
269
332
}
270
333
271
334
bool gotDisconnectNotice = false ;
335
+ byte [ ] byteInput = Encoding . UTF8 . GetBytes ( input ) ;
272
336
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 )
275
339
. Select ( parser => parser . ExtractMessage ( ) )
276
340
. Subscribe (
277
341
m =>
@@ -297,8 +361,9 @@ public void Can_parse_disconnect_notice()
297
361
Disconnected, goodbye.
298
362
See you at ClueCon! http://www.cluecon.com/
299
363
" ;
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 )
302
367
. Select ( parser => parser . ExtractMessage ( ) )
303
368
. Subscribe (
304
369
Console . WriteLine ) ;
0 commit comments