Пример #1
0
        public async Task ExecuteAsync_ResponseNotOk_ReturnsFail()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;
            handler.SetupRequest(HttpMethod.Options, $"{baseUri}test/")
            .ReturnsResponse(HttpStatusCode.BadRequest);

            var factory  = handler.CreateClientFactory();
            var policies = Policy.WrapAsync(Policy.NoOpAsync <HttpResponseMessage>(), Policy.NoOpAsync <HttpResponseMessage>());

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "OPTIONS", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory, policies);
            var result = await step.ExecuteAsync();

            Assert.AreEqual(ExecutionStatus.Fail, result);

            // Will already be disposed but non-breaking call suppresses warning
            client.Dispose();
        }
Пример #2
0
        public async Task ExecuteAsync_ReuseHttpMessageHandler_ExceptionThrown_Throws()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;
            handler.SetupRequest(HttpMethod.Delete, $"{baseUri}test/")
            .Throws(new Exception("test exception"));

            var factory = handler.CreateClientFactory();

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "DELETE", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory);

            await Assert.ThrowsExceptionAsync <Exception>(
                () => step.ExecuteAsync());

            client.Dispose();
        }
Пример #3
0
        public async Task ExecuteAsync_PoliciesNull_Asynchronous_TaskFaulted_ReturnsSuccess()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;

            handler.SetupRequest(HttpMethod.Get, $"{baseUri}test/")
            .Throws(new Exception("test exception"));

            var factory = handler.CreateClientFactory();

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory, null);

            var result = await step.ExecuteAsync();

            Assert.AreEqual(ExecutionStatus.Success, result);

            // Will already be disposed but non-breaking call suppresses warning
            client.Dispose();
        }
Пример #4
0
        public async Task ExecuteAsync_ReuseHttpMessageHandler_ResponseNotOk_ReturnsFail()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;
            handler.SetupRequest(HttpMethod.Trace, $"{baseUri}test/")
            .ReturnsResponse(HttpStatusCode.BadRequest);

            var factory = handler.CreateClientFactory();

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "TRACE", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory);
            var result = await step.ExecuteAsync();

            Assert.AreEqual(ExecutionStatus.Fail, result);
            client.Dispose();
        }
Пример #5
0
        public async Task ExecuteAsync_ThrowsException_Throws()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;

            handler.SetupRequest(HttpMethod.Get, $"{baseUri}test/")
            .Throws(new Exception("test exception"));

            var factory  = handler.CreateClientFactory();
            var policies = Policy.WrapAsync(Policy.NoOpAsync <HttpResponseMessage>(), Policy.NoOpAsync <HttpResponseMessage>());

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory, policies);

            await Assert.ThrowsExceptionAsync <Exception>(
                () => step.ExecuteAsync());

            // Will already be disposed but non-breaking call suppresses warning
            client.Dispose();
        }
Пример #6
0
        public void write_simple_content()
        {
            var step = new RequestStep(15, new object());

            var tag = new RequestStepTag(step, "some content");

            tag.Id().ShouldEqual(step.Id.ToString());
        }
        public RequestStepTag(RequestStep step, string content) : base("div")
        {
            AddClass("row-fluid");
            Id(step.Id.ToString());

            Text(content).Encoded(false);

            this.PrependAnchor();
        }
Пример #8
0
        public RequestStepTag(RequestStep step, string content) : base("div")
        {
            AddClass("row-fluid");
            Id(step.Id.ToString());

            Text(content).Encoded(false);

            this.PrependAnchor();
        }
Пример #9
0
        public void Deserialization_RequiredData_CreatesValidInstance()
        {
            RequestStep step = JsonConvert.DeserializeObject <RequestStep>(
                "{ client : 'testClient', method : 'get', path : 'test/', size : 128, reuseSockets : true, trueAsync : false }");

            Assert.IsFalse(step.Asynchrounous);
            Assert.IsNull(step.CacheId);
            Assert.AreEqual(0, step.CacheUniqueness);
            Assert.AreEqual("testClient", step.ClientName);
            Assert.AreEqual("get", step.Method);
            Assert.AreEqual("test/", step.Path);
            Assert.AreEqual(128, step.PayloadSize);
            Assert.AreEqual(true, step.ReuseHttpMessageHandler);
        }
Пример #10
0
        public void Configure_Factory_NullFactory_Throws()
        {
            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;

            Assert.ThrowsException <ArgumentNullException>(
                () => step.Configure(null));
        }
