Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MessageTest.cs 2.0 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using DotNetCore.CAP.Messages;
  4. using DotNetCore.CAP.Serialization;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class MessageTest
  10. {
  11. private readonly IServiceProvider _provider;
  12. public MessageTest()
  13. {
  14. var services = new ServiceCollection();
  15. ServiceCollectionExtensions.ServiceCollection = services;
  16. services.AddSingleton<ISerializer, JsonUtf8Serializer>();
  17. _provider = services.BuildServiceProvider();
  18. }
  19. [Fact]
  20. public void Serialize_then_Deserialize_Message_With_Utf8JsonSerializer()
  21. {
  22. // Given
  23. var givenMessage = new Message(
  24. headers: new Dictionary<string, string>() {
  25. { "cap-msg-name", "authentication.users.update"},
  26. { "cap-msg-type", "User" },
  27. { "cap-corr-seq", "0"},
  28. { "cap-msg-group","service.v1"}
  29. },
  30. value: new MessageValue("test@test.com", "User"));
  31. // When
  32. var serializer = _provider.GetRequiredService<ISerializer>();
  33. var json = serializer.Serialize(givenMessage);
  34. var deserializedMessage = serializer.Deserialize(json);
  35. // Then
  36. Assert.True(serializer.IsJsonType(deserializedMessage.Value));
  37. var result = serializer.Deserialize(deserializedMessage.Value, typeof(MessageValue)) as MessageValue;
  38. Assert.NotNull(result);
  39. Assert.Equal(result.Email, ((MessageValue)givenMessage.Value).Email);
  40. Assert.Equal(result.Name, ((MessageValue)givenMessage.Value).Name);
  41. }
  42. }
  43. public class MessageValue
  44. {
  45. public MessageValue(string email, string name)
  46. {
  47. Email = email;
  48. Name = name;
  49. }
  50. public string Email { get; }
  51. public string Name { get; }
  52. }
  53. }