forked from ServiceStack/ServiceStack
/
ExceptionHandlingTests.cs
132 lines (116 loc) · 3.47 KB
/
ExceptionHandlingTests.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.ServiceInterface;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using NUnit.Framework;
using Funq;
using ServiceStack.Service;
using ServiceStack.ServiceClient.Web;
using System.Collections;
using ServiceStack.WebHost.Endpoints.Support;
using ServiceStack.WebHost.Endpoints.Tests.Support;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[RestService("/users")]
public class User { }
public class UserResponse : IHasResponseStatus
{
public ResponseStatus ResponseStatus { get; set; }
}
public class UserService : RestServiceBase<User>
{
public override object OnGet(User request)
{
return new HttpError(System.Net.HttpStatusCode.BadRequest, "CanNotExecute", "Failed to execute!");
}
public override object OnPost(User request)
{
throw new HttpError(System.Net.HttpStatusCode.BadRequest, "CanNotExecute", "Failed to execute!");
}
public override object OnPut(User request)
{
throw new ArgumentException();
}
}
[TestFixture]
public class ExceptionHandlingTests
{
private const string ListeningOn = "http://localhost:82/";
public class ExceptionHandlingAppHostHttpListener
: AppHostHttpListenerBase
{
public ExceptionHandlingAppHostHttpListener()
: base("Exception handling tests", typeof(UserService).Assembly) { }
public override void Configure(Container container)
{
}
}
ExceptionHandlingAppHostHttpListener appHost;
[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
appHost = new ExceptionHandlingAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}
static IRestClient[] ServiceClients =
{
new JsonServiceClient(ListeningOn),
new XmlServiceClient(ListeningOn),
new JsvServiceClient(ListeningOn)
//SOAP not supported in HttpListener
//new Soap11ServiceClient(ServiceClientBaseUri),
//new Soap12ServiceClient(ServiceClientBaseUri)
};
[Test, TestCaseSource("ServiceClients")]
public void Handles_Returned_Http_Error(IRestClient client)
{
try
{
client.Get<UserResponse>("/users");
}
catch (WebServiceException ex)
{
Assert.That(ex.ErrorCode, Is.EqualTo("CanNotExecute"));
Assert.That(ex.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.BadRequest));
Assert.That(ex.Message, Is.EqualTo("Failed to execute!"));
}
}
[Test, TestCaseSource("ServiceClients")]
public void Handles_Thrown_Http_Error(IRestClient client)
{
try
{
client.Post<UserResponse>("/users", new User());
}
catch (WebServiceException ex)
{
Assert.That(ex.ErrorCode, Is.EqualTo("CanNotExecute"));
Assert.That(ex.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.BadRequest));
Assert.That(ex.Message, Is.EqualTo("Failed to execute!"));
}
}
[Test, TestCaseSource("ServiceClients")]
public void Handles_Normal_Exception(IRestClient client)
{
try
{
client.Put<UserResponse>("/users", new User());
}
catch (WebServiceException ex)
{
Assert.That(ex.ErrorCode, Is.EqualTo("ArgumentException"));
Assert.That(ex.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.BadRequest));
}
}
}
}