Search in sources :

Example 36 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandlerTests method responseEntityNoContent.

@Test
public void responseEntityNoContent() throws Exception {
    MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
    ResponseEntity<?> emitter = ResponseEntity.noContent().build();
    this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
    assertFalse(this.request.isAsyncStarted());
    assertEquals(204, this.response.getStatus());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 37 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandlerTests method responseEntity.

@Test
public void responseEntity() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
    ResponseEntity<StreamingResponseBody> emitter = ResponseEntity.ok().header("foo", "bar").body(outputStream -> {
        outputStream.write("foo".getBytes(StandardCharsets.UTF_8));
        latch.countDown();
    });
    this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
    assertTrue(this.request.isAsyncStarted());
    assertEquals(200, this.response.getStatus());
    assertEquals("bar", this.response.getHeader("foo"));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals("foo", this.response.getContentAsString());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 38 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandlerTests method streamingResponseBody.

@Test
public void streamingResponseBody() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    MethodParameter returnType = returnType(TestController.class, "handle");
    StreamingResponseBody streamingBody = outputStream -> {
        outputStream.write("foo".getBytes(StandardCharsets.UTF_8));
        latch.countDown();
    };
    this.handler.handleReturnValue(streamingBody, returnType, this.mavContainer, this.webRequest);
    assertTrue(this.request.isAsyncStarted());
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals("foo", this.response.getContentAsString());
}
Also used : MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) StandardCharsets(java.nio.charset.StandardCharsets) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Assert.assertFalse(org.junit.Assert.assertFalse) MethodParameter(org.springframework.core.MethodParameter) ResponseEntity(org.springframework.http.ResponseEntity) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) Method(java.lang.reflect.Method) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 39 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandlerTests method responseEntityWithHeadersAndNoContent.

@Test
public void responseEntityWithHeadersAndNoContent() throws Exception {
    ResponseEntity<?> emitter = ResponseEntity.noContent().header("foo", "bar").build();
    MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
    this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);
    assertEquals(Collections.singletonList("bar"), this.response.getHeaders("foo"));
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 40 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class UriComponentsBuilderMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    this.resolver = new UriComponentsBuilderMethodArgumentResolver();
    this.servletRequest = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(this.servletRequest);
    Method method = this.getClass().getDeclaredMethod("handle", UriComponentsBuilder.class, ServletUriComponentsBuilder.class, int.class);
    this.builderParam = new MethodParameter(method, 0);
    this.servletBuilderParam = new MethodParameter(method, 1);
    this.intParam = new MethodParameter(method, 2);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19