public async Task CanLoginWhenForced(string token, string user, string password) { _http.ResponseQueue.Clear(); var sut = new HarborClient("http://localhost:5555/", LoginBehavior.ForcePre17); ConfigureAuthToken(token); await sut.Login(user, password); Assert.Equal(token, sut.SessionToken); _http.ShouldHaveCalled("*/login").WithVerb(HttpMethod.Post).WithRequestBody($"principal={user}&password={password}").Times(1); _http.ShouldNotHaveCalled("*/api/systeminfo"); }
public async Task Logout_ThrowsWithNoSession() { try { await client.Logout(); Assert.True(false, "Expected exception to be thrown"); } catch (Exception ex) { Assert.IsAssignableFrom <InvalidOperationException>(ex); Assert.Equal("Not logged in", ex.Message); _http.ShouldNotHaveCalled("*"); } }
public async Task can_setup_multiple_responses() { HttpTest .RespondWith("one") .RespondWith("two") .RespondWith("three"); HttpTest.ShouldNotHaveMadeACall(); await "http://www.api.com/1".GetAsync(); await "http://www.api.com/2".GetAsync(); await "http://www.api.com/3".GetAsync(); var calls = HttpTest.CallLog; Assert.AreEqual(3, calls.Count); Assert.AreEqual("one", await calls[0].Response.GetStringAsync()); Assert.AreEqual("two", await calls[1].Response.GetStringAsync()); Assert.AreEqual("three", await calls[2].Response.GetStringAsync()); HttpTest.ShouldHaveMadeACall(); HttpTest.ShouldHaveCalled("http://www.api.com/*").WithVerb(HttpMethod.Get).Times(3); HttpTest.ShouldNotHaveCalled("http://www.otherapi.com/*"); // #323 make sure it's a full string match and not a "contains" Assert.Throws <HttpTestException>(() => HttpTest.ShouldHaveCalled("http://www.api.com/")); HttpTest.ShouldNotHaveCalled("http://www.api.com/"); }
public void SerializeListServerOptionsInUrl() { using (var httpTest = new HttpTest()) { httpTest.RespondWithJson(new ServerSummaryCollection()); _compute.ListServerSummaries(new ServerListOptions()); httpTest.ShouldNotHaveCalled("*metadata*"); } }
public async void Orders_GetOrdersAsync_QueryOnePagesResult_ReturnNotEmpty() { const int count = 100; const string query = "limit=100&paymentStatus=paid"; _httpTest .RespondWithJson(Mocks.MockSearchResultWithManyOrderAndPages(count, 0, count)); var result = await _client.GetOrdersAsync(new { limit = count, paymentStatus = "paid" }); _httpTest.ShouldHaveCalled($"{CheckOrdersUrl}&{query}") .WithVerb(HttpMethod.Get) .Times(1); _httpTest.ShouldNotHaveCalled($"{CheckOrdersUrl}&offset=*&{query}"); Assert.Equal(count, result.Count()); }
public async Task can_disable_redirect() { HttpTest .RespondWith("", 301, new { Location = "/next" }) .RespondWith("done!"); var fc = new FlurlClient(); fc.Settings.Redirects.Enabled = false; await fc.Request("http://start.com").GetAsync(); HttpTest.ShouldNotHaveCalled("http://start.com/next"); }
public async Task can_block_redirect_from_event() { HttpTest .RespondWith("", 301, new { Location = "/next" }) .RespondWith("done!"); var fc = new FlurlClient(); await fc.Request("http://start.com") .OnRedirect(call => call.Redirect.Follow = false) .GetAsync(); HttpTest.ShouldNotHaveCalled("http://start.com/next"); }
public async void Legacy_Orders_GetOrdersAsync_QueryMultiPagesResultOnePage_ReturnCorrectResult() { _httpTest .RespondWithJson( Mocks.MockLegacyOrderResponseWithManyOrderAndPages( $"{_checkOrdersLegacyUrl}&limit=5&offset=5")); var result = await _legacyClient.Orders.Limit(5).GetAsync(); _httpTest.ShouldHaveCalled($"{_checkOrdersLegacyUrl}&limit=5") .WithVerb(HttpMethod.Get) .Times(1); _httpTest.ShouldNotHaveCalled($"{_checkOrdersLegacyUrl}&limit=5&offset=5"); Assert.Equal(200, result.Count()); }
public async Task can_setup_multiple_responses() { HttpTest .RespondWith("one") .RespondWith("two") .RespondWith("three"); await "http://www.api.com/1".GetAsync(); await "http://www.api.com/2".GetAsync(); await "http://www.api.com/3".GetAsync(); var calls = HttpTest.CallLog; Assert.AreEqual(3, calls.Count); Assert.AreEqual("one", await calls[0].Response.Content.ReadAsStringAsync()); Assert.AreEqual("two", await calls[1].Response.Content.ReadAsStringAsync()); Assert.AreEqual("three", await calls[2].Response.Content.ReadAsStringAsync()); HttpTest.ShouldHaveCalled("http://www.api.com/*").WithVerb(HttpMethod.Get).Times(3); HttpTest.ShouldNotHaveCalled("http://www.otherapi.com/*"); }
public async Task Throws_vehicle_make_not_found_exception_when_api_returns_404() { // Arrange var make = "yo"; var sut = new VehicleSummaryService(_testConfiguration, _fakeLogger); using (var httpTest = new HttpTest()) { httpTest.RespondWith("Not found", 404); // Act // Assert await Assert.ThrowsAsync <VehicleMakeNotFoundException>(async() => await sut.GetSummaryByMake(make)); httpTest.ShouldHaveCalled("http://fake-models-by-make-url/yo") .WithQueryParamValue("api-version", "fake-version") .WithHeader("Ocp-Apim-Subscription-Key", "fake-key"); httpTest.ShouldNotHaveCalled("http://fake-years-of-models-by-make-url/*"); } }
public async Task can_limit_redirects(int?max) { for (var i = 1; i <= 20; i++) { HttpTest.RespondWith("", 301, new { Location = "/redir" + i }); } var fc = new FlurlClient(); if (max.HasValue) { fc.Settings.Redirects.MaxAutoRedirects = max.Value; } await fc.Request("http://start.com").GetAsync(); var count = max ?? 10; HttpTest.ShouldHaveCalled("http://start.com/redir*").Times(count); HttpTest.ShouldHaveCalled("http://start.com/redir" + count); HttpTest.ShouldNotHaveCalled("http://start.com/redir" + (count + 1)); }
public async Task can_allow_redirect_secure_to_insecure(bool allow) { HttpTest .RespondWith("", 301, new { Location = "http://insecure.com/next" }) .RespondWith("done!"); var fc = new FlurlClient(); if (allow) // test that false is default (don't set explicitly) { fc.Settings.Redirects.AllowSecureToInsecure = true; } await fc.Request("https://secure.com").GetAsync(); if (allow) { HttpTest.ShouldHaveCalled("http://insecure.com/next"); } else { HttpTest.ShouldNotHaveCalled("http://insecure.com/next"); } }