public async Task CancelsFirstRequestWhenSecondOneIsSent_SortPropertyChanged() { var results = 0; var requestsSent = 0; Func <string, Task <HttpResponseMessage> > getHttpAsync = async _ => { requestsSent++; return((await GetResponseWithDelay(CancellationToken.None)).Item1); }; var paginationService = new PaginationBaseService <User>("https://example.com", getHttpAsync); paginationService.PaginationResult.Subscribe(_ => results++); await Task.WhenAny(paginationService.PaginationResult.FirstAsync().ToTask(), Task.Delay(10_000)); // Timeout Assert.Equal(1, results); Assert.Equal(1, requestsSent); paginationService.SetSortProperty("Email"); paginationService.SetSortProperty("UserName"); await Task.WhenAny(paginationService.PaginationResult.Skip(1).FirstAsync().ToTask(), Task.Delay(10_000)); // Timeout Assert.Equal(2, results); Assert.Equal(3, requestsSent); }
public void AcceptsAbsoluteBaseUrl() { var baseUrl = "https://example.com"; var service = new PaginationBaseService <MockPayload>(baseUrl, x => Task.FromResult(new HttpResponseMessage())); Assert.Equal(baseUrl, service.BaseUrl); }
public void AcceptsRelativeBaseUrl() { var baseUrl = "/api/values"; var service = new PaginationBaseService <MockPayload>(baseUrl, x => Task.FromResult(new HttpResponseMessage())); Assert.Equal(baseUrl, service.BaseUrl); }
public UrlBuilder() { _service = new PaginationBaseService <MockPayload>(_baseUrl, x => { _lastRequestedUrl = x; return(Task.FromResult(new HttpResponseMessage())); }); }
public Disposable() { _service = new PaginationBaseService <MockPayload>(string.Empty, x => { _hasCalled = true; return(Task.FromResult(new HttpResponseMessage())); }); }
private void InitializeService() { _service = new PaginationBaseService <MockPayload>(string.Empty, x => Task.FromResult(_response)); _service.PaginationResult.Subscribe(r => { _hasRaisedNewPaginationResult = true; _receivedResult = r; }); }
private void InitializeServiceAndResetObservedValues(object payload = null) { _response = GetResponse(payload); _service = new PaginationBaseService <MockPayload>(string.Empty, x => Task.FromResult(_response)); _service.PaginationResult.Subscribe(r => { _hasRaisedNewPaginationResult = true; _receivedResult = r; }); _receivedResult = null; _hasRaisedNewPaginationResult = false; }
public void SetsPropertiesFromDefaultOptions() { var service = new PaginationBaseService <MockPayload>(string.Empty, x => Task.FromResult(new HttpResponseMessage()), new DefaultPaginationOptions { Page = 12, PageSize = 4, SortDescending = true, SortProperty = "Name" }); Assert.Equal(12, service.Page); Assert.Equal(4, service.PageSize); Assert.True(service.SortDescending); Assert.Equal("Name", service.SortProperty); }
private void InitializeService() { _service = new PaginationBaseService <MockPayload>(string.Empty, x => { var response = GetResponse(); var parsedQuery = QueryHelpers.ParseQuery(x); _lastCalledResponsePage = Convert.ToInt32(parsedQuery["page"]); if (_firstCalledResponsePage == default) { _firstCalledResponsePage = _lastCalledResponsePage; } return(Task.FromResult(response)); }, new DefaultPaginationOptions { Page = _initialResponsePage }); }
public void CallsOnInitializationWithCustomOptions() { _service = new PaginationBaseService <MockPayload>(_baseUrl, x => { _lastRequestedUrl = x; return(Task.FromResult(new HttpResponseMessage())); }, new DefaultPaginationOptions { Page = 3, PageSize = 17, SortDescending = true, SortProperty = "Name" }); var expectedUrl = $"{_baseUrl}?page=3&pageSize=17&sort=Name%20desc"; Assert.Equal(expectedUrl, _lastRequestedUrl); }
public async Task CancelsFirstRequestWhenSecondOneIsSent_UsesCancellationToken() { var results = 0; var requestsSent = 0; var requestsCancelled = new bool[3]; var responseTasks = new List <Task>(); var currentRequests = 0; Func <string, CancellationToken, Task <HttpResponseMessage> > getHttpAsync = async(_, token) => { requestsSent++; var currentRequest = currentRequests++; var responseTask = GetResponseWithDelay(token); responseTasks.Add(responseTask); var response = await responseTask; requestsCancelled[currentRequest] = response.Item2; return(response.Item1); }; var paginationService = new PaginationBaseService <User>("https://example.com", getHttpAsync); paginationService.PaginationResult.Subscribe(_ => results++); await Task.WhenAny(paginationService.PaginationResult.FirstAsync().ToTask(), Task.Delay(10_000)); // Timeout Assert.Equal(1, results); Assert.Equal(1, requestsSent); paginationService.Page++; paginationService.Page++; await Task.WhenAny(paginationService.PaginationResult.Skip(1).FirstAsync().ToTask(), Task.Delay(10_000)); // Timeout Assert.Equal(2, results); Assert.Equal(3, requestsSent); Assert.False(requestsCancelled[0]); Assert.True(requestsCancelled[1]); Assert.False(requestsCancelled[2]); }
public void AcceptsEmptyBaseUrl() { var service = new PaginationBaseService <MockPayload>(string.Empty, x => Task.FromResult(new HttpResponseMessage())); Assert.NotNull(service); }
public void SetsBaseUrlToStringEmptyForEmptyBaseUrl() { var service = new PaginationBaseService <MockPayload>(null, x => Task.FromResult(new HttpResponseMessage())); Assert.Equal(string.Empty, service.BaseUrl); }
public RequestRunningNotifications() { _service = new PaginationBaseService <MockPayload>(string.Empty, x => Task.FromResult(new HttpResponseMessage())); _service.RequestRunning.Subscribe(isRunning => _isRunningEvents.Add(isRunning)); }