forked from reactiveui/refit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlContentSerializerTests.cs
131 lines (108 loc) · 4.53 KB
/
XmlContentSerializerTests.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
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Xunit;
namespace Refit.Tests
{
public class XmlContentSerializerTests
{
public class Dto
{
public DateTime CreatedOn { get; set; }
public string Identifier { get; set; }
[XmlElement(Namespace = "https://google.com")]
public string Name { get; set; }
}
[Fact]
public void MediaTypeShouldBeApplicationXmlAsync()
{
var dto = BuildDto();
var sut = new XmlContentSerializer();
var content = sut.ToHttpContent(dto);
Assert.Equal("application/xml", content.Headers.ContentType.MediaType);
}
[Fact]
public async Task ShouldSerializeToXml()
{
var dto = BuildDto();
var sut = new XmlContentSerializer();
var content = sut.ToHttpContent(dto);
var document = new XmlDocument();
document.LoadXml(await content.ReadAsStringAsync());
var root = document[nameof(Dto)] ?? throw new NullReferenceException("Root element was not found");
Assert.Equal(dto.CreatedOn, XmlConvert.ToDateTime(root[nameof(Dto.CreatedOn)].InnerText, XmlDateTimeSerializationMode.Utc));
Assert.Equal(dto.Identifier, root[nameof(Dto.Identifier)].InnerText);
Assert.Equal(dto.Name, root[nameof(Dto.Name)].InnerText);
}
[Fact]
public async Task ShouldSerializeToXmlUsingAttributeOverrides()
{
const string overridenRootElementName = "dto-ex";
var dto = BuildDto();
var serializerSettings = new XmlContentSerializerSettings();
var attributes = new XmlAttributes { XmlRoot = new XmlRootAttribute(overridenRootElementName) };
serializerSettings.XmlAttributeOverrides.Add(dto.GetType(), attributes);
var sut = new XmlContentSerializer(serializerSettings);
var content = sut.ToHttpContent(dto);
var document = new XmlDocument();
document.LoadXml(await content.ReadAsStringAsync());
Assert.Equal(overridenRootElementName, document.DocumentElement?.Name);
}
[Fact]
public async Task ShouldSerializeToXmlUsingNamespaceOverrides()
{
const string prefix = "google";
var dto = BuildDto();
var serializerSettings = new XmlContentSerializerSettings { XmlNamespaces = new XmlSerializerNamespaces() };
serializerSettings.XmlNamespaces.Add(prefix, "https://google.com");
var sut = new XmlContentSerializer(serializerSettings);
var content = sut.ToHttpContent(dto);
var document = new XmlDocument();
document.LoadXml(await content.ReadAsStringAsync());
Assert.Equal(prefix, document["Dto"]?["Name", "https://google.com"]?.Prefix);
}
[Fact]
public async Task ShouldDeserializeFromXmlAsync()
{
var serializerSettings = new XmlContentSerializerSettings { XmlNamespaces = new XmlSerializerNamespaces() };
var sut = new XmlContentSerializer(serializerSettings);
var dto = await sut.FromHttpContentAsync<Dto>(new StringContent("<Dto><Identifier>123</Identifier></Dto>"));
Assert.Equal("123", dto.Identifier);
}
[Fact]
public async Task XmlEncodingShouldMatchWriterSettingAsync()
{
var encoding = Encoding.UTF32;
var serializerSettings = new XmlContentSerializerSettings
{
XmlReaderWriterSettings = new XmlReaderWriterSettings()
{
WriterSettings = new XmlWriterSettings()
{
Encoding = encoding
}
}
};
var sut = new XmlContentSerializer(serializerSettings);
var dto = BuildDto();
var content = sut.ToHttpContent(dto);
var xml = XDocument.Parse(await content.ReadAsStringAsync());
var documentEncoding = xml.Declaration.Encoding;
Assert.Equal(encoding.WebName, documentEncoding);
}
static Dto BuildDto()
{
var dto = new Dto
{
CreatedOn = DateTime.UtcNow,
Identifier = Guid.NewGuid().ToString(),
Name = "Test Dto Object"
};
return dto;
}
}
}