This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from yandooo/graphql-spring-boot
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from BlasiusSecundus/feature/fix-invalid-json…
…-handling fix(#524): return valid error even if request body is invalid
- Loading branch information
Showing
3 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...e-webflux/src/test/java/graphql/kickstart/spring/webflux/boot/InvalidJsonRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package graphql.kickstart.spring.webflux.boot; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.assertj.core.util.Files; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class InvalidJsonRequestTest { | ||
|
||
@Autowired private WebTestClient webTestClient; | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"\"false\":true", "not-a-json"}) | ||
@DisplayName("Should return valid response to a request with invalid JSON or non-JSON body.") | ||
void testHandlingInvalidJsonRequest(String badRequestBody) throws IOException { | ||
// GIVEN | ||
final String expectedJson = | ||
Files.contentOf( | ||
new ClassPathResource("response-to-invalid-request.json").getFile(), | ||
StandardCharsets.UTF_8); | ||
// WHEN - THEN | ||
webTestClient | ||
.post() | ||
.uri("/graphql") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue(badRequestBody) | ||
.exchange() | ||
.expectStatus() | ||
.isEqualTo(HttpStatus.OK) | ||
.expectBody() | ||
.json(expectedJson); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...art-spring-boot-autoconfigure-webflux/src/test/resources/response-to-invalid-request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"errors": [ | ||
{ | ||
"message": "Bad request - invalid request body.", | ||
"locations": [] | ||
} | ||
], | ||
"data": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters