コード例 #1
0
        public async Task ShouldSucceedIfAllEndpointsAreOnline()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith("etc 1.2.3");
                http.RespondWith("etc 1.2.3");
                http.RespondWith("etc 1.2.3");
                http.RespondWith("etc 1.2.3");
                http.RespondWith("etc 1.2.3");
                http.RespondWith("etc 1.2.3");

                var epool = await CreateSut(VerificationStrategy)
                    .VerifyAndBuild(Uris);

                epool.Should().NotBeNull();

                epool.OnlineEndpoints.Should().HaveSameCount(Uris);

                for (var i = 0; i < Uris.Length; i++)
                {
                    var endpoint = epool.OnlineEndpoints[i];
                    endpoint.Uri
                            .Should()
                            .BeSameAs(Uris[i]);
                }
            }
        }
コード例 #2
0
ファイル: GetLeaderTests.cs プロジェクト: huoxudong125/Draft
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith(Fixtures.Statistics.LeaderResponse);

                var response = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                    .Statistics
                    .GetLeaderStatistics();

                http.Should()
                    .HaveCalled(Fixtures.EtcdUrl.AppendPathSegment(Constants.Etcd.Path_Stats_Leader))
                    .WithVerb(HttpMethod.Get)
                    .Times(1);

                response.Should().NotBeNull();

                response.Leader.Should().NotBeNullOrWhiteSpace();
                response.Followers.Should().NotBeNull()
                    .And.HaveCount(2);
                response.Followers.Keys.OrderBy(x => x).Should()
                    .HaveCount(2)
                    .And.ContainInOrder("6e3bd23ae5f1eae0", "a8266ecf031671f3");
            }
        }
コード例 #3
0
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWithJson(Fixtures.Cluster.CreateResponse(Fixtures.EtcdUrl.ToUri()));

                var member = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                                       .Cluster
                                       .CreateMember()
                                       .WithPeerUri(Fixtures.EtcdUrl.ToUri());

                http.Should()
                    .HaveCalled(
                        Fixtures.EtcdUrl
                                .AppendPathSegment(Constants.Etcd.Path_Members)
                    )
                    .WithVerb(HttpMethod.Post)
                    .WithContentType(Constants.Http.ContentType_ApplicationJson)
                    .Times(1);

                member.Should().NotBeNull();

                member.PeerUrls.Should()
                      .NotBeEmpty()
                      .And
                      .ContainSingle(x => Fixtures.EtcdUrl.ToUri().Equals(x));
            }
        }
コード例 #4
0
        public void Watch_ShouldRetryOnTimeoutException()
        {
            using (var http = new HttpTest())
            {
                http.SimulateTimeout()
                    .SimulateTimeout()
                    .SimulateTimeout()
                    .RespondWithJson(Fixtures.Watch.DefaultResponse);

                Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                    .Watch(Fixtures.Watch.Path)
                    .SubscribeFor(1)
                    .Wait();

                http.Should()
                    .HaveCalled(
                        Fixtures.EtcdUrl
                            .AppendPathSegment(Constants.Etcd.Path_Keys)
                            .AppendPathSegment(Fixtures.Watch.Path)
                            .SetQueryParam(Constants.Etcd.Parameter_Wait, Constants.Etcd.Parameter_True)
                    )
                    .WithVerb(HttpMethod.Get)
                    .Times(4);
            }
        }
コード例 #5
0
ファイル: GetLeaderTests.cs プロジェクト: huoxudong125/Draft
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWithJson(Fixtures.Cluster.ClusterMemberResponse(new[] {Fixtures.EtcdUrl.ToUri()}, new[] {Fixtures.EtcdUrl.ToUri()}));

                var leader = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                                       .Cluster
                                       .GetLeader();

                http.Should()
                    .HaveCalled(
                        Fixtures.EtcdUrl
                                .AppendPathSegment(Constants.Etcd.Path_Members_Leader)
                    )
                    .WithVerb(HttpMethod.Get)
                    .Times(1);

                leader.Should().NotBeNull();

                leader.PeerUrls.Should()
                      .NotBeEmpty()
                      .And
                      .ContainSingle(x => Fixtures.EtcdUrl.ToUri().Equals(x));

                leader.ClientUrls.Should()
                      .NotBeEmpty()
                      .And
                      .ContainSingle(x => Fixtures.EtcdUrl.ToUri().Equals(x));
            }
        }
