/// <summary>
 /// Helper for regression tests that URLs less than or equal to 2048 characters, or requests that are not GET,
 /// are not modified. If you are testing with GET, the length should be less than 2048.
 /// </summary>
 private void SubtestGetWithUrlOkayLengthByDefault(uint length, HttpMethod method)
 {
     var requestUri = "http://www.example.com/?q=" + new String('x', (int)length - 26);
     var request = new HttpRequestMessage(method, requestUri);
     var mockHandler = new MockMessageHandler();
     var handler = new ConfigurableMessageHandler(mockHandler);
     handler.AddExecuteInterceptor(new MaxUrlLengthInterceptor(DefaultMaxUrlLength));
     using (var httpClient = new HttpClient(handler))
     {
         httpClient.SendAsync(request);
         Assert.That(request.RequestUri.ToString().Length, Is.EqualTo(length));
         Assert.That(request.Method, Is.EqualTo(method));
         Assert.That(request.Headers.Contains("X-HTTP-Method-Override"), Is.False);
         Assert.That(request.Content, Is.Null);
         Assert.That(request.RequestUri, Is.EqualTo(new Uri(requestUri)));
     }
 }
        public void SendAsync_ExecuteInterceptor_AbnormalResponse_UnsuccessfulResponseHandler()
        {
            var handler = new InterceptorMessageHandler();
            handler.InjectedResponseMessage = new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.ServiceUnavailable
                };

            var configurableHanlder = new ConfigurableMessageHandler(handler);
            var interceptor = new InterceptorMessageHandler.Interceptor();
            configurableHanlder.AddExecuteInterceptor(interceptor);
            configurableHanlder.AddUnsuccessfulResponseHandler(new TrueUnsuccessfulResponseHandler());

            using (var client = new HttpClient(configurableHanlder))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-execute-interceptor");

                HttpResponseMessage response = client.SendAsync(request).Result;
                Assert.That(interceptor.Calls, Is.EqualTo(configurableHanlder.NumTries));
                Assert.That(handler.Calls, Is.EqualTo(configurableHanlder.NumTries));
            }
        }
        /// <summary>Tests that execute interceptor is called.</summary>
        private void SubtestSendAsyncExecuteInterceptor(HttpStatusCode code)
        {
            var handler = new InterceptorMessageHandler();
            handler.InjectedResponseMessage = new HttpResponseMessage()
                {
                    StatusCode = code
                };

            var configurableHanlder = new ConfigurableMessageHandler(handler);
            var interceptor = new InterceptorMessageHandler.Interceptor();
            configurableHanlder.AddExecuteInterceptor(interceptor);

            using (var client = new HttpClient(configurableHanlder))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-execute-interceptor");

                HttpResponseMessage response = client.SendAsync(request).Result;
                Assert.That(interceptor.Calls, Is.EqualTo(1));
                Assert.That(handler.Calls, Is.EqualTo(1));
            }
        }
 public void TestGetPathQueryWithUrlOkayLengthByDefault()
 {
     uint maxUrlLength = 1000;
     var query = "q=" + new String('x', 1000);
     var uri = "http://www.googleapis.com/language/translate/v2";
     var requestUri = uri + "?" + query;
     var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
     var mockHandler = new MockMessageHandler();
     var handler = new ConfigurableMessageHandler(mockHandler);
     handler.AddExecuteInterceptor(new MaxUrlLengthInterceptor(maxUrlLength));
     using (var httpClient = new HttpClient(handler))
     {
         httpClient.SendAsync(request);
         // Confirm the request was modified correctly, and the uri was set correctly.
         Assert.That(request.RequestUri, Is.EqualTo(new Uri(uri)));
         Assert.That(mockHandler.RequestContent, Is.EqualTo(query));
     }
 }
 public void TestGetWithUrlTooLongByUserSpecified()
 {
     uint maxUrlLength = 1000;
     var query = "q=" + new String('x', 900) + "&x=" + new String('y', 72);
     var uri = "http://www.example.com/";
     var requestUri = uri + "?" + query;
     var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
     var mockHandler = new MockMessageHandler();
     var handler = new ConfigurableMessageHandler(mockHandler);
     handler.AddExecuteInterceptor(new MaxUrlLengthInterceptor(maxUrlLength));
     using (var httpClient = new HttpClient(handler))
     {
         httpClient.SendAsync(request);
         // Confirm the test URI is one character too long.
         Assert.That(requestUri.Length, Is.EqualTo(maxUrlLength + 1));
         // Confirm the request was modified correctly:
         Assert.That(request.Method, Is.EqualTo(HttpMethod.Post));
         Assert.That(request.Headers.GetValues("X-HTTP-Method-Override").Single(), Is.EqualTo("GET"));
         Assert.That(request.Content.Headers.ContentType,
             Is.EqualTo(new MediaTypeHeaderValue("application/x-www-form-urlencoded")));
         Assert.That(request.RequestUri, Is.EqualTo(new Uri(uri)));
         Assert.That(mockHandler.RequestContent, Is.EqualTo(query));
     }
 }