-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServiceCollectionExtensionsTests.cs
152 lines (126 loc) · 8.44 KB
/
ServiceCollectionExtensionsTests.cs
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
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using System;
using System.Net;
using Microsoft.AspNetCore.Routing;
namespace OnTopic.AspNetCore.Mvc.IntegrationTests {
/*============================================================================================================================
| TEST: SERVICE COLLECTION EXTENSIONS
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// The <see cref="ServiceCollectionExtensions"/> are responsible, primarily, for establishing routes based on the <see cref
/// ="IEndpointRouteBuilder"/> interface. These integration tests validate that those routes are operating as expected.
/// </summary>
[Collection("Web Application")]
public class ServiceCollectionExtensionsTests: IClassFixture<WebApplicationFactory<Startup>> {
/*==========================================================================================================================
| PRIVATE VARIABLES
\-------------------------------------------------------------------------------------------------------------------------*/
private readonly WebApplicationFactory<Startup> _factory;
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Initializes a new instance of the <see cref="TopicViewLocationExpanderTest"/>.
/// </summary>
public ServiceCollectionExtensionsTests(WebApplicationFactory<Startup> factory) {
_factory = factory;
}
/*==========================================================================================================================
| TEST: REQUEST PAGE: EXPECTED RESULTS
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Evaluates various routes enabled by the routing extension methods to ensure they correctly map to the expected
/// controllers, actions, and views.
/// </summary>
[Theory]
[InlineData("/Web/ContentList/", "~/Views/ContentList/ContentList.cshtml")] // MapTopicRoute()
[InlineData("/Area/Area/", "~/Areas/Area/Views/ContentType/ContentType.cshtml")] // MapTopicAreaRoute()
[InlineData("/Area/Controller/AreaAction/", "~/Areas/Area/Views/Controller/AreaAction.cshtml")] // MapTopicAreaRoute()
[InlineData("/Area/Accordion/", "~/Views/ContentList/Accordion.cshtml")] // MapImplicitAreaControllerRoute()
[InlineData("/Topic/3/", "~/Views/ContentList/ContentList.cshtml")] // MapTopicRedirect()
[InlineData("/Error/404", "400")] // MapTopicErrors()
[InlineData("/Error/Http/404", "400")] // MapDefaultControllerRoute()
[InlineData("/Error/Unauthorized/", "Unauthorized")] // MapTopicRoute()
public async Task RequestPage_ExpectedResults(string path, string expectedContent) {
var client = _factory.CreateClient();
var uri = new Uri(path, UriKind.Relative);
var response = await client.GetAsync(uri).ConfigureAwait(false);
var actualContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
Assert.Equal("text/html; charset=utf-8", response.Content.Headers.ContentType?.ToString());
Assert.Equal(expectedContent, actualContent);
}
/*==========================================================================================================================
| TEST: MAP TOPIC SITEMAP: RESPONDS TO REQUEST
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Evaluates a route associated with <see cref="ServiceCollectionExtensions.MapTopicSitemap(IEndpointRouteBuilder)"/> and
/// confirms that it responds appropriately.
/// </summary>
[Fact]
public async Task MapTopicSitemap_RespondsToRequest() {
var client = _factory.CreateClient();
var uri = new Uri($"/Sitemap/", UriKind.Relative);
var response = await client.GetAsync(uri).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
response.EnsureSuccessStatusCode();
Assert.Equal("text/xml", response.Content.Headers.ContentType?.ToString());
Assert.True(content.Contains("/Web/ContentList/</loc>", StringComparison.OrdinalIgnoreCase));
}
/*==========================================================================================================================
| TEST: USE STATUS CODE PAGES: RETURNS EXPECTED STATUS CODE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Evaluates a route with an error, and confirms that it returns a page with the expected status code.
/// </summary>
[Theory]
[InlineData("/MissingPage/", HttpStatusCode.NotFound, "400")]
[InlineData("/Web/MissingPage/", HttpStatusCode.NotFound, "400")]
[InlineData("/Web/Container/", HttpStatusCode.Forbidden, "400")]
[InlineData("/Scripts/ECMAScript.js", HttpStatusCode.NotFound, "The resource requested could not found.")]
public async Task UseStatusCodePages_ReturnsExpectedStatusCode(string path, HttpStatusCode statusCode, string expectedContent) {
var client = _factory.CreateClient();
var uri = new Uri(path, UriKind.Relative);
var response = await client.GetAsync(uri).ConfigureAwait(false);
var actualContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.Equal(statusCode, response.StatusCode);
Assert.Equal(expectedContent, actualContent);
}
/*==========================================================================================================================
| TEST: USE RESPONSE CACHING: RETURNS CACHED PAGE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Evaluates a route with response caching, and confirms that the page remains unchanged after subsequent calls.
/// </summary>
/// <remarks>
/// The <c>Counter.cshtml</c> page will increment a number output for every request to a given path. The <c>CachedPage</c>
/// request will not increment because the cached result is being returned; the <c>UncachedPage</c> will increment because
/// the results are not cached.
/// </remarks>
[Theory]
[InlineData("/Web/CachedPage/", "1", "1", true)]
[InlineData("/Web/UncachedPage/", "1", "2", false)]
public async Task UseResponseCaching_ReturnsCachedPage(
string path,
string firstResult,
string secondResult,
bool validateHeaders
) {
var client = _factory.CreateClient();
var uri = new Uri(path, UriKind.Relative);
var response1 = await client.GetAsync(uri).ConfigureAwait(false);
var content1 = await response1.Content.ReadAsStringAsync().ConfigureAwait(false);
var response2 = await client.GetAsync(uri).ConfigureAwait(false);
var content2 = await response2.Content.ReadAsStringAsync().ConfigureAwait(false);
response1.EnsureSuccessStatusCode();
Assert.StartsWith(firstResult, content1, StringComparison.Ordinal);
Assert.StartsWith(secondResult, content2, StringComparison.Ordinal);
Assert.Equal(validateHeaders? true : null, response1.Headers.CacheControl?.Public);
Assert.Equal(validateHeaders? TimeSpan.FromSeconds(10) : null, response1?.Headers.CacheControl?.MaxAge);
}
} //Class
} //Namespace