Exemplo n.º 1
0
        public void ValidRequestFlowWorks()
        {
            var factory = new MockRepository(MockBehavior.Loose);
            var config  = factory.Create <Primitive.Config.IHttpConfig>();

            config.Setup(t => t.RequestTimeoutMs).Returns(1000);
            config.Setup(t => t.BaseUrl).Returns("http://www.test.com");
            var client  = factory.Create <IHttpClient>();
            var request = factory.Create <Base.Http.IRequest>();

            request.Setup(t => t.EndpointUri).Returns("/api/uri");
            var response = factory.Create <IResponse>();

            var instance = new Base.Http.Fetcher.HttpFetcher(config.Object, client.Object);

            client.Setup(t => t.SendAsync(It.IsAny <IRequest>())).ReturnsAsync(response.Object);

            var task = instance.ExecuteAsync(request.Object);

            task.Wait();

            client.Verify(t => t.SendAsync(It.IsAny <IRequest>()), Times.Once);
            request.Verify(t => t.EndpointUri, Times.Once);
            factory.VerifyAll();
        }
		public async Task VerifyTimeoutBehavior()
		{
			var factory = new MockRepository(MockBehavior.Loose);
			Mock<Primitive.Config.IHttpConfig> config = factory.Create<Primitive.Config.IHttpConfig>();
			config.Setup(t => t.RequestTimeoutMs).Returns(1000);
			config.Setup(t => t.BaseUrl).Returns("http://www.test.com");
			Mock<IHttpClient> client = factory.Create<IHttpClient>();
			Mock<IRequest> request = factory.Create<Base.Http.IRequest>();
			request.Setup(t => t.EndpointUri).Returns("/api/uri");
			Mock<IResponse> response = factory.Create<IResponse>();
			response.Setup(t => t.StatusCode).Returns(HttpStatusCode.ServiceUnavailable);

			var instance = new Base.Http.Fetcher.HttpFetcher(config.Object, client.Object);
			client.Setup(t => t.SendAsync(It.IsAny<IRequest>())).ThrowsAsync(new TaskCanceledException());

			await Assert.ThrowsAsync<Base.Http.Exception.ConnectionException>(
			    () => instance.ExecuteAsync(request.Object)
			);
		}
		public async Task EmptyBaseUriTriggersException()
		{
			var factory = new MockRepository(MockBehavior.Loose);
			Mock<Primitive.Config.IHttpConfig> config = factory.Create<Primitive.Config.IHttpConfig>();
			config.Setup(t => t.RequestTimeoutMs).Returns(1000);
			config.Setup(t => t.BaseUrl).Returns("http://www.test.com");
			Mock<IHttpClient> client = factory.Create<IHttpClient>();
			Mock<IRequest> request = factory.Create<Base.Http.IRequest>();
			request.Setup(t => t.EndpointUri).Returns("");

			var instance = new Base.Http.Fetcher.HttpFetcher(config.Object, client.Object);

			// should trigger InitException
			await Assert.ThrowsAsync<Base.Exception.InvalidValueException>(
			    () => instance.ExecuteAsync(request.Object)
			);

			factory.Verify();
		}
Exemplo n.º 4
0
        public async Task VerifyBehaviorForApiThrottling()
        {
            var factory = new MockRepository(MockBehavior.Loose);

            var config = factory.Create <Primitive.Config.IHttpConfig>();

            config.Setup(t => t.RequestTimeoutMs).Returns(1000);
            config.Setup(t => t.BaseUrl).Returns("http://www.test.com");
            var client  = factory.Create <IHttpClient>();
            var request = factory.Create <Base.Http.IRequest>();

            request.Setup(t => t.EndpointUri).Returns("/api/uri");
            var response = factory.Create <IResponse>();

            response.Setup(t => t.StatusCode).Returns((System.Net.HttpStatusCode) 429);

            var instance = new Base.Http.Fetcher.HttpFetcher(config.Object, client.Object);

            client.Setup(t => t.SendAsync(It.IsAny <IRequest>())).ReturnsAsync(response.Object);

            await Assert.ThrowsAsync <Base.Http.Exception.ThrottleException>(
                () => instance.ExecuteAsync(request.Object)
                );
        }