From 08b32a62fa6e4ed6f4bceee4bbd95e30aa8262a2 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Thu, 26 May 2016 23:52:20 +0100 Subject: [PATCH] Fix tag.getTopArtists error test #87 --- .../Api/Commands/Tag/GetTopArtistsCommandTests.cs | 6 +++--- src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj | 1 - .../Resources/GetTopArtistsError.json | 2 +- .../Api/Commands/Tag/GetTopArtistsCommand.cs | 12 +++++++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/IF.Lastfm.Core.Tests/Api/Commands/Tag/GetTopArtistsCommandTests.cs b/src/IF.Lastfm.Core.Tests/Api/Commands/Tag/GetTopArtistsCommandTests.cs index 60e6f881..9d89f215 100644 --- a/src/IF.Lastfm.Core.Tests/Api/Commands/Tag/GetTopArtistsCommandTests.cs +++ b/src/IF.Lastfm.Core.Tests/Api/Commands/Tag/GetTopArtistsCommandTests.cs @@ -41,12 +41,12 @@ public async Task HandleSuccessResponse() Assert.IsTrue(parsed.Success); Assert.IsNotNull(parsed.Content); } - + [Test] - public async Task HandleErrorResponse() + public async Task ErrorIfTagIsEmpty() { var command = new GetTopArtistsCommand(MAuth.Object, "errorTag"); - + var response = CreateResponseMessage(Encoding.UTF8.GetString(TagApiResponses.GetTopArtistsError)); var parsed = await command.HandleResponse(response); diff --git a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj index e6306898..310c2edd 100644 --- a/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj +++ b/src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj @@ -150,7 +150,6 @@ - Designer diff --git a/src/IF.Lastfm.Core.Tests/Resources/GetTopArtistsError.json b/src/IF.Lastfm.Core.Tests/Resources/GetTopArtistsError.json index 0dc8f28e..781b34bb 100644 --- a/src/IF.Lastfm.Core.Tests/Resources/GetTopArtistsError.json +++ b/src/IF.Lastfm.Core.Tests/Resources/GetTopArtistsError.json @@ -2,7 +2,7 @@ "topartists": { "artist": [], "@attr": { - "tag": "asfasf", + "tag": "", "page": "1", "perPage": "1", "totalPages": "0", diff --git a/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopArtistsCommand.cs b/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopArtistsCommand.cs index eb168928..2efc4da7 100644 --- a/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopArtistsCommand.cs +++ b/src/IF.Lastfm.Core/Api/Commands/Tag/GetTopArtistsCommand.cs @@ -28,19 +28,25 @@ public override void SetParameters() public override async Task> HandleResponse(HttpResponseMessage response) { var json = await response.Content.ReadAsStringAsync(); + var jtoken = JsonConvert.DeserializeObject(json); + var resultsToken = jtoken.SelectToken("topartists"); LastResponseStatus status; if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode) { - var jtoken = JsonConvert.DeserializeObject(json); - var resultsToken = jtoken.SelectToken("topartists"); + if (string.IsNullOrEmpty(resultsToken.SelectToken("@attr.tag").Value())) + { + return PageResponse.CreateErrorResponse(LastResponseStatus.MissingParameters); + } + var itemsToken = resultsToken.SelectToken("artist"); return PageResponse.CreateSuccessResponse(itemsToken, resultsToken, LastArtist.ParseJToken, LastPageResultsType.Attr); } else { - return LastResponse.CreateErrorResponse>(status); + // The tag api always returns a "valid" response, so + return PageResponse.CreateErrorResponse(status); } } }