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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Hosting;
using RoutingWebSite;
using Xunit;
namespace Microsoft.AspNetCore.Routing.FunctionalTests;
public class RouterSampleTest : IDisposable
{
private readonly HttpClient _client;
private readonly IHost _host;
private readonly TestServer _testServer;
public RouterSampleTest()
{
var hostBuilder = Program.GetHostBuilder(new[] { Program.RouterScenario, });
_host = hostBuilder.Build();
_testServer = _host.GetTestServer();
_host.Start();
_client = _testServer.CreateClient();
_client.BaseAddress = new Uri("http://localhost");
}
[Theory]
[InlineData("Branch1")]
[InlineData("Branch2")]
public async Task Routing_CanRouteRequest_ToBranchRouter(string branch)
{
// Arrange
var message = new HttpRequestMessage(HttpMethod.Get, $"{branch}/api/get/5");
// Act
var response = await _client.SendAsync(message);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal($"{branch} - API Get 5", await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task Routing_CanRouteRequestDelegate_ToSpecificHttpVerb()
{
// Arrange
var message = new HttpRequestMessage(HttpMethod.Get, "api/get/5");
// Act
var response = await _client.SendAsync(message);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal($"API Get 5", await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task Routing_CanRouteRequest_ToSpecificMiddleware()
{
// Arrange
var message = new HttpRequestMessage(HttpMethod.Get, "api/middleware");
// Act
var response = await _client.SendAsync(message);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal($"Middleware!", await response.Content.ReadAsStringAsync());
}
[Theory]
[InlineData("GET")]
[InlineData("POST")]
[InlineData("PUT")]
[InlineData("PATCH")]
[InlineData("DELETE")]
[InlineData("HEAD")]
[InlineData("OPTIONS")]
public async Task Routing_CanRouteRequest_ToDefaultHandler(string httpVerb)
{
// Arrange
var message = new HttpRequestMessage(new HttpMethod(httpVerb), "api/all/Joe/Duf");
var expectedBody = $"Verb = {httpVerb} - Path = /api/all/Joe/Duf - Route values - [name, Joe], [lastName, Duf]";
// Act
var response = await _client.SendAsync(message);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var body = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedBody, body);
}
public void Dispose()
{
_testServer.Dispose();
_client.Dispose();
_host.Dispose();
}
}
|