forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRouteTest.cs
More file actions
115 lines (99 loc) · 4.89 KB
/
Copy pathHttpRouteTest.cs
File metadata and controls
115 lines (99 loc) · 4.89 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
// 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.Net.Http;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
namespace System.Web.Http.Hosting
{
public class HttpRouteTest
{
[Fact]
public void Ctor_PreservesWhitespaceInRouteTemplate()
{
string whitespace = " ";
HttpRoute httpRoute = new HttpRoute(whitespace);
Assert.Equal(whitespace, httpRoute.RouteTemplate);
}
[Theory]
[InlineData("{controller}/{id}", "/SelfHostServer", "http://localhost/SelfHostServer/Customer/999")]
[InlineData("{controller}/{id}", "", "http://localhost/Customer/999")]
[InlineData("{controller}", "", "http://localhost/")]
[InlineData("{controller}", "/SelfHostServer", "http://localhost/SelfHostServer")]
[InlineData("{controller}", "", "http://localhost")]
[InlineData("{controller}/{id}", "", "http://localhost/")]
[InlineData("{controller}/{id}", "/SelfHostServer", "http://localhost/SelfHostServer")]
[InlineData("{controller}/{id}", "", "http://localhost")]
[InlineData("api", "", "http://localhost/api")]
[InlineData("api", "", "http://LOCALHOST/API")]
[InlineData("{controller}/{id}", "/SelfHostServer/Customer/999", "http://localhost/SelfHostServer/Customer/999")]
[InlineData("{controller}/{id}", "/SelfHostServer/Customer/999", "http://localhost/SelfHostServer/Customer/999/")]
[InlineData("{controller}/{id}", "/SelfHostServer/Customer/999/", "http://localhost/SelfHostServer/Customer/999/")]
[InlineData("{controller}", "/SelfHostServer", "http://localhost/SelfHostServer/")]
[InlineData("{controller}", "/SelfHostServer/", "http://localhost/SelfHostServer/")]
public void GetRouteDataShouldMatch(string uriTemplate, string virtualPathRoot, string requestUri)
{
HttpRoute route = new HttpRoute(uriTemplate);
route.Defaults.Add("controller", "Customer");
route.Defaults.Add("id", "999");
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri(requestUri);
IHttpRouteData data = route.GetRouteData(virtualPathRoot, request);
// Assert
Assert.NotNull(data);
IDictionary<string, object> expectedResult = new Dictionary<string, object>();
expectedResult["controller"] = "Customer";
expectedResult["id"] = "999";
Assert.Equal(expectedResult, data.Values, new DictionaryEqualityComparer());
}
[Theory]
[InlineData("{controller}/{id}", "/SelfHostServer/Customer/999/Invalid", "http://localhost/SelfHostServer/Customer/999")]
[InlineData("{controller}", "/SelfHostServer/", "http://localhost/SelfHostServer")]
public void GetRouteDataDoesNotMatch(string uriTemplate, string virtualPathRoot, string requestUri)
{
HttpRoute route = new HttpRoute(uriTemplate);
route.Defaults.Add("controller", "Customer");
route.Defaults.Add("id", "999");
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri(requestUri);
IHttpRouteData data = route.GetRouteData(virtualPathRoot, request);
// Assert
Assert.Null(data);
}
[Theory]
[InlineData("controller")]
[InlineData("cOnTrOlLeR")]
[InlineData("CONTROLLER")]
public void GetVirtualPath_GetsValuesInCaseInsensitiveWay(string controllerKey)
{
var route = new HttpRoute("{controller}");
var request = new HttpRequestMessage();
request.SetRouteData(
new HttpRouteData(route, new HttpRouteValueDictionary() {
{ "controller", "Employees" }
}));
var values = new HttpRouteValueDictionary()
{
{ "httproute", true },
{ controllerKey, "Customers" }
};
IHttpVirtualPathData virtualPath = route.GetVirtualPath(request, values);
Assert.NotNull(virtualPath);
Assert.Equal("Customers", virtualPath.VirtualPath);
}
[Fact]
public void GetVirtualPath_GeneratesPathWithoutRouteData()
{
var route = new HttpRoute("{controller}");
var request = new HttpRequestMessage();
var values = new HttpRouteValueDictionary()
{
{ "httproute", true },
{ "controller", "Customers" }
};
IHttpVirtualPathData virtualPath = route.GetVirtualPath(request, values);
Assert.NotNull(virtualPath);
Assert.Equal("Customers", virtualPath.VirtualPath);
}
}
}