コード例 #6
0
ファイル: GetStoreTests.cs プロジェクト: huoxudong125/Draft
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith(Fixtures.Statistics.StoreResponse);

                var response = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                    .Statistics
                    .GetStoreStatistics();

                http.Should()
                    .HaveCalled(Fixtures.EtcdUrl.AppendPathSegment(Constants.Etcd.Path_Stats_Store))
                    .WithVerb(HttpMethod.Get)
                    .Times(1);

                response.Should().NotBeNull();
                response.CreateSuccess.Should().Be(2);
                response.GetsFail.Should().Be(4);
                response.GetsSuccess.Should().Be(75);
                response.SetsFail.Should().Be(2);
                response.SetsSuccess.Should().Be(4);
                response.CompareAndDeleteFail.Should().Be(0);
                response.CompareAndDeleteSuccess.Should().Be(0);
                response.CompareAndSwapFail.Should().Be(0);
                response.CompareAndSwapSuccess.Should().Be(0);
                response.DeleteSuccess.Should().Be(0);
                response.DeleteFail.Should().Be(0);
                response.ExpireCount.Should().Be(0);
                response.Watchers.Should().Be(0);
            }
        }
コード例 #7
0
        public void CreateService()
        {
            using (var httpTest = new HttpTest())
            {
                var response = new HttpResponseMessage(HttpStatusCode.Created);
                response.Headers.Location = "http://api.com".AppendPathSegments("services", "service-id").ToUri();
                httpTest.ResponseQueue.Enqueue(response);

                var service = new ServiceDefinition("service-name", "flavor-id", "www.example.com", "example.com")
                {
                    Caches =
                    {
                        new ServiceCache("keep-one-day", TimeSpan.FromDays(1))
                    },
                    Restrictions =
                    {
                        new ServiceRestriction("internal-users-only", new[]
                        {
                            new ServiceRestrictionRule("intranet", "intranet.example.com")
                        })
                    }
                };
                var serviceId = _cdnService.CreateService(service);

                Assert.Equal("service-id", serviceId);
            }
        }
コード例 #8
0
 protected HttpTest InitializeInvalidHostHelper(Func<HttpTest, HttpRequestMessage, HttpResponseMessage> responseFactory = null)
 {
     var httpTest = new HttpTest();
     FlurlHttp.Configure(
         x => { x.HttpClientFactory = new TestingHttpClientFactory(httpTest, responseFactory); });
     return httpTest;
 }
コード例 #9
0
ファイル: GetSelfTests.cs プロジェクト: huoxudong125/Draft
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith(Fixtures.Statistics.SelfResponse);

                var response = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                    .Statistics
                    .GetServerStatistics();

                http.Should()
                    .HaveCalled(Fixtures.EtcdUrl.AppendPathSegment(Constants.Etcd.Path_Stats_Self))
                    .WithVerb(HttpMethod.Get)
                    .Times(1);

                response.Should().NotBeNull();
                response.Id.Should().NotBeNullOrWhiteSpace();
                response.Name.Should().NotBeNullOrWhiteSpace();

                response.LeaderInfo.Should().NotBeNull();
                response.LeaderInfo.Leader.Should().Be("8a69d5f6b7814500");
                response.LeaderInfo.StartTime.Should().HaveValue().And.NotBe(default(DateTime));
                response.LeaderInfo.Uptime.Should().NotBeNullOrWhiteSpace();

                response.State.Should().Be(StateType.StateFollower);
            }
        }
