Skip to content

Commit

Permalink
Add dynamic attribute example
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Dec 11, 2014
1 parent aae570c commit 355365b
Showing 1 changed file with 66 additions and 20 deletions.
86 changes: 66 additions & 20 deletions tests/ServiceStack.WebHost.Endpoints.Tests/RuntimeAttributeTests.cs
Expand Up @@ -7,51 +7,97 @@
using NUnit.Framework;
using ServiceStack.Auth;
using ServiceStack.Host;
using ServiceStack.Testing;
using ServiceStack.Web;

namespace ServiceStack.WebHost.Endpoints.Tests
{
public class RuntimeAttributes : IReturn<RuntimeAttributes>
{
public int Id { get; set; }
}

public class RuntimeAttributeService : Service
{
public object Any(RuntimeAttributes request)
{
return request;
}
}

public class RuntimeAttributeRequestFilter : RequestFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object requestDto)
{
((RuntimeAttributes)requestDto).Id++;
}
}

[TestFixture]
public class RuntimeAttributeTests
{
public class RuntimeAttributeAppHost : BasicAppHost
private ServiceStackHost appHost;

[TestFixtureSetUp]
public void OnTestFixtureSetUp()
{
appHost = new RuntimeAttributeAppHost()
.Init()
.Start(Config.ListeningOn);
}

[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
}

public class RuntimeAttributeAppHost : AppSelfHostBase
{
public RuntimeAttributeAppHost()
: base(typeof (RuntimeAttributeAppHost).Assembly)
: base(typeof(RuntimeAttributeTests).Name, typeof(RuntimeAttributeAppHost).Assembly) {}

public override void Configure(Container container)
{
typeof(RuntimeAttributes)
.AddAttributes(new RuntimeAttributeRequestFilter());

typeof(Register)
.AddAttributes(new RouteAttribute("/custom-register"))
.AddAttributes(new RestrictAttribute(RequestAttributes.Json));
}

public override void Configure(Container container)
{
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
this.RegisterService<RegisterService>("/register");

Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new BasicAuthProvider(),
}));

Plugins.Add(new RegistrationFeature());
}
}

[Test]
public void Does_add_CustomAttributes_to_when_added_in_AppHost_constructor()
{
using (var appHost = new RuntimeAttributeAppHost().Init())
{
string contentType;
var restPath = RestHandler.FindMatchingRestPath("GET", "/custom-register", out contentType);
string contentType;
var restPath = RestHandler.FindMatchingRestPath("GET", "/custom-register", out contentType);

Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.RequestType, Is.EqualTo(typeof(Register)));
Assert.That(restPath, Is.Not.Null);
Assert.That(restPath.RequestType, Is.EqualTo(typeof(Register)));

//Allows JSON
appHost.ServiceController.AssertServiceRestrictions(typeof(Register), RequestAttributes.Json);
//Allows JSON
appHost.ServiceController.AssertServiceRestrictions(typeof(Register), RequestAttributes.Json);

Assert.Throws<UnauthorizedAccessException>(() =>
appHost.ServiceController.AssertServiceRestrictions(typeof(Register), RequestAttributes.Xml));
}
Assert.Throws<UnauthorizedAccessException>(() =>
appHost.ServiceController.AssertServiceRestrictions(typeof(Register), RequestAttributes.Xml));
}

[Test]
public void Can_add_RequestFilter_attribute_in_Configure()
{
var client = new JsonServiceClient(Config.ListeningOn);

var response = client.Get(new RuntimeAttributes { Id = 1 });

Assert.That(response.Id, Is.EqualTo(2));
}
}
}

0 comments on commit 355365b

Please sign in to comment.