From 43ebe650452fe0dadbfcb023696548410c6f2237 Mon Sep 17 00:00:00 2001 From: Benoit Potty Date: Thu, 5 Oct 2023 12:09:50 +0200 Subject: [PATCH] Add more tests --- .../Factories/RokkaResponseFactoryTests.cs | 42 +++++++++++++++++++ .../Factories/RokkaResponseFactory.cs | 18 ++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/rokka-client-c-sharp.tests/Factories/RokkaResponseFactoryTests.cs b/rokka-client-c-sharp.tests/Factories/RokkaResponseFactoryTests.cs index f39b2ed..6f2f369 100644 --- a/rokka-client-c-sharp.tests/Factories/RokkaResponseFactoryTests.cs +++ b/rokka-client-c-sharp.tests/Factories/RokkaResponseFactoryTests.cs @@ -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}"; @@ -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( () => new RokkaResponseFactory().BuildRokkaResponse(responseMessage)); + + } + + [Fact] + public async void GivenAnUnknownSuccessResponse_WhenBuildRokkaResponse_ThrowsException() + { + var responseMessage = CreateUnknownSuccessResponseMessage(); + + await Assert.ThrowsAsync( () => new RokkaResponseFactory().BuildRokkaResponse(responseMessage)); + + } } \ No newline at end of file diff --git a/rokka-client-c-sharp/Factories/RokkaResponseFactory.cs b/rokka-client-c-sharp/Factories/RokkaResponseFactory.cs index 0204fcd..7fd531d 100644 --- a/rokka-client-c-sharp/Factories/RokkaResponseFactory.cs +++ b/rokka-client-c-sharp/Factories/RokkaResponseFactory.cs @@ -13,7 +13,8 @@ public class RokkaResponseFactory ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() - } + }, + MissingMemberHandling = MissingMemberHandling.Error }; public async Task BuildRokkaResponse(HttpResponseMessage httpResponseMessage) @@ -26,8 +27,19 @@ public async Task BuildRokkaResponse(HttpResponseMessage httpResp private async Task DeserializeBody(HttpResponseMessage httpResponseMessage) where T : new() { var bodyString = await httpResponseMessage.Content.ReadAsStringAsync(); - var deserializeObject = JsonConvert.DeserializeObject(bodyString, _jsonSerializerSettings); - return deserializeObject ?? new T(); + try + { + var deserializeObject = JsonConvert.DeserializeObject(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 BuildSuccessResponse(HttpResponseMessage httpResponseMessage)