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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// 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 Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;
public class SimpleWithWebApplicationBuilderTests : IClassFixture<MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.Program>>
{
private readonly MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.Program> _fixture;
public SimpleWithWebApplicationBuilderTests(MvcTestFixture<SimpleWebSiteWithWebApplicationBuilder.Program> fixture)
{
_fixture = fixture;
Client = _fixture.CreateDefaultClient();
}
public HttpClient Client { get; }
[Fact]
public async Task HelloWorld()
{
// Arrange
var expected = "Hello World";
using var client = _fixture.CreateDefaultClient();
// Act
var content = await client.GetStringAsync("http://localhost/");
// Assert
Assert.Equal(expected, content);
}
[Fact]
public async Task JsonResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/json");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, content);
}
[Fact]
public async Task OkObjectResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/ok-object");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, content);
}
[Fact]
public async Task AcceptedObjectResult_Works()
{
// Arrange
var expected = "{\"name\":\"John\",\"age\":42}";
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/accepted-object");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Accepted);
Assert.Equal("/ok-object", response.Headers.Location.ToString());
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, content);
}
[Fact]
public async Task ActionReturningMoreThanOneResult_NotFound()
{
// Arrange
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/many-results?id=-1");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.NotFound);
}
[Fact]
public async Task ActionReturningMoreThanOneResult_Found()
{
// Arrange
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/many-results?id=7");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.MovedPermanently);
Assert.Equal("/json", response.Headers.Location.ToString());
}
[Fact]
public async Task MvcControllerActionWorks()
{
// Arrange
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.GetAsync("/greet");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello human", content);
}
[Fact(Skip = "Failing on Windows environments: https://github.com/dotnet/aspnetcore/issues/41937")]
public async Task DefaultEnvironment_Is_Development()
{
// Arrange
var expected = "Development";
using var client = new WebApplicationFactory<SimpleWebSiteWithWebApplicationBuilder.Program>().CreateClient();
// Act
var content = await client.GetStringAsync("http://localhost/environment");
// Assert
Assert.Equal(expected, content);
}
[Fact]
public async Task Configuration_Can_Be_Overridden()
{
// Arrange
var fixture = _fixture.WithWebHostBuilder(builder =>
{
builder.ConfigureAppConfiguration(builder =>
{
var config = new[]
{
KeyValuePair.Create("Greeting", "Bonjour tout le monde"),
};
builder.AddInMemoryCollection(config);
});
});
var expected = "Bonjour tout le monde";
using var client = fixture.CreateDefaultClient();
// Act
var content = await client.GetStringAsync("http://localhost/greeting");
// Assert
Assert.Equal(expected, content);
}
[Fact]
public async Task Environment_Can_Be_Overridden()
{
// Arrange
var fixture = _fixture.WithWebHostBuilder(builder =>
{
builder.UseEnvironment(Environments.Staging);
});
var expected = "Staging";
using var client = fixture.CreateDefaultClient();
// Act
var content = await client.GetStringAsync("http://localhost/environment");
// Assert
Assert.Equal(expected, content);
}
[Fact]
public async Task WebRoot_Can_Be_Overriden()
{
var webRoot = "foo";
var expectedWebRoot = "";
// Arrange
var fixture = _fixture.WithWebHostBuilder(builder =>
{
expectedWebRoot = Path.GetFullPath(Path.Combine(builder.GetSetting(WebHostDefaults.ContentRootKey), webRoot));
builder.UseSetting(WebHostDefaults.WebRootKey, webRoot);
});
using var client = fixture.CreateDefaultClient();
// Act
var content = await client.GetStringAsync("http://localhost/webroot");
// Assert
Assert.Equal(expectedWebRoot, content);
}
[Fact]
public async Task Accepts_Json_WhenBindingAComplexType()
{
// Act
var response = await Client.PostAsJsonAsync("accepts-default", new { name = "Test" });
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
}
[Fact]
public async Task Rejects_NonJson_WhenBindingAComplexType()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Post, "accepts-default");
request.Content = new StringContent("<xml />");
request.Content.Headers.ContentType = new("application/xml");
// Act
var response = await Client.SendAsync(request);
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.UnsupportedMediaType);
}
[Fact]
public async Task Accepts_NonJsonMediaType()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Post, "accepts-xml");
request.Content = new StringContent("<xml />");
request.Content.Headers.ContentType = new("application/xml");
// Act
var response = await Client.SendAsync(request);
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.Accepted);
}
[Fact]
public async Task FileUpload_Works()
{
// Arrange
var expected = "42";
var content = new MultipartFormDataContent();
content.Add(new StringContent(new string('a', 42)), "file", "file.txt");
using var client = _fixture.CreateDefaultClient();
// Act
var response = await client.PostAsync("/fileupload", content);
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var actual = await response.Content.ReadAsStringAsync();
Assert.Equal(expected, actual);
}
}
|