Search in sources :

Example 1 with HttpMessageReader

use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.

the class WebFluxConfigurationSupportTests method customMessageConverterConfig.

@Test
public void customMessageConverterConfig() throws Exception {
    ApplicationContext context = loadConfig(CustomMessageConverterConfig.class);
    String name = "requestMappingHandlerAdapter";
    RequestMappingHandlerAdapter adapter = context.getBean(name, RequestMappingHandlerAdapter.class);
    assertNotNull(adapter);
    List<HttpMessageReader<?>> messageReaders = adapter.getMessageReaders();
    assertEquals(2, messageReaders.size());
    assertHasMessageReader(messageReaders, String.class, TEXT_PLAIN);
    assertHasMessageReader(messageReaders, TestBean.class, APPLICATION_XML);
}
Also used : DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 2 with HttpMessageReader

use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.

the class WebFluxConfigurationSupportTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() throws Exception {
    ApplicationContext context = loadConfig(WebFluxConfig.class);
    String name = "requestMappingHandlerAdapter";
    RequestMappingHandlerAdapter adapter = context.getBean(name, RequestMappingHandlerAdapter.class);
    assertNotNull(adapter);
    List<HttpMessageReader<?>> readers = adapter.getMessageReaders();
    assertEquals(7, readers.size());
    assertHasMessageReader(readers, byte[].class, APPLICATION_OCTET_STREAM);
    assertHasMessageReader(readers, ByteBuffer.class, APPLICATION_OCTET_STREAM);
    assertHasMessageReader(readers, String.class, TEXT_PLAIN);
    assertHasMessageReader(readers, Resource.class, IMAGE_PNG);
    assertHasMessageReader(readers, TestBean.class, APPLICATION_XML);
    assertHasMessageReader(readers, TestBean.class, APPLICATION_JSON);
    assertHasMessageReader(readers, TestBean.class, null);
    WebBindingInitializer bindingInitializer = adapter.getWebBindingInitializer();
    assertNotNull(bindingInitializer);
    WebExchangeDataBinder binder = new WebExchangeDataBinder(new Object());
    bindingInitializer.initBinder(binder);
    name = "webFluxConversionService";
    ConversionService service = context.getBean(name, ConversionService.class);
    assertSame(service, binder.getConversionService());
    name = "webFluxValidator";
    Validator validator = context.getBean(name, Validator.class);
    assertSame(validator, binder.getValidator());
}
Also used : DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConversionService(org.springframework.core.convert.ConversionService) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) Validator(org.springframework.validation.Validator) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) Test(org.junit.Test)

Example 3 with HttpMessageReader

use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.

the class DefaultServerRequestTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockRequest.getHeaders()).thenReturn(httpHeaders);
    when(mockRequest.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultRequest.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 4 with HttpMessageReader

use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.

the class DefaultServerRequestTests method bodyUnacceptable.

@Test
public void bodyUnacceptable() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockRequest.getHeaders()).thenReturn(httpHeaders);
    when(mockRequest.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.emptySet();
    when(mockHandlerStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Flux<String> resultFlux = defaultRequest.bodyToFlux(String.class);
    StepVerifier.create(resultFlux).expectError(UnsupportedMediaTypeStatusException.class).verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 5 with HttpMessageReader

use of org.springframework.http.codec.HttpMessageReader in project spring-framework by spring-projects.

the class DefaultClientResponseTests method body.

@Test
public void body() throws Exception {
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    when(mockResponse.getHeaders()).thenReturn(httpHeaders);
    when(mockResponse.getBody()).thenReturn(body);
    Set<HttpMessageReader<?>> messageReaders = Collections.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
    when(mockExchangeStrategies.messageReaders()).thenReturn(messageReaders::stream);
    Mono<String> resultMono = defaultClientResponse.body(toMono(String.class));
    assertEquals("foo", resultMono.block());
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) StringDecoder(org.springframework.core.codec.StringDecoder) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

HttpMessageReader (org.springframework.http.codec.HttpMessageReader)16 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)14 Test (org.junit.Test)12 StringDecoder (org.springframework.core.codec.StringDecoder)11 HttpHeaders (org.springframework.http.HttpHeaders)9 DataBuffer (org.springframework.core.io.buffer.DataBuffer)8 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)7 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)7 List (java.util.List)5 ApplicationContext (org.springframework.context.ApplicationContext)3 ResolvableType (org.springframework.core.ResolvableType)3 MediaType (org.springframework.http.MediaType)3 Validator (org.springframework.validation.Validator)3 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Supplier (java.util.function.Supplier)2 Collectors (java.util.stream.Collectors)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2