forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceTests.cs
More file actions
248 lines (204 loc) · 7.96 KB
/
InterfaceTests.cs
File metadata and controls
248 lines (204 loc) · 7.96 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using ServiceStack.Messaging;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.Text.Tests.JsonTests;
namespace ServiceStack.Text.Tests
{
[TestFixture]
public class InterfaceTests : TestBase
{
[Test]
public void Can_serialize_Message()
{
var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
var messageString = TypeSerializer.SerializeToString(message);
Assert.That(messageString, Is.EqualTo(
"{Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
Serialize(message);
}
[Test]
public void Can_serialize_IMessage()
{
var message = new Message<string> { Id = new Guid(), CreatedDate = new DateTime(), Body = "test" };
var messageString = TypeSerializer.SerializeToString((IMessage<string>)message);
Assert.That(messageString, Is.EqualTo(
"{__type:\"ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces\","
+ "Id:00000000000000000000000000000000,CreatedDate:0001-01-01,Priority:0,RetryAttempts:0,Body:test}"));
}
public class DtoWithObject
{
public object Results { get; set; }
}
[Test]
public void Can_deserialize_dto_with_object()
{
var dto = Serialize(new DtoWithObject { Results = new Message<string>("Body") }, includeXml: false);
Assert.That(dto.Results, Is.Not.Null);
Assert.That(dto.Results.GetType(), Is.EqualTo(typeof(Message<string>)));
}
[Test]
public void Can_serialize_ToString()
{
var type = Type.GetType(typeof(Message<string>).AssemblyQualifiedName);
Assert.That(type, Is.Not.Null);
type = AssemblyUtils.FindType(typeof(Message<string>).AssemblyQualifiedName);
Assert.That(type, Is.Not.Null);
type = Type.GetType("ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
Assert.That(type, Is.Not.Null);
}
[Test, TestCaseSource(typeof(InterfaceTests), "EndpointExpectations")]
public void Does_serialize_minimum_type_info_whilst_still_working(
Type type, string expectedTypeString)
{
Assert.That(type.ToTypeString(), Is.EqualTo(expectedTypeString));
var newType = AssemblyUtils.FindType(type.ToTypeString());
Assert.That(newType, Is.Not.Null);
Assert.That(newType, Is.EqualTo(type));
}
public static IEnumerable EndpointExpectations
{
get
{
yield return new TestCaseData(typeof(Message<string>),
"ServiceStack.Messaging.Message`1[[System.String, mscorlib]], ServiceStack.Interfaces");
yield return new TestCaseData(typeof(Cat),
"ServiceStack.Text.Tests.JsonTests.Cat, ServiceStack.Text.Tests");
yield return new TestCaseData(typeof(Zoo),
"ServiceStack.Text.Tests.JsonTests.Zoo, ServiceStack.Text.Tests");
}
}
[Test]
public void Can_deserialize_interface_into_concrete_type()
{
var dto = Serialize(new MessagingTests.DtoWithInterface { Results = new Message<string>("Body") }, includeXml: false);
Assert.That(dto.Results, Is.Not.Null);
}
public class UserSession
{
public UserSession()
{
this.ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>();
}
public string ReferrerUrl { get; set; }
public string Id { get; set; }
public string TwitterUserId { get; set; }
public string TwitterScreenName { get; set; }
public string RequestTokenSecret { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime LastModified { get; set; }
public Dictionary<string, IOAuthTokens> ProviderOAuthAccess { get; set; }
}
[Test]
public void Can_Serialize_User_OAuthSession_map()
{
var userSession = new UserSession {
Id = "1",
CreatedAt = DateTime.UtcNow,
LastModified = DateTime.UtcNow,
ReferrerUrl = "http://referrer.com",
ProviderOAuthAccess = new Dictionary<string, IOAuthTokens>
{
{"twitter", new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
{"facebook", new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }} },
}
};
var fromDto = Serialize(userSession, includeXml: false);
Console.WriteLine(fromDto.Dump());
Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
Assert.That(fromDto.ProviderOAuthAccess["twitter"].Provider, Is.EqualTo("twitter"));
Assert.That(fromDto.ProviderOAuthAccess["facebook"].Provider, Is.EqualTo("facebook"));
Assert.That(fromDto.ProviderOAuthAccess["twitter"].Items.Count, Is.EqualTo(2));
Assert.That(fromDto.ProviderOAuthAccess["facebook"].Items.Count, Is.EqualTo(2));
}
[Test]
public void Can_Serialize_User_OAuthSession_list()
{
var userSession = new OAuthUserSession {
Id = "1",
CreatedAt = DateTime.UtcNow,
LastModified = DateTime.UtcNow,
ReferrerUrl = "http://referrer.com",
ProviderOAuthAccess = new List<IOAuthTokens>
{
new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
}
};
var fromDto = Serialize(userSession, includeXml: false);
Console.WriteLine(fromDto.Dump());
Assert.That(fromDto.ProviderOAuthAccess.Count, Is.EqualTo(2));
Assert.That(fromDto.ProviderOAuthAccess[0].Provider, Is.EqualTo("twitter"));
Assert.That(fromDto.ProviderOAuthAccess[1].Provider, Is.EqualTo("facebook"));
Assert.That(fromDto.ProviderOAuthAccess[0].Items.Count, Is.EqualTo(2));
Assert.That(fromDto.ProviderOAuthAccess[1].Items.Count, Is.EqualTo(2));
}
[Test]
public void Doesnt_serialize_TypeInfo_when_set()
{
JsConfig.ExcludeTypeInfo = true;
var userSession = new OAuthUserSession {
Id = "1",
CreatedAt = DateTime.UtcNow,
LastModified = DateTime.UtcNow,
ReferrerUrl = "http://referrer.com",
ProviderOAuthAccess = new List<IOAuthTokens>
{
new OAuthTokens { Provider = "twitter", AccessToken = "TAccessToken", Items = { {"a","1"}, {"b","2"}, }},
new OAuthTokens { Provider = "facebook", AccessToken = "FAccessToken", Items = { {"a","1"}, {"b","2"}, }},
}
};
Assert.That(userSession.ToJson().IndexOf("__type") == -1, Is.True);
Assert.That(userSession.ToJsv().IndexOf("__type") == -1, Is.True);
}
public class AggregateEvents
{
public Guid Id { get; set; }
public List<DomainEvent> Events { get; set; }
}
public abstract class DomainEvent { }
public class UserRegisteredEvent : DomainEvent
{
public Guid UserId { get; set; }
public string Name { get; set; }
}
public class UserPromotedEvent : DomainEvent
{
public Guid UserId { get; set; }
public string NewRole { get; set; }
}
[Test]
public void Can_deserialize_DomainEvent_into_Concrete_Type()
{
var userId = Guid.NewGuid();
var dto = (DomainEvent)new UserPromotedEvent { UserId = userId };
var json = dto.ToJson();
var userPromoEvent = (UserPromotedEvent)json.FromJson<DomainEvent>();
Assert.That(userPromoEvent.UserId, Is.EqualTo(userId));
}
public class Habitat<T> where T : Animal
{
public string Continent { get; set; }
public object Species { get; set; }
}
public class Animal
{
public string Name { get; set; }
}
public class CatAnimal : Animal
{
public string Color { get; set; }
}
[Test]
public void Can_serialize_dependent_type_properties()
{
var jungle = new Habitat<Animal> {
Continent = "South America",
Species = new CatAnimal { Name = "Tiger", Color = "Orange" }
};
Console.WriteLine(jungle.ToJson());
}
}
}