From 5d3990a3212d56c197528d4e1a62985290e7800c Mon Sep 17 00:00:00 2001 From: "$(git --no-pager log --format=format:'%an' -n 1)" Date: Thu, 21 Nov 2024 16:44:55 -0800 Subject: [PATCH] Remove incorrect toString() for URL in RestClientGraphQLClient error handling and added more tests --- .../dgs/client/RestClientGraphQLClient.kt | 2 +- .../dgs/client/RestClientGraphQLClientTest.kt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/graphql-dgs-client/src/main/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClient.kt b/graphql-dgs-client/src/main/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClient.kt index a2ad2abe4..0133624b1 100644 --- a/graphql-dgs-client/src/main/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClient.kt +++ b/graphql-dgs-client/src/main/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClient.kt @@ -99,7 +99,7 @@ class RestClientGraphQLClient( if (!responseEntity.statusCode.is2xxSuccessful) { throw GraphQLClientException( statusCode = responseEntity.statusCode.value(), - url = restClient.toString(), + url = "", response = responseEntity.body ?: "", request = serializedRequest, ) diff --git a/graphql-dgs-client/src/test/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClientTest.kt b/graphql-dgs-client/src/test/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClientTest.kt index 3f0a5af78..1784fa262 100644 --- a/graphql-dgs-client/src/test/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClientTest.kt +++ b/graphql-dgs-client/src/test/kotlin/com/netflix/graphql/dgs/client/RestClientGraphQLClientTest.kt @@ -34,6 +34,7 @@ import graphql.schema.idl.RuntimeWiring import graphql.schema.idl.SchemaParser import graphql.schema.idl.TypeDefinitionRegistry import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatException import org.intellij.lang.annotations.Language import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -45,6 +46,7 @@ import org.springframework.boot.test.web.server.LocalServerPort import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.client.HttpClientErrorException import org.springframework.web.client.RestClient import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -82,6 +84,27 @@ class RestClientGraphQLClientTest { assertThat(result).isEqualTo("Hi!") } + @Test + fun `Unsuccessful request with default status handling`() { + val client = RestClientGraphQLClient(restClient.mutate().baseUrl("http://localhost:$port/wrongpath").build()) + assertThatException().isThrownBy { client.executeQuery("{hello}") }.isInstanceOf(HttpClientErrorException::class.java) + } + + @Test + fun `Unsuccessful request with non default status handling`() { + val client = + RestClientGraphQLClient( + restClient + .mutate() + .defaultStatusHandler( + { true }, + { _, _ -> }, + ).baseUrl("http://localhost:$port/wrongpath") + .build(), + ) + assertThatException().isThrownBy { client.executeQuery("{hello}") }.isInstanceOf(GraphQLClientException::class.java) + } + @Test fun `Extra header can be provided`() { val client =