当前位置: 首页>>代码示例>>C#>>正文


C# HttpRequestMessage.SetRequestContext方法代码示例

本文整理汇总了C#中System.Net.Http.HttpRequestMessage.SetRequestContext方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.SetRequestContext方法的具体用法?C# HttpRequestMessage.SetRequestContext怎么用?C# HttpRequestMessage.SetRequestContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpRequestMessage的用法示例。


在下文中一共展示了HttpRequestMessage.SetRequestContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DropboxWebHookReceiverTests

        public DropboxWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();
            _settings["MS_WebHookReceiverSecret_Dropbox"] = TestSecret;

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _receiverMock = new Mock<DropboxWebHookReceiver> { CallBase = true };

            _getRequest = new HttpRequestMessage();
            _getRequest.SetRequestContext(_context);

            _postRequest = new HttpRequestMessage() { Method = HttpMethod.Post };
            _postRequest.SetRequestContext(_context);
            _postRequest.Content = new StringContent(TestContent, Encoding.UTF8, "application/json");

            byte[] secret = Encoding.UTF8.GetBytes(TestSecret);
            using (var hasher = new HMACSHA256(secret))
            {
                byte[] data = Encoding.UTF8.GetBytes(TestContent);
                byte[] testHash = hasher.ComputeHash(data);
                _testSignature = EncodingUtilities.ToHex(testHash);
            }
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:25,代码来源:DropboxWebHookReceiverTests.cs

示例2: SendAsync_SetsCurrentPrincipalToAnonymous_BeforeCallingInnerHandler

        public void SendAsync_SetsCurrentPrincipalToAnonymous_BeforeCallingInnerHandler()
        {
            // Arrange
            IPrincipal requestContextPrincipal = null;
            Mock<HttpRequestContext> requestContextMock = new Mock<HttpRequestContext>();
            requestContextMock
                .SetupSet(c => c.Principal = It.IsAny<IPrincipal>())
                .Callback<IPrincipal>((value) => requestContextPrincipal = value);
            IPrincipal principalBeforeInnerHandler = null;
            HttpMessageHandler inner = new LambdaHttpMessageHandler((ignore1, ignore2) =>
            {
                principalBeforeInnerHandler = requestContextPrincipal;
                return Task.FromResult<HttpResponseMessage>(null);
            });
            HttpMessageHandler handler = CreateProductUnderTest(inner);

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.SetRequestContext(requestContextMock.Object);

                // Act
                handler.SendAsync(request, CancellationToken.None);
            }

            // Assert
            Assert.NotNull(principalBeforeInnerHandler);
            IIdentity identity = principalBeforeInnerHandler.Identity;
            Assert.NotNull(identity);
            Assert.False(identity.IsAuthenticated);
            Assert.Null(identity.Name);
            Assert.Null(identity.AuthenticationType);
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:32,代码来源:SuppressHostPrincipalMessageHandlerTest.cs

示例3: TrelloWebHookReceiverTests

        public TrelloWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();
            _settings["MS_WebHookReceiverSecret_Trello"] = TestSecret;

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _receiverMock = new Mock<TrelloWebHookReceiver> { CallBase = true };

            _headRequest = new HttpRequestMessage() { Method = HttpMethod.Head };
            _headRequest.SetRequestContext(_context);

            _postRequest = new HttpRequestMessage(HttpMethod.Post, TestAddress);
            _postRequest.SetRequestContext(_context);
            _postRequest.Content = new StringContent(TestContent, Encoding.UTF8, "application/json");

            byte[] secret = Encoding.UTF8.GetBytes(TestSecret);
            using (var hasher = new HMACSHA1(secret))
            {
                byte[] data = Encoding.UTF8.GetBytes(TestContent);
                byte[] requestUri = Encoding.UTF8.GetBytes(TestAddress);
                byte[] combo = new byte[data.Length + requestUri.Length];
                Buffer.BlockCopy(data, 0, combo, 0, data.Length);
                Buffer.BlockCopy(requestUri, 0, combo, data.Length, requestUri.Length);
                byte[] testHash = hasher.ComputeHash(combo);
                _signature = EncodingUtilities.ToBase64(testHash, uriSafe: false);
            }
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:29,代码来源:TrelloWebHookReceiverTests.cs

