Example #1
0
        private async Task TestMiddleWare(string requestMethod, Dictionary <string, string> requestHeader, string requestUri, int expectedStatus, Dictionary <string, string> expectedResponseHeader, bool allowExternalValidation)
        {
            var context = new DefaultHttpContext();

            context.Request.Method = requestMethod;
            context.Request.Path   = requestUri;

            foreach (var(key, value) in requestHeader)
            {
                context.Request.Headers.Add(key, new[] { value });
            }

            _fakeMessageHandler.Setup(mh => mh.Send(It.IsAny <HttpRequestMessage>())).Returns(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("")
            });

            var client = new HttpClient(_fakeMessageHandler.Object);

            var feature = new MockResponseFeature();

            context.Features.Set <IHttpResponseFeature>(feature);

            async Task Next(HttpContext ctx)
            {
                Console.WriteLine(ctx.Response.Headers.Count);
                await feature.InvokeCallBack();
            }

            var nextMiddleware = new MiddlewareMock(Next);

            await new IdentityProvider.Middleware.IdentityProviderMiddleware(nextMiddleware.InvokeAsync,
                                                                             new IdentityProviderOptions
            {
                BaseAddress               = new Uri(DEFAULT_SYSTEM_BASE_URI),
                AllowExternalValidation   = allowExternalValidation,
                TenantInformationCallback = () => new TenantInformation {
                    TenantId = "0", SystemBaseUri = "https://localhost"
                },
                HttpClient = client
            })
            .Invoke(context);

            context.Response.StatusCode.Should().Be(expectedStatus);


            nextMiddleware.HasBeenInvoked.Should().BeTrue("next middleware should not have been invoked if signature is wrong");
            foreach (var(key, value) in expectedResponseHeader)
            {
                context.Response.Headers[key].ToString().Should().BeEquivalentTo(value);
            }
        }
Example #2
0
        /// <summary>
        /// Мок HTTP контекста.
        /// </summary>
        /// <param name="traceIdSettings">Настройки TraceId.</param>
        /// <returns>Мок HTTP контекста.</returns>
        private static DefaultHttpContext GetMockedContextFor(
            TraceIdSettings traceIdSettings,
            out MockResponseFeature feature
            )
        {
            feature = new MockResponseFeature();
            var context = new DefaultHttpContext();

            context.Features.Set <IHttpResponseFeature>(feature);
            if (!string.IsNullOrWhiteSpace(traceIdSettings.Header))
            {
                context.Request.Headers.Add(traceIdSettings.Header, TraceContext.Current.TraceId?.ToString());
            }

            if (!string.IsNullOrWhiteSpace(traceIdSettings.TraceIdSourceHeader))
            {
                context.Request.Headers.Add(traceIdSettings.TraceIdSourceHeader, TraceContext.Current.TraceIdSource);
            }

            return(context);
        }