Repository navigation
Expand file tree
/
Copy pathRuntimeAttributeTests.cs
More file actions
109 lines (89 loc) · 3.29 KB
/
Copy pathRuntimeAttributeTests.cs
File metadata and controls
109 lines (89 loc) · 3.29 KB
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
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using Funq;
using NUnit.Framework;
using ServiceStack.Api.Swagger;
using ServiceStack.Auth;
using ServiceStack.DataAnnotations;
using ServiceStack.Host;
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
{
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(RuntimeAttributeTests).Name, typeof (RuntimeAttributeAppHost).Assembly)
{
typeof(RuntimeAttributes)
.AddAttributes(new RuntimeAttributeRequestFilter());
typeof(Register)
.AddAttributes(new RouteAttribute("/custom-register"))
.AddAttributes(new RestrictAttribute(RequestAttributes.Json));
typeof(SwaggerResource)
.AddAttributes(new ExcludeAttribute(Feature.Soap));
}
public override void Configure(Container container)
{
this.RegisterService<RegisterService>("/register");
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new BasicAuthProvider(),
}));
}
}
[Test]
public void Does_add_CustomAttributes_to_when_added_in_AppHost_constructor()
{
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)));
//Allows JSON
appHost.ServiceController.AssertServiceRestrictions(typeof(Register), RequestAttributes.Json);
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));
}
}
}