Skip to content

Commit

Permalink
Fix tag.getTopArtists error test #87
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkit committed May 26, 2016
1 parent b285c27 commit 08b32a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/IF.Lastfm.Core.Tests/IF.Lastfm.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
<None Include="Resources\GetTopArtistsError.json" />
<None Include="Resources\GetTopArtistsSingle.json" />
<None Include="Resources\GetTopArtistsSuccess.json" />
<None Include="Resources\GetTopArtistsSuccess.txt" />
<Content Include="Resources\TrackApi\TrackScrobbleTwoRequestBody.txt" />
<None Include="app.config">
<SubType>Designer</SubType>
Expand Down
2 changes: 1 addition & 1 deletion src/IF.Lastfm.Core.Tests/Resources/GetTopArtistsError.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"topartists": {
"artist": [],
"@attr": {
"tag": "asfasf",
"tag": "",
"page": "1",
"perPage": "1",
"totalPages": "0",
Expand Down
12 changes: 9 additions & 3 deletions src/IF.Lastfm.Core/Api/Commands/Tag/GetTopArtistsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ public override void SetParameters()
public override async Task<PageResponse<LastArtist>> HandleResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var resultsToken = jtoken.SelectToken("topartists");

LastResponseStatus status;
if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var resultsToken = jtoken.SelectToken("topartists");
if (string.IsNullOrEmpty(resultsToken.SelectToken("@attr.tag").Value<string>()))
{
return PageResponse<LastArtist>.CreateErrorResponse(LastResponseStatus.MissingParameters);
}

var itemsToken = resultsToken.SelectToken("artist");

return PageResponse<LastArtist>.CreateSuccessResponse(itemsToken, resultsToken, LastArtist.ParseJToken, LastPageResultsType.Attr);
}
else
{
return LastResponse.CreateErrorResponse<PageResponse<LastArtist>>(status);
// The tag api always returns a "valid" response, so
return PageResponse<LastArtist>.CreateErrorResponse(status);
}
}
}
Expand Down

0 comments on commit 08b32a6

Please sign in to comment.