Mock-Response for testing purposes.
Inheritance: BaseClientService
コード例 #1
0
        /// <summary>Creates a client service for the given features.</summary>
        private IClientService CreateClientService(Features? features = null)
        {
            var client = new MockClientService();
            if (features.HasValue)
            {
                client.SetFeatures(new[] { Utilities.GetEnumStringValue(features.Value) });
            }

            return client;
        }
コード例 #2
0
        public void TestGetWithUrlMaxLengthDisabled()
        {
            var query = "q=" + new String('x', 5000) + "&x=" + new String('y', 6000);
            var uri = "http://www.example.com/";
            var requestUri = uri + "?" + query;
            var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            var messageHandler = new MockMessageHandler();
            var initializer = (new BaseClientService.Initializer
            {
                HttpClientFactory = new MockHttpClientFactory(messageHandler),
                MaxUrlLength = 0
            });

            using (var service = new MockClientService(initializer))
            {
                service.HttpClient.SendAsync(request);
                // Confirm the request was not modified.
                Assert.That(request.RequestUri.ToString().Length, Is.EqualTo(requestUri.Length));
                Assert.That(request.Method, Is.EqualTo(HttpMethod.Get));
                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)));
            }
        }
コード例 #3
0
 public void TestGetWithUrlTooLongByDefault()
 {
     // Build a query string such that the whole URI adds up to 2049 characters
     var query = "q=" + new String('x', 1020) + "&x=" + new String('y', 1000);
     var uri = "http://www.example.com/";
     var requestUri = uri + "?" + query;
     var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
     var messageHandler = new MockMessageHandler();
     using (var service = new MockClientService(new BaseClientService.Initializer()
         {
             HttpClientFactory = new MockHttpClientFactory(messageHandler)
         }))
     {
         service.HttpClient.SendAsync(request);
         // Confirm the test URI is one character too long.
         Assert.That(requestUri.Length, Is.EqualTo(DefaultMaxUrlLength + 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(messageHandler.RequestContent, Is.EqualTo(query));
     }
 }
コード例 #4
0
        public void Constructor_DefaultValues()
        {
            var service = new MockClientService(new BaseClientService.Initializer());
            Assert.NotNull(service.HttpClient);
            Assert.Null(service.HttpClientInitializer);
            Assert.True(service.GZipEnabled);

            // Back-off handler for unsuccessful response (503) is added by default.
            Assert.That(service.HttpClient.MessageHandler.UnsuccessfulResponseHandlers.Count, Is.EqualTo(1));
            Assert.That(service.HttpClient.MessageHandler.UnsuccessfulResponseHandlers[0],
                Is.InstanceOf<BackOffHandler>());

            // An execute interceptors is expected (for handling GET requests with URLs that are too long)
            Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors.Count, Is.EqualTo(1));
            Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors[0],
                Is.InstanceOf<MaxUrlLengthInterceptor>());
        }
コード例 #5
0
        public void TestDeserializationString()
        {
            const string Response = @"{""kind"":""urlshortener#url"",""longUrl"":""http://google.com/""}";

            MockClientService client = new MockClientService();

            // Check that the response is decoded correctly
            var stream = new MemoryStream(Encoding.Default.GetBytes(Response));
            var response = new HttpResponseMessage { Content = new StreamContent(stream) };
            string result = client.DeserializeResponse<string>(response).Result;
            Assert.AreEqual(Response, result);
        }
        public void Constructor_DefaultValues()
        {
            var service = new MockClientService(new BaseClientService.Initializer());
            Assert.NotNull(service.HttpClient);
            Assert.Null(service.HttpClientInitializer);
            Assert.True(service.GZipEnabled);

            // Disable "<member> is obsolete" warning for these uses.
            // MessageHandler no longer provides a supported way for clients to query the list of handlers,
            // but we rely on the obsolete property as an implementation detail here.
            #pragma warning disable 618
            // Back-off handler for unsuccessful response (503) is added by default.
            Assert.That(service.HttpClient.MessageHandler.UnsuccessfulResponseHandlers.Count, Is.EqualTo(1));
            Assert.That(service.HttpClient.MessageHandler.UnsuccessfulResponseHandlers.FirstOrDefault(),
                Is.InstanceOf<BackOffHandler>());

            // An execute interceptors is expected (for handling GET requests with URLs that are too long)
            Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors.Count, Is.EqualTo(1));
            Assert.That(service.HttpClient.MessageHandler.ExecuteInterceptors.FirstOrDefault(),
                Is.InstanceOf<MaxUrlLengthInterceptor>());
            #pragma warning restore 618
        }