コード例 #10
0
ファイル: GetMembersTests.cs プロジェクト: huoxudong125/Draft
        public async Task ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWithJson(
                    Fixtures.Cluster.ClusterMembersResponse(
                        Fixtures.Cluster.ClusterMemberResponse(),
                        Fixtures.Cluster.ClusterMemberResponse(),
                        Fixtures.Cluster.ClusterMemberResponse(),
                        Fixtures.Cluster.ClusterMemberResponse(),
                        Fixtures.Cluster.ClusterMemberResponse()
                        ));

                var members = await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                                        .Cluster
                                        .GetMembers();

                http.Should()
                    .HaveCalled(
                        Fixtures.EtcdUrl
                                .AppendPathSegment(Constants.Etcd.Path_Members)
                    )
                    .WithVerb(HttpMethod.Get)
                    .Times(1);

                members.Should().NotBeNull()
                       .And
                       .HaveCount(5);
            }
        }
コード例 #11
0
        public async Task ExpectedValue_ShouldCallTheCorrectUrlByAwaitingImmediately()
        {
            using (var http = new HttpTest())
            {
                http.RespondWithJson(Fixtures.CompareAndSwap.DefaultResponse)
                    .RespondWithJson(Fixtures.CompareAndSwap.DefaultResponse);

                var req = Etcd.ClientFor(Fixtures.EtcdUrl.ToUri())
                    .Atomic.CompareAndSwap(Fixtures.CompareAndSwap.Path)
                    .WithExpectedValue(Fixtures.CompareAndSwap.ExpectedValue)
                    .WithNewValue(Fixtures.CompareAndSwap.NewValue);

                await req;
                await req.Execute();

                http.Should()
                    .HaveCalled(
                        Fixtures.EtcdUrl
                            .AppendPathSegments(Constants.Etcd.Path_Keys, Fixtures.CompareAndSwap.Path)
                            .SetQueryParam(Constants.Etcd.Parameter_PrevValue, Fixtures.CompareAndSwap.ExpectedValue)
                    )
                    .WithVerb(HttpMethod.Put)
                    .WithRequestBody(Fixtures.CompareAndSwap.DefaultRequest())
                    .Times(2);
            }
        }
コード例 #12
0
ファイル: ClientConfigTests.cs プロジェクト: llenroc/Flurl
		public async Task can_allow_specific_http_status() {
			using (var test = new HttpTest()) {
				test.RespondWith(404, "Nothing to see here");
				// no exception = pass
				await "http://www.api.com"
					.AllowHttpStatus(HttpStatusCode.Conflict, HttpStatusCode.NotFound)
					.DeleteAsync();
			}
		}
コード例 #13
0
ファイル: EdmundsTests.cs プロジェクト: ghotiphud/EdmundsApi
        public void Init()
        {
            _api = new Edmunds(_apiKey);

            if (_mockHttp)
            {
                //Flurl is in test mode
                _httpTest = new HttpTest();
            }
        }
コード例 #14
0
        public void ShouldThrowInvalidRequestException()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith(HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString());

                CallFixture.ShouldThrow<InvalidRequestException>()
                    .And
                    .IsInvalidRequest.Should().BeTrue();
            }
        }
コード例 #15
0
        public void ShouldThrowEtcdTimeoutException()
        {
            using (var http = new HttpTest())
            {
                http.SimulateTimeout();

                CallFixture.ShouldThrow<EtcdTimeoutException>()
                    .And
                    .IsTimeout.Should().BeTrue();
            }
        }
コード例 #16
0
ファイル: ClientConfigTests.cs プロジェクト: carolynvs/Flurl
		public async Task can_allow_non_success_status() {
			using (var test = new HttpTest()) {
				test.RespondWith(418, "I'm a teapot");
				try {
					var result = await "http://www.api.com".AllowHttpStatus("1xx,300-500").GetAsync();
					Assert.IsFalse(result.IsSuccessStatusCode);
				}
				catch (Exception) {
					Assert.Fail("Exception should not have been thrown.");
				}
			}
		}
