Search in sources :

Example 1 with RequestBodyAdvice

use of cn.taketoday.web.handler.method.RequestBodyAdvice in project today-framework by TAKETODAY.

the class RequestResponseBodyAdviceChainTests method responseBodyAdvice.

@SuppressWarnings("unchecked")
@Test
public void responseBodyAdvice() {
    RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
    ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
    List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
    RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
    String expected = "body++";
    given(responseAdvice.supports(this.returnType, this.converterType)).willReturn(true);
    given(responseAdvice.beforeBodyWrite(eq(this.body), eq(this.returnType), eq(this.contentType), eq(this.converterType), same(requestContext))).willReturn(expected);
    String actual = (String) chain.beforeBodyWrite(this.body, this.returnType, this.contentType, this.converterType, requestContext);
    assertThat(actual).isEqualTo(expected);
}
Also used : RequestBodyAdvice(cn.taketoday.web.handler.method.RequestBodyAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with RequestBodyAdvice

use of cn.taketoday.web.handler.method.RequestBodyAdvice in project today-framework by TAKETODAY.

the class RequestResponseBodyAdviceChainTests method requestBodyAdvice.

@SuppressWarnings("unchecked")
@Test
public void requestBodyAdvice() throws IOException {
    RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
    ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
    List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
    RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
    HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest());
    given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true);
    given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped);
    assertThat(chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType)).isSameAs(wrapped);
    String modified = "body++";
    given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified);
    assertThat(chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType)).isEqualTo(modified);
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) ServletServerHttpRequest(cn.taketoday.http.server.ServletServerHttpRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestBodyAdvice(cn.taketoday.web.handler.method.RequestBodyAdvice) Test(org.junit.jupiter.api.Test)

Aggregations

RequestBodyAdvice (cn.taketoday.web.handler.method.RequestBodyAdvice)2 Test (org.junit.jupiter.api.Test)2 HttpInputMessage (cn.taketoday.http.HttpInputMessage)1 ServletServerHttpRequest (cn.taketoday.http.server.ServletServerHttpRequest)1 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)1