Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RouterBenchmarkTest.cs « Benchmarks « FunctionalTests « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b27f426012a9624017053f9d66d843218733aa92 (plain)
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
// 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.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Microsoft.AspNetCore.Routing.FunctionalTests;

public class RouterBenchmarkTest : IDisposable
{
    private readonly HttpClient _client;
    private readonly IHost _host;
    private readonly TestServer _testServer;

    public RouterBenchmarkTest()
    {
        // This switch and value are set by benchmark server when running the app for profiling.
        var args = new[] { "--scenarios", "PlaintextRouting" };
        var hostBuilder = Benchmarks.Program.GetHostBuilder(args);

        _host = hostBuilder.Build();

        // Make sure we are using the right startup
        var configuration = _host.Services.GetService<IConfiguration>();
        var startupName = configuration["Startup"];
        Assert.Equal(nameof(Benchmarks.StartupUsingRouter), startupName);

        _testServer = _host.GetTestServer();
        _host.Start();
        _client = _testServer.CreateClient();
        _client.BaseAddress = new Uri("http://localhost");
    }

    [Fact]
    public async Task RouteHandlerWritesResponse()
    {
        // Arrange
        var expectedContentType = "text/plain";
        var expectedContent = "Hello, World!";

        // Act
        var response = await _client.GetAsync("/plaintext");

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.NotNull(response.Content);
        Assert.NotNull(response.Content.Headers.ContentType);
        Assert.Equal(expectedContentType, response.Content.Headers.ContentType.MediaType);
        var actualContent = await response.Content.ReadAsStringAsync();
        Assert.Equal(expectedContent, actualContent);
    }

    public void Dispose()
    {
        _testServer.Dispose();
        _client.Dispose();
        _host.Dispose();
    }
}