This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added failing integration test for validation fault when response nam…
…ing convention doesn't match
- Loading branch information
1 parent
b490319
commit d246402
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/ServiceStack.WebHost.Endpoints.Tests/UserServiceValidationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using ServiceStack.FluentValidation; | ||
using ServiceStack.ServiceHost; | ||
using ServiceStack.ServiceInterface; | ||
using NUnit.Framework; | ||
using ServiceStack.ServiceInterface.Validation; | ||
using System.Collections; | ||
using Funq; | ||
using ServiceStack.ServiceClient.Web; | ||
using ServiceStack.Service; | ||
using ServiceStack.WebHost.Endpoints.Support; | ||
using ServiceStack.WebHost.Endpoints.Tests.Support; | ||
|
||
namespace ServiceStack.WebHost.Endpoints.Tests | ||
{ | ||
[RestService("/users")] | ||
[RestService("/users/{Id}")] | ||
public class User | ||
{ | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
} | ||
|
||
public interface IAddressValidator | ||
{ | ||
bool ValidAddress(string address); | ||
} | ||
|
||
public class UserValidator : AbstractValidator<User> | ||
{ | ||
public IAddressValidator AddressValidator { get; set; } | ||
|
||
public UserValidator() | ||
{ | ||
RuleSet(ApplyTo.Post | ApplyTo.Put, () => | ||
{ | ||
RuleFor(x => x.LastName).NotEmpty().WithErrorCode("ShouldNotBeEmpty"); | ||
RuleFor(x => x.FirstName).NotEmpty().WithMessage("Please specify a first name"); | ||
}); | ||
} | ||
} | ||
|
||
//Not matching the naming convention ([Request DTO Name] + "Response") | ||
public class OperationResponse | ||
{ | ||
public User Result { get; set; } | ||
} | ||
|
||
public class UserService : RestServiceBase<User> | ||
{ | ||
public override object OnGet(User request) | ||
{ | ||
return new OperationResponse { Result = request }; | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class UserServiceValidationTests | ||
{ | ||
private const string ListeningOn = "http://localhost:82/"; | ||
|
||
public class UserAppHostHttpListener | ||
: AppHostHttpListenerBase | ||
{ | ||
|
||
public UserAppHostHttpListener() | ||
: base("Validation Tests", typeof(UserService).Assembly) { } | ||
|
||
public override void Configure(Container container) | ||
{ | ||
ValidationFeature.Init(this); | ||
container.RegisterValidators(typeof(UserValidator).Assembly); | ||
} | ||
} | ||
|
||
UserAppHostHttpListener appHost; | ||
|
||
[TestFixtureSetUp] | ||
public void OnTestFixtureSetUp() | ||
{ | ||
appHost = new UserAppHostHttpListener(); | ||
appHost.Init(); | ||
appHost.Start(ListeningOn); | ||
} | ||
|
||
[TestFixtureTearDown] | ||
public void OnTestFixtureTearDown() | ||
{ | ||
appHost.Dispose(); | ||
} | ||
|
||
private static string ExpectedErrorCode = "ShouldNotBeEmpty"; | ||
|
||
protected static IServiceClient UnitTestServiceClient() | ||
{ | ||
EndpointHandlerBase.ServiceManager = new ServiceManager(true, typeof(SecureService).Assembly); | ||
return new DirectServiceClient(EndpointHandlerBase.ServiceManager); | ||
} | ||
|
||
public static IEnumerable ServiceClients | ||
{ | ||
get | ||
{ | ||
//Seriously retarded workaround for some devs idea who thought this should | ||
//be run for all test fixtures, not just this one. | ||
|
||
return new Func<IServiceClient>[] { | ||
() => UnitTestServiceClient(), | ||
() => new JsonServiceClient(ListeningOn), | ||
() => new JsvServiceClient(ListeningOn), | ||
() => new XmlServiceClient(ListeningOn), | ||
}; | ||
} | ||
} | ||
|
||
[Test, TestCaseSource(typeof(UserServiceValidationTests), "ServiceClients")] | ||
public void Get_empty_request_throws_validation_exception(Func<IServiceClient> factory) | ||
{ | ||
try | ||
{ | ||
var client = (IRestClient)factory(); | ||
var response = client.Get<OperationResponse>("Customers"); | ||
Assert.Fail("Should throw Validation Exception"); | ||
} | ||
catch (WebServiceException ex) | ||
{ | ||
Assert.That(ex.StatusCode, Is.EqualTo(400)); | ||
|
||
//One of this two properties should match | ||
Assert.That(ex.ErrorCode, Is.EqualTo(ExpectedErrorCode)); | ||
Assert.That(ex.StatusDescription, Is.EqualTo(ExpectedErrorCode)); | ||
} | ||
} | ||
|
||
} | ||
} |