示例4: CreateHttpRequestMessage

        private static HttpRequestMessage CreateHttpRequestMessage(HttpActionDescriptor actionDescriptor, ODataRoute oDataRoute, HttpConfiguration httpConfig)
        {
            Contract.Requires(httpConfig != null);
            Contract.Requires(oDataRoute != null);
            Contract.Requires(httpConfig != null);
            Contract.Ensures(Contract.Result<HttpRequestMessage>() != null);

            Contract.Assume(oDataRoute.Constraints != null);

            var httpRequestMessage = new HttpRequestMessage(actionDescriptor.SupportedHttpMethods.First(), "http://any/");

            var requestContext = new HttpRequestContext
            {
                Configuration = httpConfig
            };
            httpRequestMessage.SetConfiguration(httpConfig);
            httpRequestMessage.SetRequestContext(requestContext);

            var httpRequestMessageProperties = httpRequestMessage.ODataProperties();
            Contract.Assume(httpRequestMessageProperties != null);
            httpRequestMessageProperties.Model = oDataRoute.GetEdmModel();
            httpRequestMessageProperties.RouteName = oDataRoute.GetODataPathRouteConstraint().RouteName;
            httpRequestMessageProperties.RoutingConventions = oDataRoute.GetODataPathRouteConstraint().RoutingConventions;
            httpRequestMessageProperties.PathHandler = oDataRoute.GetODataPathRouteConstraint().PathHandler;
            return httpRequestMessage;
        }
开发者ID:bigred8982,项目名称:Swashbuckle.OData,代码行数:26,代码来源:AttributeRouteStrategy.cs

示例5: StripeWebHookReceiverTests

        public StripeWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();
            _settings["MS_WebHookReceiverSecret_Stripe"] = TestSecret;

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _stripeResponse = new HttpResponseMessage();
            _stripeResponse.Content = new StringContent("{ \"type\": \"action\" }", Encoding.UTF8, "application/json");

            _handlerMock = new HttpMessageHandlerMock();
            _handlerMock.Handler = (req, counter) =>
            {
                string expected = string.Format(CultureInfo.InvariantCulture, StripeWebHookReceiver.EventUriTemplate, TestId);
                Assert.Equal(req.RequestUri.AbsoluteUri, expected);
                return Task.FromResult(_stripeResponse);
            };

            _httpClient = new HttpClient(_handlerMock);
            _receiverMock = new Mock<StripeWebHookReceiver>(_httpClient) { CallBase = true };

            _postRequest = new HttpRequestMessage { Method = HttpMethod.Post };
            _postRequest.SetRequestContext(_context);
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:25,代码来源:StripeWebHookReceiverTests.cs

示例6: Authenticated_POST_are_allowed

 public async Task Authenticated_POST_are_allowed()
 {
     var req = new HttpRequestMessage(HttpMethod.Post, "https://www.example.net/resource");
     req.SetRequestContext(new HttpRequestContext
     {
         Principal = new ClaimsPrincipal(new GenericIdentity("Alice"))
     });
     var res = await _client.SendAsync(req);
     Assert.Equal(HttpStatusCode.OK, res.StatusCode);
 }
开发者ID:joergjo,项目名称:WebApiBook.Security,代码行数:10,代码来源:AuthorizeAttributeFacts.cs

