forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIgnoreRouteTest.cs
More file actions
179 lines (151 loc) · 6.86 KB
/
Copy pathIgnoreRouteTest.cs
File metadata and controls
179 lines (151 loc) · 6.86 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Hosting;
using System.Web.Http.Owin.ExceptionHandling;
using System.Web.Http.Routing;
using Microsoft.Owin;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Owin
{
public class IgnoreRouteIntegrationTests
{
[Fact]
public void Invoke_IfRouteIsIgnored_CallsNextMiddleware()
{
// Arrange
int expectedStatusCode = 123;
string pathToIgnoreRoute = "ignore";
OwinMiddleware next = CreateStubMiddleware(expectedStatusCode);
using (HttpServer server = new HttpServer())
{
server.Configuration.Routes.IgnoreRoute("IgnoreRouteName", pathToIgnoreRoute);
server.Configuration.MapHttpAttributeRoutes(); // See IgnoreController
OwinMiddleware product = CreateProductUnderTest(next, server);
IOwinRequest request = CreateStubRequest(new Uri("http://somehost/" + pathToIgnoreRoute));
Mock<IOwinResponse> mock = CreateStubResponseMock();
int statusCode = 0;
mock.SetupSet(r => r.StatusCode = It.IsAny<int>()).Callback<int>((s) => statusCode = s);
IOwinResponse response = mock.Object;
IOwinContext context = CreateStubContext(request, response);
// Act
Task task = product.Invoke(context);
// Assert
Assert.NotNull(task);
task.WaitUntilCompleted();
task.ThrowIfFaulted();
Assert.Equal(expectedStatusCode, statusCode);
}
}
[Fact]
public void Invoke_IfRouteIsIgnored_WithConstraints_CallsNextMiddleware()
{
// Arrange
int expectedStatusCode = 0;
string pathToIgnoreRoute = "constraint/10";
using (HttpServer server = new HttpServer())
{
server.Configuration.Routes.IgnoreRoute("Constraints", "constraint/{id}", constraints: new { constraint = new CustomConstraint() });
server.Configuration.MapHttpAttributeRoutes(); // See IgnoreController
OwinMiddleware product = CreateProductUnderTest(null, server);
IOwinRequest request = CreateStubRequest(new Uri("http://somehost/" + pathToIgnoreRoute));
Mock<IOwinResponse> mock = CreateStubResponseMock();
int statusCode = 0;
mock.SetupSet(r => r.StatusCode = It.IsAny<int>()).Callback<int>((s) => statusCode = s);
IOwinResponse response = mock.Object;
IOwinContext context = CreateStubContext(request, response);
// Act
Task task = product.Invoke(context);
// Assert
Assert.NotNull(task);
task.WaitUntilCompleted();
task.ThrowIfFaulted();
Assert.Equal(expectedStatusCode, statusCode);
}
}
private static IOwinContext CreateStubContext(IOwinRequest request, IOwinResponse response)
{
Mock<IOwinContext> mock = new Mock<IOwinContext>(MockBehavior.Strict);
mock.SetupGet(c => c.Request).Returns(request);
mock.SetupGet(c => c.Response).Returns(response);
mock.Setup(c => c.Get<bool>("server.IsLocal")).Returns(true);
return mock.Object;
}
private static OwinMiddleware CreateStubMiddleware(int statusCode)
{
Mock<OwinMiddleware> mock = new Mock<OwinMiddleware>(MockBehavior.Strict, null);
mock
.Setup(m => m.Invoke(It.IsAny<IOwinContext>()))
.Callback<IOwinContext>((c) => c.Response.StatusCode = statusCode)
.Returns(Task.FromResult(0));
return mock.Object;
}
private static IOwinRequest CreateStubRequest(Uri uri)
{
Mock<IOwinRequest> mock = new Mock<IOwinRequest>(MockBehavior.Strict);
mock.SetupGet(r => r.CallCancelled).Returns(CancellationToken.None);
mock.SetupGet(r => r.Environment).Returns((IDictionary<string, object>)null);
mock.SetupGet(r => r.Body).Returns(Stream.Null);
mock.SetupGet(r => r.Method).Returns("GET");
mock.SetupGet(r => r.Uri).Returns(uri);
mock.SetupGet(r => r.PathBase).Returns(new PathString(String.Empty));
mock.SetupGet(r => r.Headers).Returns(CreateFakeHeaders());
mock.SetupGet(r => r.User).Returns((IPrincipal)null);
return mock.Object;
}
private static Mock<IOwinResponse> CreateStubResponseMock()
{
Mock<IOwinResponse> mock = new Mock<IOwinResponse>(MockBehavior.Strict);
mock.SetupSet(r => r.ReasonPhrase = It.IsAny<string>());
mock.SetupGet(r => r.Environment).Returns((IDictionary<string, object>)null);
mock.SetupGet(r => r.Headers).Returns(CreateFakeHeaders());
mock.SetupGet(r => r.Body).Returns(Stream.Null);
return mock;
}
private static IHeaderDictionary CreateFakeHeaders()
{
return new HeaderDictionary(new Dictionary<string, string[]>());
}
private static HttpMessageHandlerAdapter CreateProductUnderTest(OwinMiddleware next, HttpMessageHandler messageHandler)
{
return new HttpMessageHandlerAdapter(next: next, options: new HttpMessageHandlerOptions
{
MessageHandler = messageHandler,
BufferPolicySelector = new Mock<IHostBufferPolicySelector>().Object,
ExceptionLogger = new EmptyExceptionLogger(),
ExceptionHandler = new Mock<IExceptionHandler>().Object
});
}
public class IgnoreController : ApiController
{
[Route("ignore")]
[Route("constraint/10")]
public IHttpActionResult Get()
{
return Ok();
}
}
public class CustomConstraint : IHttpRouteConstraint
{
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName,
IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
long id;
if (values.ContainsKey("id")
&& Int64.TryParse(values["id"].ToString(), out id)
&& (id == 10))
{
return true;
}
return false;
}
}
}
}