-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathComponentRenderingFunctionalTests.cs
213 lines (177 loc) · 7.33 KB
/
ComponentRenderingFunctionalTests.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net;
using System.Net.Http;
using System.Reflection;
using AngleSharp.Parser.Html;
using BasicWebSite;
using BasicWebSite.Services;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
public class ComponentRenderingFunctionalTests : LoggedTest
{
protected override void Initialize(TestContext context, MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper)
{
base.Initialize(context, methodInfo, testMethodArguments, testOutputHelper);
Factory = new MvcTestFixture<BasicWebSite.StartupWithoutEndpointRouting>(LoggerFactory);
}
public override void Dispose()
{
Factory.Dispose();
base.Dispose();
}
public MvcTestFixture<StartupWithoutEndpointRouting> Factory { get; private set; }
[Fact]
public async Task Renders_BasicComponent()
{
// Arrange & Act
var client = CreateClient(Factory);
var response = await client.GetAsync("http://localhost/components");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
AssertComponent("<p>Hello world!</p>", "Greetings", content);
}
[Fact]
public async Task Renders_RoutingComponent()
{
// Arrange & Act
var client = CreateClient(Factory.WithWebHostBuilder(builder =>
builder.ConfigureServices(services => services.AddRazorComponents().AddInteractiveServerComponents())));
var response = await client.GetAsync("http://localhost/components/routable");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
AssertComponent("Router component\n<p>Routed successfully</p>", "Routing", content);
}
[Fact]
public async Task Redirects_Navigation_Component()
{
// Arrange & Act
var fixture = Factory.WithWebHostBuilder(builder => builder.ConfigureServices(services => services.AddServerSideBlazor()));
fixture.ClientOptions.AllowAutoRedirect = false;
var client = CreateClient(fixture);
var response = await client.GetAsync("http://localhost/components/Navigation");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Redirect);
Assert.Equal("http://localhost/navigation-redirect", response.Headers.Location.ToString());
}
[Fact]
public async Task Renders_RoutingComponent_UsingRazorComponents_Prerenderer()
{
// Arrange & Act
var client = CreateClient(Factory.WithWebHostBuilder(builder =>
builder.ConfigureServices(services => services.AddRazorComponents().AddInteractiveServerComponents())));
var response = await client.GetAsync("http://localhost/components/routable");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
AssertComponent("Router component\n<p>Routed successfully</p>", "Routing", content);
}
[Fact]
public async Task Renders_ThrowingComponent_UsingRazorComponents_Prerenderer()
{
// Arrange & Act
var client = CreateClient(Factory.WithWebHostBuilder(builder =>
builder.ConfigureServices(services => services.AddRazorComponents().AddInteractiveServerComponents())));
var response = await client.GetAsync("http://localhost/components/throws");
// Assert
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Contains("InvalidTimeZoneException: test", content);
}
[Fact]
public async Task Renders_AsyncComponent()
{
// Arrange & Act
var expectedHtml = @"<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
<p>Weather data for 01/15/2019</p>
<table class=""table"">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr>
<td>06/05/2018</td>
<td>1</td>
<td>33</td>
<td>Freezing</td>
</tr>
<tr>
<td>07/05/2018</td>
<td>14</td>
<td>57</td>
<td>Bracing</td>
</tr>
<tr>
<td>08/05/2018</td>
<td>-13</td>
<td>9</td>
<td>Freezing</td>
</tr>
<tr>
<td>09/05/2018</td>
<td>-16</td>
<td>4</td>
<td>Balmy</td>
</tr>
<tr>
<td>10/05/2018</td>
<td>2</td>
<td>29</td>
<td>Chilly</td>
</tr>
</tbody>
</table>
";
var client = CreateClient(Factory);
var response = await client.GetAsync("http://localhost/components");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
AssertComponent(expectedHtml, "FetchData", content);
}
private void AssertComponent(string expectedContent, string divId, string responseContent)
{
var parser = new HtmlParser();
var htmlDocument = parser.Parse(responseContent);
var div = htmlDocument.Body.QuerySelector($"#{divId}");
var content = div.InnerHtml;
Assert.Equal(
expectedContent.Replace("\r\n", "\n"),
content.Replace("\r\n", "\n"));
}
// A simple delegating handler used in setting up test services so that we can configure
// services that talk back to the TestServer using HttpClient.
private class LoopHttpHandler : DelegatingHandler
{
}
private HttpClient CreateClient(
WebApplicationFactory<BasicWebSite.StartupWithoutEndpointRouting> fixture)
{
var loopHandler = new LoopHttpHandler();
var client = fixture
.WithWebHostBuilder(builder => builder.ConfigureServices(ConfigureTestWeatherForecastService))
.CreateClient();
// We configure the inner handler with a handler to this TestServer instance so that calls to the
// server can get routed properly.
loopHandler.InnerHandler = fixture.TestServer.CreateHandler();
void ConfigureTestWeatherForecastService(IServiceCollection services) =>
// We configure the test service here with an HttpClient that uses this loopback handler to talk
// to this TestServer instance.
services.AddSingleton(new WeatherForecastService(new HttpClient(loopHandler)
{
BaseAddress = fixture.ClientOptions.BaseAddress
}));
return client;
}
}