|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
| 7 | +using Microsoft.DotNet.Interactive.AspNetCore; |
| 8 | +using Microsoft.DotNet.Interactive.Commands; |
| 9 | +using Microsoft.DotNet.Interactive.CSharp; |
| 10 | +using Microsoft.DotNet.Interactive.Events; |
| 11 | +using Microsoft.DotNet.Interactive.Tests.Utility; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Microsoft.DotNet.Interactive.App.Tests |
| 15 | +{ |
| 16 | + public class AspNetCoreTests : IDisposable |
| 17 | + { |
| 18 | + private readonly CompositeKernel _kernel; |
| 19 | + |
| 20 | + public AspNetCoreTests() |
| 21 | + { |
| 22 | + _kernel = new CompositeKernel |
| 23 | + { |
| 24 | + new CSharpKernel(), |
| 25 | + }; |
| 26 | + |
| 27 | + var loadTask = new AspNetCoreKernelExtension().OnLoadAsync(_kernel); |
| 28 | + Assert.Same(Task.CompletedTask, loadTask); |
| 29 | + } |
| 30 | + |
| 31 | + public void Dispose() |
| 32 | + { |
| 33 | + _kernel.Dispose(); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task can_define_aspnet_endpoint_with_MapGet() |
| 38 | + { |
| 39 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 40 | +#!aspnet |
| 41 | +
|
| 42 | +Endpoints.MapGet(""/"", async context => |
| 43 | +{ |
| 44 | + await context.Response.WriteAsync($""Hello from MapGet!""); |
| 45 | +}); |
| 46 | +
|
| 47 | +await HttpClient.GetAsync(""/"")")); |
| 48 | + |
| 49 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 50 | + .And.ContainSingle<ReturnValueProduced>() |
| 51 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 52 | + .Which.Value.Should().Contain("Hello from MapGet!"); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task can_redefine_aspnet_endpoint_with_MapInteractive() |
| 57 | + { |
| 58 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 59 | +#!aspnet |
| 60 | +
|
| 61 | +Endpoints.MapGet(""/"", async context => |
| 62 | +{ |
| 63 | + await context.Response.WriteAsync($""Hello from MapGet!""); |
| 64 | +}); |
| 65 | +
|
| 66 | +Endpoints.MapInteractive(""/"", async context => |
| 67 | +{ |
| 68 | + await context.Response.WriteAsync($""Hello from MapInteractive!""); |
| 69 | +}); |
| 70 | +
|
| 71 | +Endpoints.MapInteractive(""/"", async context => |
| 72 | +{ |
| 73 | + await context.Response.WriteAsync($""Hello from MapInteractive 2!""); |
| 74 | +}); |
| 75 | +
|
| 76 | +await HttpClient.GetAsync(""/"")")); |
| 77 | + |
| 78 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 79 | + .And.ContainSingle<ReturnValueProduced>() |
| 80 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 81 | + .Which.Value.Should().Contain("Hello from MapInteractive 2!"); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task can_define_aspnet_middleware_with_Use() |
| 86 | + { |
| 87 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 88 | +#!aspnet |
| 89 | +
|
| 90 | +App.Use(next => |
| 91 | +{ |
| 92 | + return async httpContext => |
| 93 | + { |
| 94 | + await httpContext.Response.WriteAsync(""Hello from middleware!""); |
| 95 | + }; |
| 96 | +}); |
| 97 | +
|
| 98 | +await HttpClient.GetAsync(""/"")")); |
| 99 | + |
| 100 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 101 | + .And.ContainSingle<ReturnValueProduced>() |
| 102 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 103 | + .Which.Value.Should().Contain("Hello from middleware!"); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public async Task endpoints_take_precedence_over_new_middleware() |
| 108 | + { |
| 109 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 110 | +#!aspnet |
| 111 | +
|
| 112 | +App.Use(next => |
| 113 | +{ |
| 114 | + return async httpContext => |
| 115 | + { |
| 116 | + await httpContext.Response.WriteAsync(""Hello from middleware!""); |
| 117 | + }; |
| 118 | +}); |
| 119 | +
|
| 120 | +Endpoints.MapGet(""/"", async context => |
| 121 | +{ |
| 122 | + await context.Response.WriteAsync($""Hello from MapGet!""); |
| 123 | +}); |
| 124 | +
|
| 125 | +await HttpClient.GetAsync(""/"")")); |
| 126 | + |
| 127 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 128 | + .And.ContainSingle<ReturnValueProduced>() |
| 129 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 130 | + .Which.Value.Should().Contain("Hello from MapGet!"); |
| 131 | + |
| 132 | + // Re-adding the middleware makes no difference since it's added to the end of the pipeline. |
| 133 | + var result2 = await _kernel.SendAsync(new SubmitCode(@" |
| 134 | +#!aspnet |
| 135 | +
|
| 136 | +App.Use(next => |
| 137 | +{ |
| 138 | + return async httpContext => |
| 139 | + { |
| 140 | + await httpContext.Response.WriteAsync(""Hello from middleware!""); |
| 141 | + }; |
| 142 | +}); |
| 143 | +
|
| 144 | +await HttpClient.GetAsync(""/"")")); |
| 145 | + |
| 146 | + result2.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 147 | + .And.ContainSingle<ReturnValueProduced>() |
| 148 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 149 | + .Which.Value.Should().Contain("Hello from MapGet!"); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public async Task repeatedly_invoking_aspnet_command_noops() |
| 154 | + { |
| 155 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 156 | +#!aspnet |
| 157 | +#!aspnet |
| 158 | +
|
| 159 | +Endpoints.MapGet(""/"", async context => |
| 160 | +{ |
| 161 | + await context.Response.WriteAsync($""Hello from MapGet!""); |
| 162 | +}); |
| 163 | +
|
| 164 | +await HttpClient.GetAsync(""/"")")); |
| 165 | + |
| 166 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 167 | + .And.ContainSingle<ReturnValueProduced>() |
| 168 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 169 | + .Which.Value.Should().Contain("Hello from MapGet!"); |
| 170 | + } |
| 171 | + |
| 172 | + [Fact] |
| 173 | + public async Task aspnet_command_is_only_necessary_in_first_submission() |
| 174 | + { |
| 175 | + var commandResult = await _kernel.SendAsync(new SubmitCode("#!aspnet")); |
| 176 | + |
| 177 | + commandResult.KernelEvents.ToSubscribedList().Should().NotContainErrors(); |
| 178 | + |
| 179 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 180 | +Endpoints.MapGet(""/"", async context => |
| 181 | +{ |
| 182 | + await context.Response.WriteAsync($""Hello from MapGet!""); |
| 183 | +}); |
| 184 | +
|
| 185 | +await HttpClient.GetAsync(""/"")")); |
| 186 | + |
| 187 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 188 | + .And.ContainSingle<ReturnValueProduced>() |
| 189 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 190 | + .Which.Value.Should().Contain("Hello from MapGet!"); |
| 191 | + } |
| 192 | + |
| 193 | + [Fact] |
| 194 | + public async Task result_includes_trace_level_logs() |
| 195 | + { |
| 196 | + var commandResult = await _kernel.SendAsync(new SubmitCode("#!aspnet")); |
| 197 | + |
| 198 | + commandResult.KernelEvents.ToSubscribedList().Should().NotContainErrors(); |
| 199 | + |
| 200 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 201 | +#!aspnet |
| 202 | +
|
| 203 | +using Microsoft.Extensions.DependencyInjection; |
| 204 | +using Microsoft.Extensions.Logging; |
| 205 | +
|
| 206 | +Endpoints.MapGet(""/"", async httpContext => |
| 207 | +{ |
| 208 | + var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>(); |
| 209 | + var logger = loggerFactory.CreateLogger(""interactive""); |
| 210 | + logger.LogTrace(""Log from MapGet!""); |
| 211 | +
|
| 212 | + await httpContext.Response.WriteAsync(""Hello from MapGet!""); |
| 213 | +}); |
| 214 | +
|
| 215 | +await HttpClient.GetAsync(""/"")")); |
| 216 | + |
| 217 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 218 | + .And.ContainSingle<ReturnValueProduced>() |
| 219 | + .Which.FormattedValues.Should().ContainSingle(f => f.MimeType == "text/html") |
| 220 | + .Which.Value.Should().Contain("Log from MapGet!"); |
| 221 | + } |
| 222 | + |
| 223 | + [Fact] |
| 224 | + public async Task server_listens_on_ephemeral_port() |
| 225 | + { |
| 226 | + var result = await _kernel.SendAsync(new SubmitCode(@" |
| 227 | +#!aspnet |
| 228 | +
|
| 229 | +HttpClient.BaseAddress")); |
| 230 | + |
| 231 | + // Assume any port higher than 1000 is ephemeral. In practice, the start of the ephemeral port range is |
| 232 | + // usually even higher (Windows XP and older Windows releases notwithstanding). |
| 233 | + // https://en.wikipedia.org/wiki/Ephemeral_port |
| 234 | + result.KernelEvents.ToSubscribedList().Should().NotContainErrors() |
| 235 | + .And.ContainSingle<ReturnValueProduced>() |
| 236 | + .Which.Value.Should().Match(uri => uri.As<Uri>().Port > 1_000); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments