Browse files

Fix validation tests

  • Loading branch information...
1 parent 77287d8 commit 3a63e772184461001616e410357959ff619bd433 @mythz mythz committed Sep 22, 2012
View
15 src/ServiceStack.ServiceInterface/ServiceBase.cs
@@ -49,6 +49,13 @@ public IAppHost GetAppHost()
return appHost ?? EndpointHost.AppHost;
}
+ private HandleServiceExceptionDelegate serviceExceptionHandler;
+ public HandleServiceExceptionDelegate ServiceExceptionHandler
+ {
+ get { return serviceExceptionHandler ?? appHost.ServiceExceptionHandler; }
+ set { serviceExceptionHandler = value; }
+ }
+
public ServiceBase<TRequest> SetAppHost(IAppHost appHost) //Allow chaining
{
this.appHost = appHost;
@@ -234,11 +241,9 @@ public object Execute(TRequest request)
/// <returns></returns>
protected virtual object HandleException(TRequest request, Exception ex)
{
- var useAppHost = GetAppHost();
-
- var errorResponse = useAppHost != null && useAppHost.ServiceExceptionHandler != null
- ? useAppHost.ServiceExceptionHandler(request, ex)
- : DtoUtils.HandleException(useAppHost, request, ex);
+ var errorResponse = ServiceExceptionHandler != null
+ ? ServiceExceptionHandler(request, ex)
+ : DtoUtils.HandleException(GetAppHost(), request, ex);
AfterEachRequest(request, errorResponse ?? ex);
View
1 src/ServiceStack.ServiceInterface/ServiceStack.ServiceInterface.csproj
@@ -220,6 +220,7 @@
<Compile Include="SessionExtensions.cs" />
<Compile Include="SessionFeature.cs" />
<Compile Include="Testing\BasicAppHost.cs" />
+ <Compile Include="Testing\BasicResolver.cs" />
<Compile Include="Testing\MockHttpRequest.cs">
<SubType>Code</SubType>
</Compile>
View
22 src/ServiceStack.ServiceInterface/Testing/BasicResolver.cs
@@ -0,0 +1,22 @@
+using Funq;
+using ServiceStack.ServiceHost;
+
+namespace ServiceStack.ServiceInterface.Testing
+{
+ public class BasicResolver : IResolver
+ {
+ private readonly Container container;
+
+ public BasicResolver() : this(new Container()) {}
+
+ public BasicResolver(Container container)
+ {
+ this.container = container;
+ }
+
+ public T TryResolve<T>()
+ {
+ return this.container.TryResolve<T>();
+ }
+ }
+}
View
4 src/ServiceStack.ServiceInterface/Validation/ValidationFeature.cs
@@ -52,7 +52,7 @@ public object HandleException(object request, Exception ex)
: DtoUtils.HandleException(appHost, request, ex);
}
- public static object HandleException(IAppHost appHost, object request, Exception ex)
+ public static object HandleException(IResolver resolver, object request, Exception ex)
{
var validationException = ex as ValidationException;
if (validationException != null)
@@ -63,7 +63,7 @@ public static object HandleException(IAppHost appHost, object request, Exception
return DtoUtils.CreateErrorResponse(typeof(ValidationException).Name, validationException.Message, errors);
}
- return DtoUtils.HandleException(appHost, request, ex);
+ return DtoUtils.HandleException(resolver, request, ex);
}
/// <summary>
View
6 src/ServiceStack/ServiceHost/DtoUtils.cs
@@ -190,7 +190,7 @@ public static ResponseStatus CreateResponseStatus(string errorCode, string error
return to;
}
- public static object HandleException(IAppHost appHost, object request, Exception ex)
+ public static object HandleException(IResolver iocResolver, object request, Exception ex)
{
if (ex.InnerException != null && !(ex is IHttpError))
ex = ex.InnerException;
@@ -205,8 +205,8 @@ public static object HandleException(IAppHost appHost, object request, Exception
Log.Error("ServiceBase<TRequest>::Service Exception", ex);
- if (appHost != null)
- LogErrorInRedisIfExists(appHost.TryResolve<IRedisClientsManager>(), request.GetType().Name, responseStatus);
+ if (iocResolver != null)
+ LogErrorInRedisIfExists(iocResolver.TryResolve<IRedisClientsManager>(), request.GetType().Name, responseStatus);
var errorResponse = CreateErrorResponse(request, ex, responseStatus);
View
25 tests/ServiceStack.Common.Tests/OAuth/RegistrationServiceTests.cs
@@ -29,18 +29,6 @@ public static IUserAuthRepository GetStubRepo()
return mock.Object;
}
- private static RegistrationService SetValidatorHandler(RegistrationService service)
- {
- var appHost = new BasicAppHost(validation: false);
-
- appHost.ServiceExceptionHandler = (req, ex) =>
- ValidationFeature.HandleException(appHost, req, ex);
-
- service.SetAppHost(appHost);
-
- return service;
- }
-
public static RegistrationService GetRegistrationService(
AbstractValidator<Registration> validator = null,
IUserAuthRepository authRepo=null)
@@ -49,10 +37,12 @@ private static RegistrationService SetValidatorHandler(RegistrationService servi
var service = new RegistrationService {
RegistrationValidator = validator ?? new RegistrationValidator { UserAuthRepo = GetStubRepo() },
UserAuthRepo = authRepo ?? GetStubRepo(),
- RequestContext = requestContext
+ RequestContext = requestContext,
+ ServiceExceptionHandler = (req, ex) =>
+ ValidationFeature.HandleException(new BasicResolver(), req, ex)
};
- return SetValidatorHandler(service);
+ return service;
}
[Test]
@@ -127,9 +117,10 @@ public void Requires_unique_UserName_and_Email()
var service = new RegistrationService {
RegistrationValidator = new RegistrationValidator { UserAuthRepo = mock.Object },
- UserAuthRepo = mock.Object
- };
- SetValidatorHandler(service);
+ UserAuthRepo = mock.Object,
+ ServiceExceptionHandler = (req, ex) =>
+ ValidationFeature.HandleException(new BasicResolver(), req, ex)
+ };
var request = new Registration {
DisplayName = "DisplayName",

0 comments on commit 3a63e77

Please sign in to comment.