Search in sources :

Example 26 with TypeReference

use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.

the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.

@Test
public void bodyParameterizedTypeReference() {
    String body = "foo";
    Publisher<String> publisher = Mono.just(body);
    TypeReference<String> typeReference = new TypeReference<>() {
    };
    ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, typeReference).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
    result.writeTo(request, strategies).block();
    assertThat(request.getBody()).isNotNull();
    StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
Also used : EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Example 27 with TypeReference

use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method toEntityTypeReference.

@Test
public void toEntityTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<String> result = defaultClientResponse.toEntity(new TypeReference<String>() {
    }).block();
    assertThat(result.getBody()).isEqualTo("foo");
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) TypeReference(cn.taketoday.core.TypeReference) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 28 with TypeReference

use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method toEntityListTypeReference.

@Test
public void toEntityListTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(new TypeReference<String>() {
    }).block();
    assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 29 with TypeReference

use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.

the class HttpMessageConverterExtractorTests method generics.

@Test
@SuppressWarnings("unchecked")
void generics() throws IOException {
    responseHeaders.setContentType(contentType);
    String expected = "Foo";
    TypeReference<List<String>> reference = new TypeReference<List<String>>() {
    };
    Type type = reference.getType();
    GenericHttpMessageConverter<String> converter = mock(GenericHttpMessageConverter.class);
    HttpMessageConverterExtractor<?> extractor = new HttpMessageConverterExtractor<List<String>>(type, asList(converter));
    given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value());
    given(response.getHeaders()).willReturn(responseHeaders);
    given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
    given(converter.canRead(type, null, contentType)).willReturn(true);
    given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected);
    Object result = extractor.extractData(response);
    assertThat(result).isEqualTo(expected);
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) Type(java.lang.reflect.Type) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MediaType(cn.taketoday.http.MediaType) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) Arrays.asList(java.util.Arrays.asList) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Example 30 with TypeReference

use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.

the class RestTemplateTests method exchangeParameterizedType.

@Test
@SuppressWarnings("rawtypes")
void exchangeParameterizedType() throws Exception {
    GenericHttpMessageConverter converter = mock(GenericHttpMessageConverter.class);
    template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
    TypeReference<List<Integer>> intList = new TypeReference<List<Integer>>() {
    };
    given(converter.canRead(intList.getType(), null, null)).willReturn(true);
    given(converter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    given(converter.canWrite(String.class, String.class, null)).willReturn(true);
    HttpHeaders requestHeaders = HttpHeaders.create();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    List<Integer> expected = Collections.singletonList(42);
    HttpHeaders responseHeaders = HttpHeaders.create();
    responseHeaders.setContentType(MediaType.TEXT_PLAIN);
    responseHeaders.setContentLength(10);
    mockResponseStatus(HttpStatus.OK);
    given(response.getHeaders()).willReturn(responseHeaders);
    given(response.getBody()).willReturn(new ByteArrayInputStream(Integer.toString(42).getBytes()));
    given(converter.canRead(intList.getType(), null, MediaType.TEXT_PLAIN)).willReturn(true);
    given(converter.read(eq(intList.getType()), eq(null), any(HttpInputMessage.class))).willReturn(expected);
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.set("MyHeader", "MyValue");
    HttpEntity<String> requestEntity = new HttpEntity<>("Hello World", entityHeaders);
    ResponseEntity<List<Integer>> result = template.exchange("https://example.com", POST, requestEntity, intList);
    assertThat(result.getBody()).as("Invalid POST result").isEqualTo(expected);
    assertThat(result.getHeaders().getContentType()).as("Invalid Content-Type").isEqualTo(MediaType.TEXT_PLAIN);
    assertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(requestHeaders.getFirst("MyHeader")).as("Invalid custom header").isEqualTo("MyValue");
    assertThat(result.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
    verify(response).close();
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

TypeReference (cn.taketoday.core.TypeReference)30 Test (org.junit.jupiter.api.Test)28 List (java.util.List)22 MediaType (cn.taketoday.http.MediaType)16 ArrayList (java.util.ArrayList)16 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)14 MockHttpInputMessage (cn.taketoday.http.MockHttpInputMessage)12 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)6 HttpEntity (cn.taketoday.http.HttpEntity)6 Resource (cn.taketoday.core.io.Resource)4 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)4 HttpHeaders (cn.taketoday.http.HttpHeaders)4 HttpInputMessage (cn.taketoday.http.HttpInputMessage)4 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)4 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)4 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)2 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2