コード例 #17
0
		public void when_GetTransaction_is_called_and_not_found_then_we_should_get_a_NotFoundException()
		{
			using (HttpTest httpTest = new HttpTest())
			{
				httpTest.RespondWithJson(404, new { message = "Not Found" });

				var signhostApiClient = new SignHostApiClient(settings);

				Func<Task> getTransaction = () => signhostApiClient.GetTransaction("transaction Id");
				getTransaction.ShouldThrow<ErrorHandling.NotFoundException>();
			}
		}
コード例 #18
0
		public void when_GetTransaction_is_called_and_the_authorization_is_bad_then_we_should_get_a_BadAuthorizationException()
		{
			using (HttpTest httpTest = new HttpTest()) {

				httpTest.RespondWithJson(401, new { message = "unauthorized" });

				var signhostApiClient = new SignHostApiClient(settings);

				Func<Task> getTransaction = () => signhostApiClient.GetTransaction("transaction Id");
				getTransaction.ShouldThrow<UnauthorizedAccessException>();
			}
		}
コード例 #19
0
        public void GetService()
        {
            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWithJson(new Service{Id = "service-id"});

                var service = _cdnService.GetService("service-id");

                Assert.NotNull(service);
                Assert.Equal("service-id", service.Id);
            }
        }
コード例 #20
0
		public void when_GetTransaction_is_called_and_unkownerror_like_418_occures_then_we_should_get_a_SignhostException()
		{
			using (HttpTest httpTest = new HttpTest())
			{
				httpTest.RespondWithJson(418, new { message = "418 I'm a teapot" });

				var signhostApiClient = new SignHostApiClient(settings);

				Func<Task> getTransaction = () => signhostApiClient.GetTransaction("transaction Id");
				getTransaction.ShouldThrow<ErrorHandling.SignhostRestApiClientException>()
					.WithMessage("*418*");
			}
		}
コード例 #21
0
ファイル: GlobalConfigTests.cs プロジェクト: llenroc/Flurl
		public async Task can_allow_non_success_status() {
			using (var test = new HttpTest()) {
				GetSettings().AllowedHttpStatusRange = "4xx";
				test.RespondWith(418, "I'm a teapot");
				try {
					var result = await GetClient().GetAsync();
					Assert.IsFalse(result.IsSuccessStatusCode);
				}
				catch (Exception) {
					Assert.Fail("Exception should not have been thrown.");
				}
			}
		}
コード例 #22
0
ファイル: GlobalConfigTests.cs プロジェクト: carolynvs/Flurl
		public async Task can_allow_non_success_status() {
			FlurlHttp.Configuration.AllowedHttpStatusRange = "4xx";
			using (var test = new HttpTest()) {
				test.RespondWith(418, "I'm a teapot");
				try {
					var result = await "http://www.api.com".GetAsync();
					Assert.IsFalse(result.IsSuccessStatusCode);
				}
				catch (Exception) {
					Assert.Fail("Exception should not have been thrown.");
				}
			}
		}
コード例 #23
0
ファイル: GlobalConfigTests.cs プロジェクト: carolynvs/Flurl
		public async Task can_set_post_callback() {
			var callbackCalled = false;
			using (var test = new HttpTest()) {
				test.RespondWith("ok");
				FlurlHttp.Configuration.AfterCall = call => {
					CollectionAssert.IsEmpty(test.ResponseQueue); // verifies that callback is running after HTTP call is made
					callbackCalled = true;
				};
				Assert.IsFalse(callbackCalled);
				await "http://www.api.com".GetAsync();
				Assert.IsTrue(callbackCalled);				
			}
		}
コード例 #24
0
        public void GetFlavor()
        {
            using (var httpTest = new HttpTest())
            {
                var cdnService = new ContentDeliveryNetworkService(Stubs.AuthenticationProvider, DefaultRegion);
                httpTest.RespondWithJson(new Flavor {Id = "flavor-id"});

                var flavor = cdnService.GetFlavor("flavor-id");

                Assert.NotNull(flavor);
                Assert.Equal("flavor-id", flavor.Id);
            }
        }
