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]); } } }
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"); } }
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)); } }
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); } }
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)); } }
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); } }
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); } }
protected HttpTest InitializeInvalidHostHelper(Func<HttpTest, HttpRequestMessage, HttpResponseMessage> responseFactory = null) { var httpTest = new HttpTest(); FlurlHttp.Configure( x => { x.HttpClientFactory = new TestingHttpClientFactory(httpTest, responseFactory); }); return httpTest; }
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); } }
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); } }
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); } }
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(); } }
public void Init() { _api = new Edmunds(_apiKey); if (_mockHttp) { //Flurl is in test mode _httpTest = new HttpTest(); } }
public void ShouldThrowInvalidRequestException() { using (var http = new HttpTest()) { http.RespondWith(HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString()); CallFixture.ShouldThrow<InvalidRequestException>() .And .IsInvalidRequest.Should().BeTrue(); } }
public void ShouldThrowEtcdTimeoutException() { using (var http = new HttpTest()) { http.SimulateTimeout(); CallFixture.ShouldThrow<EtcdTimeoutException>() .And .IsTimeout.Should().BeTrue(); } }
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."); } } }
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>(); } }
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>(); } }
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); } }
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*"); } }
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."); } } }
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."); } } }
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); } }
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); } }
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()); } }
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>(); } }
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); } }
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); } }
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"); }
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); } }
private static void SetCurrentTest(HttpTest test) => _test.Value = test;
private static void SetCurrentTest(HttpTest test) => System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("FlurlHttpTest", test);
public TestHttpClientFactory(HttpTest test) { _test = test; }