You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

63 lines
2.1 KiB

  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. services.AddOptions();
  16. services.AddSingleton<IServiceCollection>(_ => services);
  17. services.AddSingleton<ISerializer, JsonUtf8Serializer>();
  18. _provider = services.BuildServiceProvider();
  19. }
  20. [Fact]
  21. public void Serialize_then_Deserialize_Message_With_Utf8JsonSerializer()
  22. {
  23. // Given
  24. var givenMessage = new Message(
  25. headers: new Dictionary<string, string>() {
  26. { "cap-msg-name", "authentication.users.update"},
  27. { "cap-msg-type", "User" },
  28. { "cap-corr-seq", "0"},
  29. { "cap-msg-group","service.v1"}
  30. },
  31. value: new MessageValue("test@test.com", "User"));
  32. // When
  33. var serializer = _provider.GetRequiredService<ISerializer>();
  34. var json = serializer.Serialize(givenMessage);
  35. var deserializedMessage = serializer.Deserialize(json);
  36. // Then
  37. Assert.True(serializer.IsJsonType(deserializedMessage.Value));
  38. var result = serializer.Deserialize(deserializedMessage.Value, typeof(MessageValue)) as MessageValue;
  39. Assert.NotNull(result);
  40. Assert.Equal(result.Email, ((MessageValue)givenMessage.Value).Email);
  41. Assert.Equal(result.Name, ((MessageValue)givenMessage.Value).Name);
  42. }
  43. }
  44. public class MessageValue
  45. {
  46. public MessageValue(string email, string name)
  47. {
  48. Email = email;
  49. Name = name;
  50. }
  51. public string Email { get; }
  52. public string Name { get; }
  53. }
  54. }