public async Task GetRequest_FailedRequest_ReturnsStatusCodeAndResponseContent() { var httpClient = Substitute.For <IHttpClient>(); httpClient.GetResponse(Arg.Is <HttpRequestOptions>(o => o.AcceptHeader == "application/json" && o.Url == "https://api.thetvdb.com/series/122/episodes?page=1" && o.RequestContent == null && o.RequestContentType == null)) .ReturnsForAnyArgs(Task.FromResult(new HttpResponseInfo { Content = Streams.ToStream("{\"Error\": \"Not Authorized\"}"), StatusCode = HttpStatusCode.Unauthorized })); var jsonSerialiser = Substitute.For <ICustomJsonSerialiser>(); var request = new GetEpisodesRequest(122, 1); var connection = new JsonConnection(httpClient, jsonSerialiser, Substitute.For <ILogManager>()); var response = await connection.GetAsync(request, Option <string> .None); response.IsLeft.Should().BeTrue(); response.IfLeft(fr => { ((object)fr.StatusCode).Should().Be(HttpStatusCode.Unauthorized); fr.ResponseContent.Should().Be("{\"Error\": \"Not Authorized\"}"); }); }
public async Task GetRequest_SuccessfulRequest_ReturnsResponse() { var httpClient = Substitute.For <IHttpClient>(); httpClient.GetResponse(Arg.Is <HttpRequestOptions>(o => o.AcceptHeader == "application/json" && o.Url == "https://api.thetvdb.com/series/122/episodes?page=1" && o.RequestContent == null && o.RequestContentType == null)) .Returns(Task.FromResult(new HttpResponseInfo { Content = Streams.ToStream( @"{ ""data"": [ { ""absoluteNumber"": 1, ""airedEpisodeNumber"": 2, ""airedSeason"": 3, ""dvdEpisodeNumber"": 4, ""dvdSeason"": 5, ""episodeName"": ""EpisodeName1"", ""firstAired"": ""01/01/2017"", ""id"": 6, ""lastUpdated"": 7, ""overview"": ""EpisodeOverview1"" }, { ""absoluteNumber"": 8, ""airedEpisodeNumber"": 9, ""airedSeason"": 10, ""dvdEpisodeNumber"": 11, ""dvdSeason"": 12, ""episodeName"": ""EpisodeName2"", ""firstAired"": ""01/01/2015"", ""id"": 13, ""lastUpdated"": 14, ""overview"": ""EpisodeOverview2"" } ], ""errors"": { ""invalidFilters"": [ ""string"" ], ""invalidLanguage"": ""string"", ""invalidQueryParams"": [ ""string"" ] }, ""links"": { ""first"": 1, ""last"": 2, ""next"": 3, ""previous"": 4 } }"), StatusCode = HttpStatusCode.OK })); var jsonSerialiser = Substitute.For <ICustomJsonSerialiser>(); var request = new GetEpisodesRequest(122, 1); jsonSerialiser.Deserialise <GetEpisodesRequest.Response>(null) .ReturnsForAnyArgs(new GetEpisodesRequest.Response(new[] { new TvDbEpisodeSummaryData(6, "EpisodeName1", 1L, 2, 3, 7, new DateTime(2017, 1, 2, 3, 4, 5), "Overview"), new TvDbEpisodeSummaryData(13, "EpisodeName2", 8L, 9, 10, 17, new DateTime(2017, 1, 2, 3, 4, 5), "Overview") }, new GetEpisodesRequest.PageLinks(1, 2, 3, 4))); var connection = new JsonConnection(httpClient, jsonSerialiser, Substitute.For <ILogManager>()); var response = await connection.GetAsync(request, Option <string> .None); response.IsRight.Should().BeTrue(); response.IfRight(r => r.Data.Data.Should().HaveCount(2)); }