Search in sources :

Example 1 with WebClientResponseException

use of cn.taketoday.web.reactive.function.client.WebClientResponseException in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method createException.

@Test
public void createException() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
    given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
    WebClientResponseException exception = resultMono.block();
    assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(exception.getMessage()).isEqualTo("404 Not Found");
    assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", Collections.singletonList("text/plain")));
    assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 2 with WebClientResponseException

use of cn.taketoday.web.reactive.function.client.WebClientResponseException in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method createError.

@Test
public void createError() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
    given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<String> resultMono = defaultClientResponse.createError();
    StepVerifier.create(resultMono).consumeErrorWith(t -> {
        assertThat(t).isInstanceOf(WebClientResponseException.class);
        WebClientResponseException exception = (WebClientResponseException) t;
        assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
        assertThat(exception.getMessage()).isEqualTo("404 Not Found");
        assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", Collections.singletonList("text/plain")));
        assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
    }).verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseCookie(cn.taketoday.http.ResponseCookie) ByteBuffer(java.nio.ByteBuffer) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ExchangeStrategies(cn.taketoday.web.reactive.function.client.ExchangeStrategies) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) InetSocketAddress(java.net.InetSocketAddress) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors.toMono(cn.taketoday.web.reactive.function.BodyExtractors.toMono) Test(org.junit.jupiter.api.Test) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) DefaultClientResponse(cn.taketoday.web.reactive.function.client.DefaultClientResponse) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Optional(java.util.Optional) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 3 with WebClientResponseException

use of cn.taketoday.web.reactive.function.client.WebClientResponseException in project today-framework by TAKETODAY.

the class DefaultClientResponseTests method createException.

@Test
public void createException() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
    given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
    WebClientResponseException exception = resultMono.block();
    assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(exception.getMessage()).isEqualTo("404 Not Found");
    assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", Collections.singletonList("text/plain")));
    assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 4 with WebClientResponseException

use of cn.taketoday.web.reactive.function.client.WebClientResponseException in project today-framework by TAKETODAY.

the class DefaultClientResponseTests method createError.

@Test
public void createError() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    httpHeaders.setContentType(MediaType.TEXT_PLAIN);
    given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
    given(mockResponse.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
    given(mockResponse.getBody()).willReturn(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    Mono<String> resultMono = defaultClientResponse.createError();
    StepVerifier.create(resultMono).consumeErrorWith(t -> {
        assertThat(t).isInstanceOf(WebClientResponseException.class);
        WebClientResponseException exception = (WebClientResponseException) t;
        assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
        assertThat(exception.getMessage()).isEqualTo("404 Not Found");
        assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", Collections.singletonList("text/plain")));
        assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
    }).verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) BeforeEach(org.junit.jupiter.api.BeforeEach) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResponseCookie(cn.taketoday.http.ResponseCookie) ByteBuffer(java.nio.ByteBuffer) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) StringDecoder(cn.taketoday.core.codec.StringDecoder) ExchangeStrategies(cn.taketoday.web.reactive.function.client.ExchangeStrategies) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) InetSocketAddress(java.net.InetSocketAddress) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors.toMono(cn.taketoday.web.reactive.function.BodyExtractors.toMono) Test(org.junit.jupiter.api.Test) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpStatusCode(cn.taketoday.http.HttpStatusCode) DefaultClientResponse(cn.taketoday.web.reactive.function.client.DefaultClientResponse) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) HttpStatus(cn.taketoday.http.HttpStatus) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Optional(java.util.Optional) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayDecoder (cn.taketoday.core.codec.ByteArrayDecoder)4 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)4 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)4 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)4 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)4 WebClientResponseException (cn.taketoday.web.reactive.function.client.WebClientResponseException)4 Test (org.junit.jupiter.api.Test)4 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 TypeReference (cn.taketoday.core.TypeReference)2 StringDecoder (cn.taketoday.core.codec.StringDecoder)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpRange (cn.taketoday.http.HttpRange)2 HttpStatus (cn.taketoday.http.HttpStatus)2 MediaType (cn.taketoday.http.MediaType)2 ResponseCookie (cn.taketoday.http.ResponseCookie)2 ResponseEntity (cn.taketoday.http.ResponseEntity)2 ClientHttpResponse (cn.taketoday.http.client.reactive.ClientHttpResponse)2 BodyExtractors.toMono (cn.taketoday.web.reactive.function.BodyExtractors.toMono)2