-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathMessagePackFormatterTests.cs
470 lines (411 loc) · 19 KB
/
MessagePackFormatterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System.Diagnostics;
using System.Runtime.Serialization;
using MessagePack;
using MessagePack.Formatters;
using MessagePack.Resolvers;
using Microsoft.VisualStudio.Threading;
using Nerdbank.Streams;
using StreamJsonRpc;
using StreamJsonRpc.Protocol;
using Xunit;
using Xunit.Abstractions;
public class MessagePackFormatterTests : FormatterTestBase<MessagePackFormatter>
{
public MessagePackFormatterTests(ITestOutputHelper logger)
: base(logger)
{
}
[Fact]
public void JsonRpcRequest_PositionalArgs()
{
var original = new JsonRpcRequest
{
RequestId = new RequestId(5),
Method = "test",
ArgumentsList = new object[] { 5, "hi", new CustomType { Age = 8 } },
};
var actual = this.Roundtrip(original);
Assert.Equal(original.RequestId, actual.RequestId);
Assert.Equal(original.Method, actual.Method);
Assert.True(actual.TryGetArgumentByNameOrIndex(null, 0, typeof(int), out object? actualArg0));
Assert.Equal(original.ArgumentsList[0], actualArg0);
Assert.True(actual.TryGetArgumentByNameOrIndex(null, 1, typeof(string), out object? actualArg1));
Assert.Equal(original.ArgumentsList[1], actualArg1);
Assert.True(actual.TryGetArgumentByNameOrIndex(null, 2, typeof(CustomType), out object? actualArg2));
Assert.Equal(((CustomType?)original.ArgumentsList[2])!.Age, ((CustomType)actualArg2!).Age);
}
[Fact]
public void JsonRpcRequest_NamedArgs()
{
var original = new JsonRpcRequest
{
RequestId = new RequestId(5),
Method = "test",
NamedArguments = new Dictionary<string, object?>
{
{ "Number", 5 },
{ "Message", "hi" },
{ "Custom", new CustomType { Age = 8 } },
},
};
var actual = this.Roundtrip(original);
Assert.Equal(original.RequestId, actual.RequestId);
Assert.Equal(original.Method, actual.Method);
Assert.True(actual.TryGetArgumentByNameOrIndex("Number", -1, typeof(int), out object? actualArg0));
Assert.Equal(original.NamedArguments["Number"], actualArg0);
Assert.True(actual.TryGetArgumentByNameOrIndex("Message", -1, typeof(string), out object? actualArg1));
Assert.Equal(original.NamedArguments["Message"], actualArg1);
Assert.True(actual.TryGetArgumentByNameOrIndex("Custom", -1, typeof(CustomType), out object? actualArg2));
Assert.Equal(((CustomType?)original.NamedArguments["Custom"])!.Age, ((CustomType)actualArg2!).Age);
}
[Fact]
public void JsonRpcResult()
{
var original = new JsonRpcResult
{
RequestId = new RequestId(5),
Result = new CustomType { Age = 7 },
};
var actual = this.Roundtrip(original);
Assert.Equal(original.RequestId, actual.RequestId);
Assert.Equal(((CustomType?)original.Result)!.Age, actual.GetResult<CustomType>().Age);
}
[Fact]
public void JsonRpcError()
{
var original = new JsonRpcError
{
RequestId = new RequestId(5),
Error = new JsonRpcError.ErrorDetail
{
Code = JsonRpcErrorCode.InvocationError,
Message = "Oops",
Data = new CustomType { Age = 15 },
},
};
var actual = this.Roundtrip(original);
Assert.Equal(original.RequestId, actual.RequestId);
Assert.Equal(original.Error.Code, actual.Error!.Code);
Assert.Equal(original.Error.Message, actual.Error.Message);
Assert.Equal(((CustomType)original.Error.Data).Age, actual.Error.GetData<CustomType>().Age);
}
[Fact]
public async Task BasicJsonRpc()
{
var (clientStream, serverStream) = FullDuplexStream.CreatePair();
var clientFormatter = new MessagePackFormatter();
var serverFormatter = new MessagePackFormatter();
var clientHandler = new LengthHeaderMessageHandler(clientStream.UsePipe(), clientFormatter);
var serverHandler = new LengthHeaderMessageHandler(serverStream.UsePipe(), serverFormatter);
var clientRpc = new JsonRpc(clientHandler);
var serverRpc = new JsonRpc(serverHandler, new Server());
serverRpc.TraceSource = new TraceSource("Server", SourceLevels.Verbose);
clientRpc.TraceSource = new TraceSource("Client", SourceLevels.Verbose);
serverRpc.TraceSource.Listeners.Add(new XunitTraceListener(this.Logger));
clientRpc.TraceSource.Listeners.Add(new XunitTraceListener(this.Logger));
clientRpc.StartListening();
serverRpc.StartListening();
int result = await clientRpc.InvokeAsync<int>(nameof(Server.Add), 3, 5).WithCancellation(this.TimeoutToken);
Assert.Equal(8, result);
}
[Fact]
public void Resolver_RequestArgInArray()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new CustomFormatter())));
var originalArg = new TypeRequiringCustomFormatter { Prop1 = 3, Prop2 = 5 };
var originalRequest = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
ArgumentsList = new object[] { originalArg },
};
var roundtripRequest = this.Roundtrip(originalRequest);
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(null, 0, typeof(TypeRequiringCustomFormatter), out object? roundtripArgObj));
var roundtripArg = (TypeRequiringCustomFormatter)roundtripArgObj!;
Assert.Equal(originalArg.Prop1, roundtripArg.Prop1);
Assert.Equal(originalArg.Prop2, roundtripArg.Prop2);
}
[Fact]
public void Resolver_RequestArgInNamedArgs_AnonymousType()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new IMessagePackFormatter[] { new CustomFormatter() }, new IFormatterResolver[] { BuiltinResolver.Instance })));
var originalArg = new { Prop1 = 3, Prop2 = 5 };
var originalRequest = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
Arguments = originalArg,
};
var roundtripRequest = this.Roundtrip(originalRequest);
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.Prop1), -1, typeof(int), out object? prop1));
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.Prop2), -1, typeof(int), out object? prop2));
Assert.Equal(originalArg.Prop1, prop1);
Assert.Equal(originalArg.Prop2, prop2);
}
[Fact]
public void Resolver_RequestArgInNamedArgs_DataContractObject()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new IMessagePackFormatter[] { new CustomFormatter() }, new IFormatterResolver[] { BuiltinResolver.Instance })));
var originalArg = new DataContractWithSubsetOfMembersIncluded { ExcludedField = "A", ExcludedProperty = "B", IncludedField = "C", IncludedProperty = "D" };
var originalRequest = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
Arguments = originalArg,
};
var roundtripRequest = this.Roundtrip(originalRequest);
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.ExcludedField), -1, typeof(string), out object? _));
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.ExcludedProperty), -1, typeof(string), out object? _));
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.IncludedField), -1, typeof(string), out object? includedField));
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.IncludedProperty), -1, typeof(string), out object? includedProperty));
Assert.Equal(originalArg.IncludedProperty, includedProperty);
Assert.Equal(originalArg.IncludedField, includedField);
}
[Fact]
public void Resolver_RequestArgInNamedArgs_NonDataContractObject()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new IMessagePackFormatter[] { new CustomFormatter() }, new IFormatterResolver[] { BuiltinResolver.Instance })));
var originalArg = new NonDataContractWithExcludedMembers { ExcludedField = "A", ExcludedProperty = "B", InternalField = "C", InternalProperty = "D", PublicField = "E", PublicProperty = "F" };
var originalRequest = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
Arguments = originalArg,
};
var roundtripRequest = this.Roundtrip(originalRequest);
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.ExcludedField), -1, typeof(string), out object? _));
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.ExcludedProperty), -1, typeof(string), out object? _));
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.InternalField), -1, typeof(string), out object? _));
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.InternalProperty), -1, typeof(string), out object? _));
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.PublicField), -1, typeof(string), out object? publicField));
Assert.True(roundtripRequest.TryGetArgumentByNameOrIndex(nameof(originalArg.PublicProperty), -1, typeof(string), out object? publicProperty));
Assert.Equal(originalArg.PublicProperty, publicProperty);
Assert.Equal(originalArg.PublicField, publicField);
}
[Fact]
public void Resolver_RequestArgInNamedArgs_NullObject()
{
var originalRequest = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
Arguments = null,
};
var roundtripRequest = this.Roundtrip(originalRequest);
Assert.Null(roundtripRequest.Arguments);
Assert.False(roundtripRequest.TryGetArgumentByNameOrIndex("AnythingReally", -1, typeof(string), out object? _));
}
[Fact]
public void Resolver_Result()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new CustomFormatter())));
var originalResultValue = new TypeRequiringCustomFormatter { Prop1 = 3, Prop2 = 5 };
var originalResult = new JsonRpcResult
{
RequestId = new RequestId(1),
Result = originalResultValue,
};
var roundtripResult = this.Roundtrip(originalResult);
var roundtripResultValue = roundtripResult.GetResult<TypeRequiringCustomFormatter>();
Assert.Equal(originalResultValue.Prop1, roundtripResultValue.Prop1);
Assert.Equal(originalResultValue.Prop2, roundtripResultValue.Prop2);
}
[Fact]
public void Resolver_ErrorData()
{
this.Formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(CompositeResolver.Create(new CustomFormatter())));
var originalErrorData = new TypeRequiringCustomFormatter { Prop1 = 3, Prop2 = 5 };
var originalError = new JsonRpcError
{
RequestId = new RequestId(1),
Error = new JsonRpcError.ErrorDetail
{
Data = originalErrorData,
},
};
var roundtripError = this.Roundtrip(originalError);
var roundtripErrorData = roundtripError.Error!.GetData<TypeRequiringCustomFormatter>();
Assert.Equal(originalErrorData.Prop1, roundtripErrorData.Prop1);
Assert.Equal(originalErrorData.Prop2, roundtripErrorData.Prop2);
}
/// <summary>
/// Verifies that the <see cref="MessagePackSerializerOptions"/> passed to an <see cref="IMessagePackFormatter{T}"/>
/// during serialization of user data is or derives from the value supplied to
/// <see cref="MessagePackFormatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions)"/>.
/// </summary>
/// <remarks>
/// This is important because some users actually pass extra state to their formatters by way of a derivation of the options class.
/// Modifying their options is fine so long as it is done using the <see cref="MessagePackSerializerOptions.Clone"/> method
/// so that the instance is still their type with any custom properties copied.
/// </remarks>
[Fact]
public void ActualOptions_IsOrDerivesFrom_SetMessagePackSerializerOptions()
{
var customFormatter = new CustomFormatter();
var options = (CustomOptions)new CustomOptions(MessagePackFormatter.DefaultUserDataSerializationOptions) { CustomProperty = 3 }
.WithResolver(CompositeResolver.Create(customFormatter));
this.Formatter.SetMessagePackSerializerOptions(options);
var value = new JsonRpcRequest
{
RequestId = new RequestId(1),
Method = "Eat",
ArgumentsList = new object[] { new TypeRequiringCustomFormatter() },
};
var sequence = new Sequence<byte>();
this.Formatter.Serialize(sequence, value);
var observedOptions = Assert.IsType<CustomOptions>(customFormatter.LastObservedOptions);
Assert.Equal(options.CustomProperty, observedOptions.CustomProperty);
}
[Fact]
public void CanDeserializeWithExtraProperty_JsonRpcRequest()
{
var dynamic = new
{
jsonrpc = "2.0",
method = "something",
extra = (object?)null,
@params = new object[] { "hi" },
};
var request = this.Read<JsonRpcRequest>(dynamic);
Assert.Equal(dynamic.jsonrpc, request.Version);
Assert.Equal(dynamic.method, request.Method);
Assert.Equal([email protected], request.ArgumentCount);
Assert.True(request.TryGetArgumentByNameOrIndex(null, 0, typeof(string), out object? arg));
Assert.Equal(dynamic.@params[0], arg);
}
[Fact]
public void CanDeserializeWithExtraProperty_JsonRpcResult()
{
var dynamic = new
{
jsonrpc = "2.0",
id = 2,
extra = (object?)null,
result = "hi",
};
var request = this.Read<JsonRpcResult>(dynamic);
Assert.Equal(dynamic.jsonrpc, request.Version);
Assert.Equal(dynamic.id, request.RequestId.Number);
Assert.Equal(dynamic.result, request.GetResult<string>());
}
[Fact]
public void CanDeserializeWithExtraProperty_JsonRpcError()
{
var dynamic = new
{
jsonrpc = "2.0",
id = 2,
extra = (object?)null,
error = new { extra = 2, code = 5 },
};
var request = this.Read<JsonRpcError>(dynamic);
Assert.Equal(dynamic.jsonrpc, request.Version);
Assert.Equal(dynamic.id, request.RequestId.Number);
Assert.Equal(dynamic.error.code, (int?)request.Error?.Code);
}
[Fact]
public void StringsInUserDataAreInterned()
{
var dynamic = new
{
jsonrpc = "2.0",
method = "something",
extra = (object?)null,
@params = new object[] { "hi" },
};
var request1 = this.Read<JsonRpcRequest>(dynamic);
var request2 = this.Read<JsonRpcRequest>(dynamic);
Assert.True(request1.TryGetArgumentByNameOrIndex(null, 0, typeof(string), out object? arg1));
Assert.True(request2.TryGetArgumentByNameOrIndex(null, 0, typeof(string), out object? arg2));
Assert.Same(arg2, arg1); // reference equality to ensure it was interned.
}
[Fact]
public void StringValuesOfStandardPropertiesAreInterned()
{
var dynamic = new
{
jsonrpc = "2.0",
method = "something",
extra = (object?)null,
@params = Array.Empty<object?>(),
};
var request1 = this.Read<JsonRpcRequest>(dynamic);
var request2 = this.Read<JsonRpcRequest>(dynamic);
Assert.Same(request1.Method, request2.Method); // reference equality to ensure it was interned.
}
protected override MessagePackFormatter CreateFormatter() => new();
private T Read<T>(object anonymousObject)
where T : JsonRpcMessage
{
var sequence = new Sequence<byte>();
var writer = new MessagePackWriter(sequence);
MessagePackSerializer.Serialize(ref writer, anonymousObject, MessagePackSerializerOptions.Standard);
writer.Flush();
return (T)this.Formatter.Deserialize(sequence);
}
[DataContract]
private class DataContractWithSubsetOfMembersIncluded
{
public string? ExcludedField;
[DataMember]
internal string? IncludedField;
public string? ExcludedProperty { get; set; }
[DataMember]
internal string? IncludedProperty { get; set; }
}
private class NonDataContractWithExcludedMembers
{
[IgnoreDataMember]
public string? ExcludedField;
public string? PublicField;
internal string? InternalField;
[IgnoreDataMember]
public string? ExcludedProperty { get; set; }
public string? PublicProperty { get; set; }
internal string? InternalProperty { get; set; }
}
private class TypeRequiringCustomFormatter
{
internal int Prop1 { get; set; }
internal int Prop2 { get; set; }
}
private class CustomFormatter : IMessagePackFormatter<TypeRequiringCustomFormatter>
{
internal MessagePackSerializerOptions? LastObservedOptions { get; private set; }
public TypeRequiringCustomFormatter Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
this.LastObservedOptions = options;
Assert.Equal(2, reader.ReadArrayHeader());
return new TypeRequiringCustomFormatter
{
Prop1 = reader.ReadInt32(),
Prop2 = reader.ReadInt32(),
};
}
public void Serialize(ref MessagePackWriter writer, TypeRequiringCustomFormatter value, MessagePackSerializerOptions options)
{
this.LastObservedOptions = options;
writer.WriteArrayHeader(2);
writer.Write(value.Prop1);
writer.Write(value.Prop2);
}
}
private class Server
{
public int Add(int a, int b) => a + b;
}
private class CustomOptions : MessagePackSerializerOptions
{
internal CustomOptions(CustomOptions copyFrom)
: base(copyFrom)
{
this.CustomProperty = copyFrom.CustomProperty;
}
internal CustomOptions(MessagePackSerializerOptions copyFrom)
: base(copyFrom)
{
}
internal int CustomProperty { get; set; }
protected override MessagePackSerializerOptions Clone() => new CustomOptions(this);
}
}