Пример #11
0
        public async Task ExecuteAsync_Asynchronous_PostLargePayload_ResponseOk_ReturnsSuccess()
        {
            Uri baseUri = new Uri("http://test.com/");
            var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var client  = handler.CreateClient();

            client.BaseAddress = baseUri;
            string url = $"{baseUri}test/";

            handler.SetupRequest(HttpMethod.Post, url)
            .ReturnsResponse(HttpStatusCode.OK);

            var factory  = handler.CreateClientFactory();
            var policies = Policy.WrapAsync(Policy.NoOpAsync <HttpResponseMessage>(), Policy.NoOpAsync <HttpResponseMessage>());

            Mock.Get(factory)
            .Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns(() => client);

            var step = new RequestStep()
            {
                Asynchrounous           = true,
                ClientName              = "testClient",
                Method                  = "POST",
                Path                    = "test/",
                PayloadSize             = 4097,
                ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory, policies);

            var result = await step.ExecuteAsync();

            // Verify success
            Assert.AreEqual(ExecutionStatus.Success, result);

            // Verify call had correct content
            handler.VerifyRequest(HttpMethod.Post, url, async r =>
            {
                var content = await r.Content.ReadAsAsync <AdaptableRequest>();
                var payload = content.Payload.ToList();
                return(payload.Count == 33 && payload[32].Length == 1);
            });

            // Will already be disposed but non-breaking call suppresses warning
            client.Dispose();
        }
Пример #12
0
        public void Configure_Factory_ReuseHttpMessageHandlerFalse_Throws()
        {
            Mock <IHttpClientFactory> factory = new Mock <IHttpClientFactory>(MockBehavior.Strict);
            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;

            Assert.ThrowsException <InvalidOperationException>(
                () => step.Configure(factory.Object));
        }
Пример #13
0
        public void Configure_Local_PoliciesNull_SetsConfigured()
        {
            var factory = new Mock <IHttpClientFactory>(MockBehavior.Strict);

            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;

            step.Configure(factory.Object, null);
            Assert.IsTrue(step.Configured);
        }
Пример #14
0
        public void Configure_Local_FactoryNull_Throws()
        {
            var policies = Policy.WrapAsync(Policy.NoOpAsync <HttpResponseMessage>(), Policy.NoOpAsync <HttpResponseMessage>());

            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = false
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;

            Assert.ThrowsException <ArgumentNullException>(
                () => step.Configure(null, policies));
        }
Пример #15
0
        public void Configure_Factory_LoggerNotInitialized_Throws()
        {
            Mock <IHttpClientFactory> factory = new Mock <IHttpClientFactory>(MockBehavior.Strict);
            var step = new RequestStep()
            {
                Asynchrounous           = true,
                ClientName              = "testClient",
                Method                  = "GET",
                Path                    = "test/",
                PayloadSize             = 16,
                ReuseHttpMessageHandler = true
            };

            Assert.ThrowsException <InvalidOperationException>(
                () => step.Configure(factory.Object));
        }
        private OutlineNodeTag addNode(RequestStep step)
        {
            string title = null;

            if (step.Log is StringMessage)
            {
                title = step.Log.As<StringMessage>().Message;
            }
            else
            {
                var description = Description.For(step.Log);
                title = description.Title;
            }

            return AddNode(title, step.Id.ToString());
        }
Пример #17
0
        public async Task ExecuteAsync_LoggerNotInitialized_Throws()
        {
            var factory = new Mock <IHttpClientFactory>(MockBehavior.Strict);

            factory.Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns <HttpClient>(null);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };

            await Assert.ThrowsExceptionAsync <InvalidOperationException>(
                () => step.ExecuteAsync());
        }
Пример #18
0
        public void Configure_Local_ReuseHttpMessageHandlerTrue_Throws()
        {
            var factory  = new Mock <IHttpClientFactory>(MockBehavior.Strict);
            var policies = Policy.WrapAsync(Policy.NoOpAsync <HttpResponseMessage>(), Policy.NoOpAsync <HttpResponseMessage>());

            var step = new RequestStep()
            {
                Asynchrounous = true, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;

            Assert.ThrowsException <InvalidOperationException>(
                () => step.Configure(factory.Object, policies));
        }
Пример #19
0
        public async Task ExecuteAsync_ReuseClient_ClientNull_Throws()
        {
            var factory = new Mock <IHttpClientFactory>(MockBehavior.Strict);

            factory.Setup(x => x.CreateClient(It.IsAny <string>()))
            .Returns <HttpClient>(null);

            var step = new RequestStep()
            {
                Asynchrounous = false, ClientName = "testClient", Method = "GET", Path = "test/",
                PayloadSize   = 16, ReuseHttpMessageHandler = true
            };
            var logger = new Mock <ILogger>(MockBehavior.Loose);

            step = step.AsTypeModel(logger.Object) as RequestStep;
            step.Configure(factory.Object);

            await Assert.ThrowsExceptionAsync <InvalidOperationException>(
                () => step.ExecuteAsync());
        }
Пример #20
0
 public RequestStepTag VisualizeStep(RequestStep step)
 {
     return(new RequestStepTag(step, contentFor(step.Log).ToString()));
 }