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.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.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.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 ShouldThrowInvalidRequestException() { using (var http = new HttpTest()) { http.RespondWith(HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString()); CallFixture.ShouldThrow<InvalidRequestException>() .And .IsInvalidRequest.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 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"); 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_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_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 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_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); } }
public void ShouldThrowServiceUnavailableExceptionOn503ResponseCode() { using (var http = new HttpTest()) { http.RespondWith(HttpStatusCode.ServiceUnavailable, string.Empty); Func<Task> action = async () => { await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri()) .Cluster .GetLeader(); }; action.ShouldThrowExactly<ServiceUnavailableException>() .And .IsServiceUnavailable.Should().BeTrue(); } }
public void ShouldThrowInvalidRequestExceptionOn404ResponseCode() { using (var http = new HttpTest()) { http.RespondWith(HttpStatusCode.NotFound, string.Empty); Func<Task> action = async () => { await Etcd.ClientFor(Fixtures.EtcdUrl.ToUri()) .Cluster .DeleteMember() .WithMemberId(StaticRandom.Instance.Next().ToString()); }; action.ShouldThrowExactly<InvalidRequestException>() .And .IsInvalidRequest.Should().BeTrue(); } }
public async Task can_set_error_callback(bool markExceptionHandled) { var callbackCalled = false; using (var test = new HttpTest()) { test.RespondWith(500, "server error"); FlurlHttp.Configuration.OnError = call => { CollectionAssert.IsEmpty(test.ResponseQueue); // verifies that callback is running after HTTP call is made callbackCalled = true; call.ExceptionHandled = markExceptionHandled; }; Assert.IsFalse(callbackCalled); try { await "http://www.api.com".GetAsync(); Assert.IsTrue(callbackCalled, "OnError was never called"); Assert.IsTrue(markExceptionHandled, "ExceptionHandled was marked false in callback, but exception was not propagated."); } catch (FlurlHttpException) { Assert.IsTrue(callbackCalled, "OnError was never called"); Assert.IsFalse(markExceptionHandled, "ExceptionHandled was marked true in callback, but exception was propagated."); } } }
public async Task Watch_ShouldStopPollingOnA500LevelResponse() { using (var http = new HttpTest()) { http.RespondWith(500, "Some error string") .RespondWithJson(Fixtures.Watch.DefaultResponse) .RespondWithJson(Fixtures.Watch.DefaultResponse); var tcs = new TaskCompletionSource<object>(); var task = tcs.Task; Etcd.ClientFor(Fixtures.EtcdUrl.ToUri()) .Watch(Fixtures.Watch.Path) .Subscribe(tcs.SetResult, x => tcs.SetException(x)); try { await task; } catch { // ignored } 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(1); task.IsCompleted.Should().BeTrue(); task.IsFaulted.Should().BeTrue(); } }
public async Task can_allow_any_http_status() { using (var test = new HttpTest()) { test.RespondWith(500, "epic fail"); try { var result = await "http://www.api.com".AllowAnyHttpStatus().GetAsync(); Assert.IsFalse(result.IsSuccessStatusCode); } catch (Exception) { Assert.Fail("Exception should not have been thrown."); } } }
public async Task can_clear_non_success_status() { using (var test = new HttpTest()) { test.RespondWith(418, "I'm a teapot"); // allow 4xx var client = "http://www.api.com".AllowHttpStatus("4xx"); // but then disallow it client.AllowedHttpStatusRanges.Clear(); await client.GetAsync(); } }
public async Task when_AddOrReplaceFileToTransaction_is_called_then_we_should_have_called_the_file_put_once() { using (HttpTest httpTest = new HttpTest()) { httpTest.RespondWith(200, string.Empty); var signhostApiClient = new SignHostApiClient(settings); using (Stream file = System.IO.File.Create("unittestdocument.pdf")) { await signhostApiClient.AddOrReplaceFileToTansaction(file, "transaction Id", "file Id"); } httpTest.ShouldHaveCalled($"{settings.Endpoint}transaction/*/file/*") .WithVerb(HttpMethod.Put) .WithContentType("application/pdf") .Times(1); } }
public void WhenIPurgeANonExistingAsset_TheOperationShouldBeConsideredSuccessful() { using (var httpTest = new HttpTest()) { httpTest.RespondWith((int)HttpStatusCode.NotFound, "Nothing to see here!"); _cdnService.PurgeCachedAsset("service-id", "missing-asset"); } }
public void DeleteService() { using (var httpTest = new HttpTest()) { httpTest.RespondWith((int)HttpStatusCode.NoContent, "All gone!"); _cdnService.DeleteService("service-id"); } }
public void WaitForServiceDeleted() { using (var httpTest = new HttpTest()) { httpTest.RespondWithJson(new Service { Status = ServiceStatus.DeleteInProgress }); httpTest.RespondWith((int)HttpStatusCode.NotFound, "All gone!"); _cdnService.WaitForServiceDeleted("service-id", TimeSpan.FromMilliseconds(1)); } }
public void WhenIDeleteANonExistingService_TheOperationShouldBeConsideredSuccessful() { using (var httpTest = new HttpTest()) { httpTest.RespondWith((int)HttpStatusCode.NotFound, "Nothing to see here!"); _cdnService.DeleteService("bad-service-id"); } }
public async Task can_disable_exception_behavior() { FlurlHttp.Configuration.OnError = call => { call.ExceptionHandled = true; }; using (var test = new HttpTest()) { test.RespondWith(500, "server error"); try { var result = await "http://www.api.com".GetAsync(); Assert.IsFalse(result.IsSuccessStatusCode); } catch (Exception) { Assert.Fail("Exception should not have been thrown."); } } }
public void when_StartTransaction_is_called_then_we_should_have_called_the_transaction_put_once() { using (HttpTest httpTest = new HttpTest()) { httpTest.RespondWith(200, string.Empty); var signhostApiClient = new SignHostApiClient(settings); signhostApiClient.StartTransaction("transaction Id"); httpTest.ShouldHaveCalled($"{settings.Endpoint}transaction/*/start") .WithVerb(HttpMethod.Put) .Times(1); } }
public void when_GetDocument_is_called_then_we_should_have_called_the_file_get_once() { using (HttpTest httpTest = new HttpTest()) { httpTest.RespondWith(200, string.Empty); var signhostApiClient = new SignHostApiClient(settings); var document = signhostApiClient.GetDocument("transaction Id", "file Id"); httpTest.ShouldHaveCalled($"{settings.Endpoint}transaction/*/file/*") .WithVerb(HttpMethod.Get) .Times(1); } }
public async Task can_override_settings_fluently() { using (var test = new HttpTest()) { FlurlHttp.GlobalSettings.AllowedHttpStatusRange = "*"; test.RespondWith(500, "epic fail"); await "http://www.api.com".ConfigureClient(c => c.AllowedHttpStatusRange = "2xx").GetAsync(); } }
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(); } }