コード例 #25
0
        public void ListFlavors()
        {
            using (var httpTest = new HttpTest())
            {
                var cdnService = new ContentDeliveryNetworkService(Stubs.AuthenticationProvider, DefaultRegion);
                httpTest.RespondWithJson(new FlavorCollection(new[] {new Flavor()}));

                var flavors = cdnService.ListFlavors();

                Assert.NotNull(flavors);
                Assert.Equal(1, flavors.Count());
            }
        }
コード例 #26
0
        public void ShouldVerifyAndBuildWithoutException()
        {
            using (var http = new HttpTest())
            {
                http.RespondWith("etcd 1.2.3")
                    .RespondWith("etcd 1.2.3")
                    .RespondWith("etcd 1.2.4")
                    .RespondWith("etcd 1.2.4")
                    .RespondWith("etcd 1.2.4");

                BuildAndVerifyAction.ShouldNotThrow<Exception>();
            }
        }
コード例 #27
0
ファイル: GlobalConfigTests.cs プロジェクト: llenroc/Flurl
		public async Task can_set_post_callback() {
			var callbackCalled = false;
			using (var test = new HttpTest()) {
				test.RespondWith("ok");
				GetSettings().AfterCall = call => {
					CollectionAssert.IsEmpty(test.ResponseQueue); // verifies that callback is running after HTTP call is made
					callbackCalled = true;
				};
				Assert.IsFalse(callbackCalled);
				await GetClient().GetAsync();
				Assert.IsTrue(callbackCalled);				
			}
		}
コード例 #28
0
ファイル: SettingsTests.cs プロジェクト: damirjuretic/Flurl
		public async Task can_set_pre_callback() {
			var callbackCalled = false;
			using (var test = new HttpTest()) {
				test.RespondWith("ok");
				FlurlHttp.GlobalSettings.BeforeCall = req => {
					CollectionAssert.IsNotEmpty(test.ResponseQueue); // verifies that callback is running before HTTP call is made
					callbackCalled = true;
				};
				Assert.IsFalse(callbackCalled);
				await "http://www.api.com".GetAsync();
				Assert.IsTrue(callbackCalled);
			}
		}
コード例 #29
0
ファイル: Tester.cs プロジェクト: llenroc/Flurl
		public static async Task DoTestsAsync(Action<string> log) {
			var source = await "http://www.google.com".GetStringAsync();
			log(source.Substring(0, 40));
			log("^-- real response");
			using (var test = new HttpTest()) {
				test.RespondWith("totally fake google source");
				log(await "http://www.google.com".GetStringAsync());
				log("^-- fake response");
			}

			var path = await "http://www.google.com".DownloadFileAsync("c:\\flurl", "google.txt");
			log("dowloaded google source to " + path);
			log("done");
		}
コード例 #30
0
		public async Task when_a_GetTransaction_is_called_then_we_should_have_called_the_transaction_get_once()
		{
			using (HttpTest httpTest = new HttpTest()) {
				httpTest.RespondWith(200, APIResponses.GetTransaction);

				var signhostApiClient = new SignHostApiClient(settings);

				var result = await signhostApiClient.GetTransaction("transaction Id");
				result.Id.Should().Be("c487be92-0255-40c7-bd7d-20805a65e7d9");

				httpTest.ShouldHaveCalled($"{settings.Endpoint}transaction/*")
					.WithVerb(HttpMethod.Get)
					.Times(1);
			}
		}
コード例 #31
0
 private static void SetCurrentTest(HttpTest test) => _test.Value = test;
コード例 #32
0
 private static void SetCurrentTest(HttpTest test) => System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("FlurlHttpTest", test);
コード例 #33
0
 public TestHttpClientFactory(HttpTest test)
 {
     _test = test;
 }