-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
WebServicesTests.cs
270 lines (235 loc) · 9.72 KB
/
WebServicesTests.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.Xml;
using NUnit.Framework;
using ServiceStack.Common.Tests;
using ServiceStack.Text;
using ServiceStack.Validation;
using ServiceStack.WebHost.IntegrationTests.Services;
namespace ServiceStack.WebHost.IntegrationTests.Tests
{
/// <summary>
/// This base class executes all Web Services ignorant of the endpoints its hosted on.
/// The same tests below are re-used by the Unit and Integration TestFixture's declared below
/// </summary>
[TestFixture]
public abstract class WebServicesTests
: TestBase
{
private const string TestString = "ServiceStack";
protected WebServicesTests()
: base(Config.ServiceStackBaseUri, typeof(ReverseService).Assembly)
{
}
protected override void Configure(Funq.Container container) { }
[Test]
public void Does_Execute_ReverseService()
{
var client = CreateNewServiceClient();
var response = client.Send<ReverseResponse>(
new Reverse { Value = TestString });
var expectedValue = ReverseService.Execute(TestString);
Assert.That(response.Result, Is.EqualTo(expectedValue));
}
[Test]
public void Does_Execute_Rot13Service()
{
var client = CreateNewServiceClient();
var response = client.Send<Rot13Response>(new Rot13 { Value = TestString });
var expectedValue = TestString.ToRot13();
Assert.That(response.Result, Is.EqualTo(expectedValue));
}
[Test]
public void Does_Execute_AddInts()
{
var client = CreateNewServiceClient();
var response = client.Send<AddIntsResponse>(new AddInts { A = 1, B = 2 });
Assert.That(response.Result, Is.EqualTo(3));
}
[Test]
public void Can_Handle_Exception_from_AlwaysThrowService()
{
var client = CreateNewServiceClient();
try
{
var response = client.Send<AlwaysThrowsResponse>(
new AlwaysThrows { Value = TestString });
Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
var response = (AlwaysThrowsResponse)webEx.ResponseDto;
var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
Assert.That(response.ResponseStatus.ErrorCode,
Is.EqualTo(typeof(NotImplementedException).Name));
Assert.That(response.ResponseStatus.Message,
Is.EqualTo(expectedError));
}
}
[Test]
public void Can_Handle_Exception_from_AlwaysThrowsList_with_GET_route()
{
var client = CreateNewServiceClient();
if (client is WcfServiceClient) return;
try
{
var response = client.Get<List<AlwaysThrows>>("/throwslist/404/{0}".Fmt(TestString));
Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
Assert.That(webEx.StatusCode, Is.EqualTo(404));
var response = (ErrorResponse)webEx.ResponseDto;
var expectedError = AlwaysThrowsService.GetErrorMessage(TestString);
Assert.That(response.ResponseStatus.ErrorCode,
Is.EqualTo(typeof(NotImplementedException).Name));
Assert.That(response.ResponseStatus.Message,
Is.EqualTo(expectedError));
}
}
[Test]
public void Can_Handle_Exception_from_AlwaysThrowsValidation()
{
var client = CreateNewServiceClient();
try
{
var response = client.Send<List<AlwaysThrows>>(
new AlwaysThrowsValidation());
Assert.Fail("Should throw HTTP errors");
}
catch (WebServiceException webEx)
{
var response = (ErrorResponse)webEx.ResponseDto;
var status = response.ResponseStatus;
Assert.That(status.ErrorCode, Is.EqualTo("NotEmpty"));
Assert.That(status.Message, Is.EqualTo("'Value' must not be empty."));
Assert.That(status.Errors[0].ErrorCode, Is.EqualTo("NotEmpty"));
Assert.That(status.Errors[0].FieldName, Is.EqualTo("Value"));
Assert.That(status.Errors[0].Message, Is.EqualTo("'Value' must not be empty."));
}
}
[Test]
public void Request_items_are_preserved_between_filters()
{
var client = CreateNewServiceClient();
if (client is DirectServiceClient) return;
var response = client.Send<RequestItemsResponse>(new RequestItems { });
Assert.That(response.Result, Is.EqualTo("MissionSuccess"));
}
}
/// <summary>
/// Unit tests simulates an in-process ServiceStack host where all services
/// are executed all in-memory so DTO's are not even serialized.
/// </summary>
public class UnitTests : WebServicesTests
{
public UnitTests()
{
AppHost.Container.RegisterValidators(typeof(AlwaysThrowsValidator).Assembly);
AppHost.LoadPlugin(new ValidationFeature());
}
protected override IServiceClient CreateNewServiceClient()
{
return new DirectServiceClient(this, AppHost.ServiceController);
}
}
public class XmlIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new XmlServiceClient(ServiceClientBaseUri);
}
}
public class JsonIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new JsonServiceClient(ServiceClientBaseUri);
}
}
public class JsvIntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new JsvServiceClient(ServiceClientBaseUri);
}
}
public class Soap11IntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new Soap11ServiceClient(ServiceClientBaseUri);
}
}
public class Soap12IntegrationTests : WebServicesTests
{
protected override IServiceClient CreateNewServiceClient()
{
return new Soap12ServiceClient(ServiceClientBaseUri);
// return new Soap12ServiceClient("http://test.servicestack.net");
}
[Test]
public void Call_AddInts()
{
var soap = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"" xmlns:a=""http://www.w3.org/2005/08/addressing"">
<s:Header>
<a:Action s:mustUnderstand=""1"">AddInts</a:Action>
<a:MessageID>urn:uuid:e6be43e0-c120-4ba8-920e-7ecaa1823fd2</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand=""1"">http://localhost:50000/api/Soap12</a:To>
</s:Header>
<s:Body>
<AddInts xmlns=""http://schemas.servicestack.net/types"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
<A>1</A>
<B>2</B>
</AddInts>
</s:Body>
</s:Envelope>";
var responseXml = ServiceClientBaseUri.CombineWith("/soap12")
.PostToUrl(soap, requestFilter:req => req.ContentType = "application/soap+xml; charset=utf-8");
responseXml.Print();
Assert.That(responseXml, Does.Contain("<Result>3</Result>"));
}
[Test]
public void Sending_invalid_request_returns_invalid_response()
{
var soap = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<s:Envelope xmlns:s=""http://www.w3.org/2003/05/soap-envelope"" xmlns:a=""http://www.w3.org/2005/08/addressing"">
<s:Header>
<a:Action s:mustUnderstand=""1"">AddInts</a:Action>
<a:MessageID>urn:uuid:e6be43e0-c120-4ba8-920e-7ecaa1823fd2</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand=""1"">http://localhost:50000/api/Soap12</a:To>
</s:Header>
<s:Body>
<AddInts xmlns=""http://schemas.servicestack.net/types"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
<A>not a</A>
<B>number</B>
</AddInts>
</s:Body>
</s:Envelope>";
var responseXml = ServiceClientBaseUri.CombineWith("/soap12")
.PostToUrl(soap, requestFilter: req => req.ContentType = "application/soap+xml; charset=utf-8");
var doc = new XmlDocument();
doc.LoadXml(responseXml);
var responseMsg = Message.CreateMessage(new XmlNodeReader(doc), int.MaxValue,
MessageVersion.Soap12WSAddressingAugust2004);
using (var reader = responseMsg.GetReaderAtBodyContents())
{
var bodyXml = reader.ReadOuterXml();
var responseType = typeof(AddIntsResponse);
var response = (AddIntsResponse)Serialization.DataContractSerializer.Instance.DeserializeFromString(bodyXml, responseType);
Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(nameof(SerializationException)));
Assert.That(response.ResponseStatus.Message, Does.Contain("Error trying to deserialize requestType:"));
}
}
}
}