Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebTestClient throws IllegalArgumentException for invalid Parameters [SPR-17363] #21896

Closed
spring-projects-issues opened this issue Oct 11, 2018 · 1 comment
Assignees
Labels
in: test in: web type: bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 11, 2018

Peter Luttrell opened SPR-17363 and commented

Experienced with Spring 5.1.0 RELEASE that is included with SpringBoot 2.1.0.M4.

With the following controller: 

@RestController
class SimpleController {

  @PostMapping(
      path = "/test",
      params = "p",
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE
  )
  Mono<Simple> test(@RequestBody Simple simple) {
    return Mono.just(simple);
  }

  @Data
  @NoArgsConstructor
  @AllArgsConstructor
  private static class Simple {
    private Long num;
  }

}

And the following tests:

@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
class DocTests {

  @Autowired
  protected WebTestClient webTestClient;

  @BeforeEach
  void setUp(ApplicationContext applicationContext, RestDocumentationContextProvider restDocumentation) {
    this.webTestClient = WebTestClient.bindToApplicationContext(applicationContext)
        .configureClient()
        .filter(documentationConfiguration(restDocumentation))
        .build();
  }

  private static final String BODY = "{\"num\":123}";

  @Test
  void springErrorGenerator() {
    webTestClient.post()
        .uri("/test")
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .syncBody(BODY)
        .exchange()
        .expectStatus().isBadRequest()
        .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
        .expectBody()
        .consumeWith(document("spring-error"));
  }

  @Test
  void demonstrateOk() {
    webTestClient.post()
        .uri(uriBuilder -> uriBuilder.path("/test").queryParam("p", "test").build())
        .contentType(MediaType.APPLICATION_JSON_UTF8)
        .syncBody(BODY)
        .exchange()
        .expectStatus().isOk();
  }

}

I'm experiencing the following exception:

java.lang.IllegalArgumentException: Request body incomplete.java.lang.IllegalArgumentException: Request body incomplete.
 at org.springframework.util.Assert.isTrue(Assert.java:118) at org.springframework.test.web.reactive.server.ExchangeResult.getRequestBodyContent(ExchangeResult.java:149) at org.springframework.restdocs.webtestclient.WebTestClientRequestConverter.convert(WebTestClientRequestConverter.java:73) at org.springframework.restdocs.webtestclient.WebTestClientRequestConverter.convert(WebTestClientRequestConverter.java:60) at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:199) at org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.lambda$document$0(WebTestClientRestDocumentation.java:81) at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.lambda$consumeWith$3(DefaultWebTestClient.java:540) at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:194) at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodyContentSpec.consumeWith(DefaultWebTestClient.java:540) at example.DocTests.springErrorGenerator(DocTests.java:50) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71) at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Note that with request body is the same for both and is valid. The 'springErrorGenerator' test generates the error. Note that with this test, I am attempting to capture using Spring Rest Docs the Spring response body when the required URL parameter is missing, such as can be displayed by making a test call such as this: 

$ echo '{"num":2}' | http POST localhost:8080/test
HTTP/1.1 400 Bad Request
Content-Length: 175
Content-Type: application/json;charset=UTF-8

{
    "error": "Bad Request",
    "message": "Unsatisfied query parameter conditions: [[p]], actual parameters: {}",
    "path": "/test",
    "status": 400,
    "timestamp": "2018-10-11T07:11:47.206+0000"
}

 

 


Affects: 5.1 GA

Referenced from: commits 8df0bc8, c567e65

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 11, 2018

Rossen Stoyanchev commented

This is an issue indeed since the test uses a mock server request connected directly to the client-side request body Flux, and the 400 occurs before the request body is read.

@spring-projects-issues spring-projects-issues added type: bug in: test in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.1.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test in: web type: bug
Projects
None yet
Development

No branches or pull requests

2 participants