|
| 1 | +// Copyright (C) 2021 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Net; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.TestHost; |
| 9 | +using Microsoft.Extensions.Hosting; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Microsoft.AspNetCore.Mvc; |
| 12 | +using NUnit.Framework; |
| 13 | +using Xtensive.Orm.Web.Tests.Middleware; |
| 14 | +using Microsoft.AspNetCore.Builder; |
| 15 | +using System.Linq; |
| 16 | +using System.Diagnostics.CodeAnalysis; |
| 17 | + |
| 18 | +namespace Xtensive.Orm.Web.Tests.Middleware |
| 19 | +{ |
| 20 | + public class MiddlewareTestController : Controller |
| 21 | + { |
| 22 | + [SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "parameter required for test")] |
| 23 | + public IActionResult NoDomainAsService([FromServices] SessionAccessor accessor) |
| 24 | + { |
| 25 | + return Ok(); |
| 26 | + } |
| 27 | + |
| 28 | + public IActionResult NoAccessors() |
| 29 | + { |
| 30 | + var sessionObject = (Session) HttpContext.Items[SessionAccessor.SessionIdentifier]; |
| 31 | + if (sessionObject == null) |
| 32 | + return BadRequest("Session not initialized"); |
| 33 | + return Ok("Ok"); |
| 34 | + } |
| 35 | + |
| 36 | + public IActionResult WithOneAccessorNoMiddleware([FromServices] SessionAccessor accessor) |
| 37 | + { |
| 38 | + var sessionObject = (Session) HttpContext.Items[SessionAccessor.SessionIdentifier]; |
| 39 | + if (sessionObject == null && !accessor.ContextIsBound) |
| 40 | + return Ok(); |
| 41 | + return BadRequest("Session initialized or context bound"); |
| 42 | + } |
| 43 | + |
| 44 | + public IActionResult WithOneAccessorFromServices([FromServices] SessionAccessor accessor) |
| 45 | + { |
| 46 | + if (!accessor.ContextIsBound) |
| 47 | + return BadRequest("ContextIsBound=false"); |
| 48 | + if (accessor.Session == null) |
| 49 | + return BadRequest("SessionIsNull"); |
| 50 | + return Ok(); |
| 51 | + } |
| 52 | + |
| 53 | + public IActionResult WithTwoAccessorsFromServices( |
| 54 | + [FromServices] SessionAccessor accessor1, |
| 55 | + [FromServices] SessionAccessor accessor2) |
| 56 | + { |
| 57 | + if (!accessor1.ContextIsBound || !accessor2.ContextIsBound) |
| 58 | + return BadRequest("ContextIsBound=false"); |
| 59 | + if (accessor1.Session == null || accessor2.Session == null) |
| 60 | + return BadRequest("SessionIsNull"); |
| 61 | + return Ok(); |
| 62 | + } |
| 63 | + |
| 64 | + [SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "parameter required for test")] |
| 65 | + public IActionResult ManualSessionRemoval([FromServices] SessionAccessor accessor) |
| 66 | + { |
| 67 | + _ = HttpContext.Items.Remove(SessionAccessor.SessionIdentifier); |
| 68 | + return Ok(); |
| 69 | + } |
| 70 | + |
| 71 | + [SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "parameter required for test")] |
| 72 | + public IActionResult ManualTransactionRemoval([FromServices] SessionAccessor accessor) |
| 73 | + { |
| 74 | + _ = HttpContext.Items.Remove(SessionAccessor.ScopeIdentifier); |
| 75 | + return Ok(); |
| 76 | + } |
| 77 | + |
| 78 | + public IActionResult AutoCompleteTransaction([FromServices] SessionAccessor accessor) |
| 79 | + { |
| 80 | + _ = new DummyEntity(accessor.Session); |
| 81 | + return Ok(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +namespace Xtensive.Orm.Web.Tests |
| 87 | +{ |
| 88 | + public class MiddlewareTest : WebTestBase |
| 89 | + { |
| 90 | + [Test] |
| 91 | + public async Task NoDomainAsServiceTest() |
| 92 | + { |
| 93 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 94 | + nameof(MiddlewareTestController.NoDomainAsService), |
| 95 | + (s) => { |
| 96 | + // no domain registration |
| 97 | + AddTestRequiredServices(s); |
| 98 | + AddControllers(s); |
| 99 | + }); |
| 100 | + |
| 101 | + using (var host = await hostBuilder.StartAsync()) { |
| 102 | + var ex = Assert.ThrowsAsync<InvalidOperationException>(async () => await host.GetTestClient().GetAsync("/")); |
| 103 | + Assert.That(ex.Message.Contains("Domain is not found"), Is.True); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public async Task NoAccessorsTest() |
| 109 | + { |
| 110 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 111 | + nameof(MiddlewareTestController.NoAccessors)); |
| 112 | + |
| 113 | + using (var host = await hostBuilder.StartAsync()) { |
| 114 | + var response = await host.GetTestClient().GetAsync("/"); |
| 115 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public async Task WithOneAccessorNoMiddlewareTest() |
| 121 | + { |
| 122 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 123 | + nameof(MiddlewareTestController.WithOneAccessorNoMiddleware), |
| 124 | + (app, controller, action)=> ConfigureRouting(app, controller, action)); |
| 125 | + |
| 126 | + using (var host = await hostBuilder.StartAsync()) { |
| 127 | + var response = await host.GetTestClient().GetAsync("/"); |
| 128 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public async Task WithOneAccessorFromServicesTest() |
| 134 | + { |
| 135 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 136 | + nameof(MiddlewareTestController.WithOneAccessorFromServices)); |
| 137 | + |
| 138 | + using (var host = await hostBuilder.StartAsync()) { |
| 139 | + var response = await host.GetTestClient().GetAsync("/"); |
| 140 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + [Test] |
| 145 | + public async Task WithTwoAccessorsFromServicesTest() |
| 146 | + { |
| 147 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 148 | + nameof(MiddlewareTestController.WithTwoAccessorsFromServices)); |
| 149 | + |
| 150 | + using (var host = await hostBuilder.StartAsync()) { |
| 151 | + var response = await host.GetTestClient().GetAsync("/"); |
| 152 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + [Test] |
| 157 | + public async Task ManualSessionRemovalTest() |
| 158 | + { |
| 159 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 160 | + nameof(MiddlewareTestController.ManualSessionRemoval)); |
| 161 | + |
| 162 | + using (var host = await hostBuilder.StartAsync()) { |
| 163 | + Assert.DoesNotThrowAsync(async () => await host.GetTestClient().GetAsync("/")); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + [Test] |
| 168 | + public async Task ManualTransactionRemovalTest() |
| 169 | + { |
| 170 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 171 | + nameof(MiddlewareTestController.ManualTransactionRemoval)); |
| 172 | + |
| 173 | + using (var host = await hostBuilder.StartAsync()) { |
| 174 | + Assert.DoesNotThrowAsync(async () => await host.GetTestClient().GetAsync("/")); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + [Test] |
| 179 | + public async Task AutoCompleteTransactionTest() |
| 180 | + { |
| 181 | + var hostBuilder = GetConfiguredHostBuilder<MiddlewareTestController>( |
| 182 | + nameof(MiddlewareTestController.AutoCompleteTransaction)); |
| 183 | + |
| 184 | + using (var host = await hostBuilder.StartAsync()) { |
| 185 | + var response = await host.GetTestClient().GetAsync("/"); |
| 186 | + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); |
| 187 | + } |
| 188 | + |
| 189 | + await using (var session = await Domain.OpenSessionAsync()) |
| 190 | + await using (var tx = await session.OpenTransactionAsync()) { |
| 191 | + Assert.That(session.Query.All<DummyEntity>().FirstOrDefault(), Is.Not.Null); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + protected override IApplicationBuilder ConfigurePreRoutingPart(IApplicationBuilder app) => |
| 196 | + app.UseDataObjectsSessionOpener(); |
| 197 | + |
| 198 | + protected override void AddTestRequiredServices(IServiceCollection services) |
| 199 | + { |
| 200 | + base.AddTestRequiredServices(services); |
| 201 | + _ = services.AddScoped<SessionAccessor>(); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments