Search in sources :

Example 1 with HttpStatus

use of cn.taketoday.http.HttpStatus in project today-infrastructure by TAKETODAY.

the class DefaultClientResponse method createException.

@Override
public Mono<WebClientResponseException> createException() {
    return bodyToMono(byte[].class).defaultIfEmpty(EMPTY).onErrorReturn(ex -> !(ex instanceof Error), EMPTY).map(bodyBytes -> {
        HttpRequest request = this.requestSupplier.get();
        Charset charset = headers().contentType().map(MimeType::getCharset).orElse(null);
        int statusCode = rawStatusCode();
        HttpStatus httpStatus = HttpStatus.resolve(statusCode);
        if (httpStatus != null) {
            return WebClientResponseException.create(statusCode, httpStatus.getReasonPhrase(), headers().asHttpHeaders(), bodyBytes, charset, request);
        } else {
            return new UnknownHttpStatusCodeException(statusCode, headers().asHttpHeaders(), bodyBytes, charset, request);
        }
    });
}
Also used : Supplier(java.util.function.Supplier) ResponseCookie(cn.taketoday.http.ResponseCookie) OptionalLong(java.util.OptionalLong) HttpHeaders(cn.taketoday.http.HttpHeaders) ClientHttpResponse(cn.taketoday.http.client.reactive.ClientHttpResponse) Charset(java.nio.charset.Charset) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) TypeReference(cn.taketoday.core.TypeReference) BodyExtractor(cn.taketoday.web.reactive.function.BodyExtractor) HttpRequest(cn.taketoday.http.HttpRequest) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) Flux(reactor.core.publisher.Flux) List(java.util.List) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) MimeType(cn.taketoday.util.MimeType) HttpRequest(cn.taketoday.http.HttpRequest) HttpStatus(cn.taketoday.http.HttpStatus) Charset(java.nio.charset.Charset)

Example 2 with HttpStatus

use of cn.taketoday.http.HttpStatus in project today-infrastructure by TAKETODAY.

the class DefaultClientResponseTests method statusCode.

@Test
public void statusCode() {
    HttpStatus status = HttpStatus.CONTINUE;
    given(mockResponse.getStatusCode()).willReturn(status);
    assertThat(defaultClientResponse.statusCode()).isEqualTo(status);
}
Also used : HttpStatus(cn.taketoday.http.HttpStatus) Test(org.junit.jupiter.api.Test)

Example 3 with HttpStatus

use of cn.taketoday.http.HttpStatus in project today-infrastructure by TAKETODAY.

the class ExchangeFilterFunctionsTests method statusHandlerNoMatch.

@Test
public void statusHandlerNoMatch() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
    ClientResponse response = mock(ClientResponse.class);
    given(response.statusCode()).willReturn(HttpStatus.NOT_FOUND);
    Mono<ClientResponse> result = ExchangeFilterFunctions.statusError(HttpStatus::is5xxServerError, req -> new MyException()).filter(request, req -> Mono.just(response));
    StepVerifier.create(result).expectNext(response).expectComplete().verify();
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 4 with HttpStatus

use of cn.taketoday.http.HttpStatus in project today-infrastructure by TAKETODAY.

the class ClientResponseWrapperTests method statusCode.

@Test
public void statusCode() {
    HttpStatus status = HttpStatus.BAD_REQUEST;
    given(mockResponse.statusCode()).willReturn(status);
    assertThat(wrapper.statusCode()).isSameAs(status);
}
Also used : HttpStatus(cn.taketoday.http.HttpStatus) Test(org.junit.jupiter.api.Test)

Example 5 with HttpStatus

use of cn.taketoday.http.HttpStatus in project today-infrastructure by TAKETODAY.

the class ErrorResponseException method getMessage.

@Override
public String getMessage() {
    HttpStatus httpStatus = HttpStatus.resolve(this.status);
    String message = (httpStatus != null ? httpStatus : String.valueOf(this.status)) + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
    return ExceptionUtils.buildMessage(message, getCause());
}
Also used : HttpStatus(cn.taketoday.http.HttpStatus)

Aggregations

HttpStatus (cn.taketoday.http.HttpStatus)17 Test (org.junit.jupiter.api.Test)8 Mono (reactor.core.publisher.Mono)5 ClientResponse (cn.taketoday.web.reactive.function.client.ClientResponse)4 URI (java.net.URI)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 TypeReference (cn.taketoday.core.TypeReference)3 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)3 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)3 HttpHeaders (cn.taketoday.http.HttpHeaders)3 MediaType (cn.taketoday.http.MediaType)3 ResponseEntity (cn.taketoday.http.ResponseEntity)3 BodyExtractors (cn.taketoday.web.reactive.function.BodyExtractors)3 Collections (java.util.Collections)3 Map (java.util.Map)3 StepVerifier (reactor.test.StepVerifier)3 DataBufferFactory (cn.taketoday.core.io.buffer.DataBufferFactory)2 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)2 NettyDataBufferFactory (cn.taketoday.core.io.buffer.NettyDataBufferFactory)2 AbstractDataBufferAllocatingTests (cn.taketoday.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests)2