/// <inheritdoc /> public async Task <IUpdateCheckResult> CheckAsync() { IReposApi api = _apiFactory.BuildReposApi(_username, _repoName); try { IRelease latest = await api.LatestRelease().ConfigureAwait(false); Version latestVersion = new Version(latest.tag_name.TrimStart('v')); return(latestVersion > ApplicationVersion ? latest.assets.Any() ? UpdateCheckResult.FromLatestRelease(latest) : new UpdateCheckResult(EventLevel.Error, Messages.NoUpdateTitle, Messages.NoUpdateNoFiles) : new UpdateCheckResult(EventLevel.Informational, Messages.NoUpdateTitle, Messages.NoUpdateAlreadyLatest)); } catch (WebException wex) { switch (wex.Status) { case WebExceptionStatus.NameResolutionFailure: case WebExceptionStatus.ConnectFailure: return(new UpdateCheckResult(EventLevel.Error, Messages.NoUpdateTitle, Messages.NoUpdateCannotConnect)); case WebExceptionStatus.ProtocolError: if ((wex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { return(new UpdateCheckResult(EventLevel.Error, Messages.NoUpdateTitle, Messages.NoUpdateNoReleases)); } goto default; // we only handled not found, so anything else is a random other error default: return(new UpdateCheckResult(EventLevel.Error, Messages.NoUpdateTitle, string.Format(Messages.NoUpdateError, wex.Message))); } } }
private static IReposApi GetReposApi(WebException wex) { IReposApi api = A.Fake <IReposApi>(); A.CallTo(() => api.LatestRelease()).Throws(wex); return(api); }
private static IReposApi GetReposApi(IRelease latestRelease) { IReposApi api = A.Fake <IReposApi>(); A.CallTo(() => api.LatestRelease()).Returns(Task.FromResult(latestRelease)); return(api); }
public async Task LatestRelease_MultipleReleases_ReturnsIRelease() { IReposApi api = GetReposApi(_repoWithReleases); IRelease latest = await api.LatestRelease().ConfigureAwait(false); Assert.IsNotNull(latest, $"{nameof(api.LatestRelease)}() should return an IRelease object for a repo that has at least one release."); }
public async Task LatestRelease_NoReleases_Throws404() { IReposApi api = GetReposApi(_repoNoReleases); WebException caughtException = null; try { await api.LatestRelease().ConfigureAwait(false); } catch (WebException e) { caughtException = e; } Assert.AreEqual(WebExceptionStatus.ProtocolError, caughtException.Status, $"{nameof(api.LatestRelease)}() is expected to throw an error due to the API response returning an error code when the requested repo doesn't have any releases. Did GitHub repo {_username}/{_repoNoReleases} have a release?"); Assert.AreEqual(HttpStatusCode.NotFound, (caughtException.Response as HttpWebResponse).StatusCode, $"{nameof(api.LatestRelease)}() is expected to throw an error with a 404 not found status code when the requested repo doesn't have any releases. Did GitHub repo {_username}/{_repoNoReleases} have a release?"); }
// instantiating interface in the constructor in this example instead. _url could come from testdata object // injected from hooks or other methods public ReposSteps() { _reposApi = RestClient.For <IReposApi>(_url); }