コード例 #1
0
ファイル: DelegateTests.cs プロジェクト: tarynt/AspNetCore
    public async Task UpdateDelegationRuleTest()
    {
        var queueName = Guid.NewGuid().ToString();

        using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
        },
                                                        options =>
        {
            options.RequestQueueName = queueName;
        });

        DelegationRule destination = default;

        using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
        {
            var delegateFeature = httpContext.Features.Get <IHttpSysRequestDelegationFeature>();
            delegateFeature.DelegateRequest(destination);
            return(Task.CompletedTask);
        });

        var delegationProperty = delegator.Features.Get <IServerDelegationFeature>();

        destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
        // Send a request to ensure the rule is fully active
        var responseString = await SendRequestAsync(delegatorAddress);

        destination?.Dispose();
        destination    = delegationProperty.CreateDelegationRule(queueName, receiverAddress);
        responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);
        destination?.Dispose();
    }
コード例 #2
0
ファイル: DelegateTests.cs プロジェクト: tarynt/AspNetCore
    public async Task DelegateAfterWriteToResponseBodyShouldThrowTest()
    {
        var queueName = Guid.NewGuid().ToString();

        using var receiver = Utilities.CreateHttpServer(out var receiverAddress, httpContext =>
        {
            httpContext.Response.StatusCode = StatusCodes.Status418ImATeapot;
            return(Task.CompletedTask);
        },
                                                        options =>
        {
            options.RequestQueueName = queueName;
        });

        DelegationRule destination = default;

        using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
            var delegateFeature = httpContext.Features.Get <IHttpSysRequestDelegationFeature>();
            Assert.False(delegateFeature.CanDelegate);
            Assert.Throws <InvalidOperationException>(() => delegateFeature.DelegateRequest(destination));
        });

        var delegationProperty = delegator.Features.Get <IServerDelegationFeature>();

        destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);

        var responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);
        destination?.Dispose();
    }
コード例 #3
0
ファイル: DelegateTests.cs プロジェクト: tarynt/AspNetCore
    public async Task DelegateAfterRequestBodyReadShouldThrow()
    {
        var queueName = Guid.NewGuid().ToString();

        using var receiver = Utilities.CreateHttpServer(out var receiverAddress, httpContext =>
        {
            httpContext.Response.StatusCode = StatusCodes.Status418ImATeapot;
            return(Task.CompletedTask);
        },
                                                        options =>
        {
            options.RequestQueueName = queueName;
        });

        DelegationRule destination = default;

        using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, async httpContext =>
        {
            var memoryStream = new MemoryStream();
            await httpContext.Request.Body.CopyToAsync(memoryStream);
            var delegateFeature = httpContext.Features.Get <IHttpSysRequestDelegationFeature>();
            Assert.Throws <InvalidOperationException>(() => delegateFeature.DelegateRequest(destination));
        });

        var delegationProperty = delegator.Features.Get <IServerDelegationFeature>();

        destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);

        _ = await SendRequestWithBodyAsync(delegatorAddress);

        destination?.Dispose();
    }
コード例 #4
0
    public async Task DelegateAfterReceiverRestart()
    {
        var queueName = Guid.NewGuid().ToString();

        using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
        },
                                                        options =>
        {
            options.RequestQueueName = queueName;
        });

        DelegationRule destination = default;

        using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
        {
            var delegateFeature = httpContext.Features.Get <IHttpSysRequestDelegationFeature>();
            delegateFeature.DelegateRequest(destination);
            return(Task.CompletedTask);
        });

        var delegationProperty = delegator.Features.Get <IServerDelegationFeature>();

        destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);

        var responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);

        // Stop the receiver
        receiver?.Dispose();

        // Start the receiver again but this time we need to attach to the existing queue.
        // Due to https://github.com/dotnet/aspnetcore/issues/40359, we have to manually
        // register URL prefixes and attach the server's queue to them.
        using var receiverRestarted = (MessagePump)Utilities.CreateHttpServer(out receiverAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
        },
                                                                              options =>
        {
            options.RequestQueueName = queueName;
            options.RequestQueueMode = RequestQueueMode.Attach;
            options.UrlPrefixes.Clear();
            options.UrlPrefixes.Add(receiverAddress);
        });
        AttachToUrlGroup(receiverRestarted.Listener.RequestQueue);
        receiverRestarted.Listener.Options.UrlPrefixes.RegisterAllPrefixes(receiverRestarted.Listener.UrlGroup);

        responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);

        destination?.Dispose();
    }
コード例 #5
0
ファイル: DelegateTests.cs プロジェクト: tarynt/AspNetCore
    public async Task DelegateAfterReceiverRestart()
    {
        var queueName = Guid.NewGuid().ToString();

        using var receiver = Utilities.CreateHttpServer(out var receiverAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
        },
                                                        options =>
        {
            options.RequestQueueName = queueName;
        });

        DelegationRule destination = default;

        using var delegator = Utilities.CreateHttpServer(out var delegatorAddress, httpContext =>
        {
            var delegateFeature = httpContext.Features.Get <IHttpSysRequestDelegationFeature>();
            delegateFeature.DelegateRequest(destination);
            return(Task.CompletedTask);
        });

        var delegationProperty = delegator.Features.Get <IServerDelegationFeature>();

        destination = delegationProperty.CreateDelegationRule(queueName, receiverAddress);

        var responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);

        // Stop the receiver
        receiver?.Dispose();

        // Start the receiver again but this time we need to use CreateOrAttach to attach to the existing queue and setup the UrlPrefixes
        using var receiverRestarted = (MessagePump)Utilities.CreateHttpServer(out receiverAddress, async httpContext =>
        {
            await httpContext.Response.WriteAsync(_expectedResponseString);
        },
                                                                              options =>
        {
            options.RequestQueueName = queueName;
            options.RequestQueueMode = RequestQueueMode.CreateOrAttach;
            options.UrlPrefixes.Clear();
            options.UrlPrefixes.Add(receiverAddress);
        });

        responseString = await SendRequestAsync(delegatorAddress);

        Assert.Equal(_expectedResponseString, responseString);

        destination?.Dispose();
    }
コード例 #6
0
 private void DisposeDelegationRule(DelegationRule rule)
 {
     try
     {
         // Note that dispose doesn't properly cleanup everything.
         // This is only an issue if you want to create the same rule again in the same process.
         // Shutdown of the process properly cleans up everything.
         // https://github.com/dotnet/aspnetcore/issues/27126
         rule.Dispose();
     }
     catch (ArgumentNullException ex)
     {
         // There is a bug in rule cleanup that causes a failure
         // https://github.com/dotnet/aspnetcore/issues/26989
         // This failure then causes a null ref bug to get hit
         // https://github.com/dotnet/aspnetcore/issues/26982
         this.logger.LogWarning(ex, "Known issue with disposing delegation rules");
     }
 }