Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Potty committed Oct 5, 2023
1 parent 4429697 commit 43ebe65
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
42 changes: 42 additions & 0 deletions rokka-client-c-sharp.tests/Factories/RokkaResponseFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ private static HttpResponseMessage CreateSuccessResponseMessage()
};
}

private static HttpResponseMessage CreateNonJsonResponseMessage()
{
const string responseString = "not json";

return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
ReasonPhrase = SuccessReasonPhrase,
Content = new StringContent(responseString)
};
}

private static HttpResponseMessage CreateUnknownSuccessResponseMessage()
{
const string responseString = "{\"unknown_property\":\"unknown_value\"}";

return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
ReasonPhrase = SuccessReasonPhrase,
Content = new StringContent(responseString)
};
}

private static HttpResponseMessage CreateErrorResponseMessage()
{
const string responseString = "{\n \"code\": 403,\n \"message\": \"API credentials are not supplied or not a valid format.\",\n \"invalid_authentication\": true\n}";
Expand Down Expand Up @@ -186,4 +210,22 @@ public async void GivenAnErrorListResponse_WhenBuildRokkaResponse_ErrorIsCorrect
var error = response.Error;
Assert.Equivalent(expectedError, error);
}

[Fact]
public async void GivenAnNonJsonResponse_WhenBuildRokkaResponse_ThrowsException()
{
var responseMessage = CreateNonJsonResponseMessage();

await Assert.ThrowsAsync<RokkaClientException>( () => new RokkaResponseFactory().BuildRokkaResponse(responseMessage));

}

[Fact]
public async void GivenAnUnknownSuccessResponse_WhenBuildRokkaResponse_ThrowsException()
{
var responseMessage = CreateUnknownSuccessResponseMessage();

await Assert.ThrowsAsync<RokkaClientException>( () => new RokkaResponseFactory().BuildRokkaResponse(responseMessage));

}
}
18 changes: 15 additions & 3 deletions rokka-client-c-sharp/Factories/RokkaResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class RokkaResponseFactory
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
},
MissingMemberHandling = MissingMemberHandling.Error
};

public async Task<RokkaResponse> BuildRokkaResponse(HttpResponseMessage httpResponseMessage)
Expand All @@ -26,8 +27,19 @@ public async Task<RokkaResponse> BuildRokkaResponse(HttpResponseMessage httpResp
private async Task<T> DeserializeBody<T>(HttpResponseMessage httpResponseMessage) where T : new()
{
var bodyString = await httpResponseMessage.Content.ReadAsStringAsync();
var deserializeObject = JsonConvert.DeserializeObject<T>(bodyString, _jsonSerializerSettings);
return deserializeObject ?? new T();
try
{
var deserializeObject = JsonConvert.DeserializeObject<T>(bodyString, _jsonSerializerSettings);
return deserializeObject ?? new T();
}
catch (JsonReaderException e)
{
throw new RokkaClientException($"Response from Rokka is not JSON. Reason: {e.Message}");
}
catch (JsonSerializationException e)
{
throw new RokkaClientException($"Unknown JSON response from Rokka. Reason: {e.Message}");
}
}

private async Task<RokkaResponse> BuildSuccessResponse(HttpResponseMessage httpResponseMessage)
Expand Down

0 comments on commit 43ebe65

Please sign in to comment.