示例7: WebHookReceiverTests

        public WebHookReceiverTests()
        {
            _config = new HttpConfiguration();
            _resolverMock = new Mock<IDependencyResolver>();
            _config.DependencyResolver = _resolverMock.Object;
            WebHooksConfig.Initialize(_config);
            _request = new HttpRequestMessage();
            _receiverMock = new WebHookReceiverMock();

            _context = new HttpRequestContext();
            _context.Configuration = _config;
            _request.SetRequestContext(_context);
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:13,代码来源:WebHookReceiverTests.cs

示例8: Can_make_uri_for_v2

 public void Can_make_uri_for_v2()
 {
     var req = new HttpRequestMessage(HttpMethod.Get, "http://example.org/MultipleRouteTests/v2");
     req.SetRequestContext(new HttpRequestContext
     {
         VirtualPathRoot = "/"
     });
     req.SetRouteData(_config.Routes.GetRouteData(req));
     req.SetConfiguration(_config);
     var uriMaker = _factory.NewUriMakerFor<MultipleRouteTests.MultipleRouteResourceController>(req);
     var uri = uriMaker.UriFor(c => c.GetById(123));
     Assert.Equal("http://example.org/MultipleRouteTests/v2/123", uri.ToString());
 }
开发者ID:hid-shibayama,项目名称:drum,代码行数:13,代码来源:MultipleRouteTests.cs

示例9: SlackWebHookReceiverTests

        public SlackWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();
            _settings["MS_WebHookReceiverSecret_Slack"] = TestSecret;

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _receiverMock = new Mock<SlackWebHookReceiver> { CallBase = true };

            _postRequest = new HttpRequestMessage(HttpMethod.Post, "https://some.ssl.host");
            _postRequest.SetRequestContext(_context);
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:13,代码来源:SlackWebHookReceiverTests.cs

示例10: PaypalWebHookReceiverTests

        public PaypalWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _receiverMock = new Mock<PaypalWebHookReceiver>(false) { CallBase = true };

            _postRequest = new HttpRequestMessage() { Method = HttpMethod.Post };
            _postRequest.SetRequestContext(_context);
            _postRequest.Content = new StringContent(TestContent, Encoding.UTF8, "application/json");
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:13,代码来源:PaypalWebHookReceiverTests.cs

示例11: ExecuteThrowsWhenRequestContextHasNoConfiguration

        public void ExecuteThrowsWhenRequestContextHasNoConfiguration(HttpRequestMessage request, HttpStatusCode status,
            IJSendResponse response)
        {
            // Fixture setup
            var requestContext = new HttpRequestContext();
            request.SetRequestContext(requestContext);

            var result = new JSendResult<IJSendResponse>(status, response, request);
            // Exercise system and verify outcome
            result.Awaiting(r => r.ExecuteAsync(CancellationToken.None))
                .ShouldThrow<InvalidOperationException>()
                .WithMessage("HttpRequestContext.Configuration must not be null.");
        }
开发者ID:lofrank,项目名称:JSendWebApi,代码行数:13,代码来源:JSendResultTests.cs

示例12: WebHookQueueHandlerTests

 public WebHookQueueHandlerTests()
 {
     _queueHandlerMock = new Mock<WebHookQueueHandler>() { CallBase = true };
     _data = new object();
     HttpConfiguration config = new HttpConfiguration();
     HttpRequestContext context = new HttpRequestContext { Configuration = config, };
     HttpRequestMessage request = new HttpRequestMessage();
     request.SetRequestContext(context);
     _context = new WebHookHandlerContext(_actions)
     {
         Data = _data,
         Request = request
     };
 }
开发者ID:itomek,项目名称:WebHooks,代码行数:14,代码来源:WebHookQueueHandlerTests.cs

示例13: MailChimpWebHookReceiverTests

        public MailChimpWebHookReceiverTests()
        {
            _settings = new SettingsDictionary();
            _settings["MS_WebHookReceiverSecret_MailChimp"] = TestSecret;

            _config = HttpConfigurationMock.Create(new Dictionary<Type, object> { { typeof(SettingsDictionary), _settings } });
            _context = new HttpRequestContext { Configuration = _config };

            _receiverMock = new Mock<MailChimpWebHookReceiver> { CallBase = true };

            _postRequest = new HttpRequestMessage(HttpMethod.Post, TestHost);
            _postRequest.SetRequestContext(_context);

            _getRequest = new HttpRequestMessage(HttpMethod.Get, TestHost);
            _getRequest.SetRequestContext(_context);
        }
开发者ID:Joshzx,项目名称:WebHooks,代码行数:16,代码来源:MailChimpWebHookReceiverTests.cs

示例14: CopyBatchRequestProperties_SetsRequestContextWithUrlHelperForSubRequest

        public void CopyBatchRequestProperties_SetsRequestContextWithUrlHelperForSubRequest()
        {
            // Arrange
            using (HttpRequestMessage subRequest = new HttpRequestMessage())
            using (HttpRequestMessage batchRequest = new HttpRequestMessage())
            {
                subRequest.SetRequestContext(new HttpRequestContext());

                // Act
                BatchHttpRequestMessageExtensions.CopyBatchRequestProperties(subRequest, batchRequest);

                // Assert
                HttpRequestContext context = subRequest.GetRequestContext();
                Assert.NotNull(context);
                Assert.NotNull(context.Url);
                Assert.Same(subRequest, context.Url.Request);
            }
        }
开发者ID:ahmetgoktas,项目名称:aspnetwebstack,代码行数:18,代码来源:BatchHttpRequestMessageExtensionsTest.cs

示例15: SendAsync_Throws_WhenOwinContextIsNull

        public void SendAsync_Throws_WhenOwinContextIsNull()
        {
            // Arrange
            HttpMessageHandler innerHandler = CreateStubHandler();
            HttpMessageHandler handler = CreateProductUnderTest(innerHandler);

            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                request.SetRequestContext(new HttpRequestContext());

                // Act & Assert
                InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() =>
                {
                    HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
                });
                Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message);
            }
        }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:18,代码来源:PassiveAuthenticationMessageHandlerTest.cs


注:本文中的System.Net.Http.HttpRequestMessage.SetRequestContext方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。