Search in sources :

Example 11 with HttpStatus

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

the class WebClientDataBufferAllocatingTests method testOnStatus.

private void testOnStatus(Throwable expected, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
    HttpStatus errorStatus = HttpStatus.BAD_GATEWAY;
    this.server.enqueue(new MockResponse().setResponseCode(errorStatus.value()).setHeader("Content-Type", "application/json").setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5));
    Mono<String> mono = this.webClient.get().uri("/json").accept(MediaType.APPLICATION_JSON).retrieve().onStatus(status -> status.equals(errorStatus), exceptionFunction).bodyToMono(String.class);
    StepVerifier.create(mono).expectErrorSatisfies(actual -> assertThat(actual).isSameAs(expected)).verify(DELAY);
    assertThat(this.server.getRequestCount()).isEqualTo(1);
}
Also used : NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) AbstractDataBufferAllocatingTests(cn.taketoday.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests) WebClient(cn.taketoday.web.reactive.function.client.WebClient) StepVerifier(reactor.test.StepVerifier) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) ChannelOption(io.netty.channel.ChannelOption) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) BeforeAll(org.junit.jupiter.api.BeforeAll) ReactorResourceFactory(cn.taketoday.http.client.reactive.ReactorResourceFactory) Duration(java.time.Duration) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) TypeReference(cn.taketoday.core.TypeReference) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) ReactorClientHttpConnector(cn.taketoday.http.client.reactive.ReactorClientHttpConnector) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) HttpStatus(cn.taketoday.http.HttpStatus) MediaType(cn.taketoday.http.MediaType) MockResponse(okhttp3.mockwebserver.MockResponse) Collections(java.util.Collections) MockResponse(okhttp3.mockwebserver.MockResponse) HttpStatus(cn.taketoday.http.HttpStatus)

Example 12 with HttpStatus

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

the class RelativeRedirectFilterTests method doFilterSendRedirectWhenCustomSendRedirectHttpStatusThenLocationAnd301.

@Test
public void doFilterSendRedirectWhenCustomSendRedirectHttpStatusThenLocationAnd301() throws Exception {
    String location = "/foo";
    HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
    this.filter.setRedirectStatus(status);
    sendRedirect(location);
    InOrder inOrder = Mockito.inOrder(this.response);
    inOrder.verify(this.response).setStatus(status.value());
    inOrder.verify(this.response).setHeader(HttpHeaders.LOCATION, location);
}
Also used : InOrder(org.mockito.InOrder) HttpStatus(cn.taketoday.http.HttpStatus) Test(org.junit.jupiter.api.Test)

Example 13 with HttpStatus

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

the class ResponseStatusException method getMessage.

@Override
public String getMessage() {
    HttpStatus code = HttpStatus.resolve(getRawStatusCode());
    String msg = (code != null ? code : getRawStatusCode()) + (this.reason != null ? " \"" + this.reason + "\"" : "");
    return ExceptionUtils.buildMessage(msg, getCause());
}
Also used : HttpStatus(cn.taketoday.http.HttpStatus)

Example 14 with HttpStatus

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

the class ExchangeFilterFunctionsTests method statusHandlerMatch.

@Test
public void statusHandlerMatch() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
    ClientResponse response = mock(ClientResponse.class);
    given(response.statusCode()).willReturn(HttpStatus.NOT_FOUND);
    ExchangeFunction exchange = r -> Mono.just(response);
    ExchangeFilterFunction errorHandler = ExchangeFilterFunctions.statusError(HttpStatus::is4xxClientError, r -> new MyException());
    Mono<ClientResponse> result = errorHandler.filter(request, exchange);
    StepVerifier.create(result).expectError(MyException.class).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) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) HttpStatus(cn.taketoday.http.HttpStatus) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Example 15 with HttpStatus

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

the class WebClientDataBufferAllocatingTests method testOnStatus.

private void testOnStatus(Throwable expected, Function<ClientResponse, Mono<? extends Throwable>> exceptionFunction) {
    HttpStatus errorStatus = HttpStatus.BAD_GATEWAY;
    this.server.enqueue(new MockResponse().setResponseCode(errorStatus.value()).setHeader("Content-Type", "application/json").setChunkedBody("{\"error\" : {\"status\" : 502, \"message\" : \"Bad gateway.\"}}", 5));
    Mono<String> mono = this.webClient.get().uri("/json").accept(MediaType.APPLICATION_JSON).retrieve().onStatus(status -> status.equals(errorStatus), exceptionFunction).bodyToMono(String.class);
    StepVerifier.create(mono).expectErrorSatisfies(actual -> assertThat(actual).isSameAs(expected)).verify(DELAY);
    assertThat(this.server.getRequestCount()).isEqualTo(1);
}
Also used : NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) AbstractDataBufferAllocatingTests(cn.taketoday.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests) WebClient(cn.taketoday.web.reactive.function.client.WebClient) StepVerifier(reactor.test.StepVerifier) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) ChannelOption(io.netty.channel.ChannelOption) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) BeforeAll(org.junit.jupiter.api.BeforeAll) ReactorResourceFactory(cn.taketoday.http.client.reactive.ReactorResourceFactory) Duration(java.time.Duration) Map(java.util.Map) MockWebServer(okhttp3.mockwebserver.MockWebServer) TypeReference(cn.taketoday.core.TypeReference) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) PER_CLASS(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS) Mono(reactor.core.publisher.Mono) ResponseEntity(cn.taketoday.http.ResponseEntity) ReactorClientHttpConnector(cn.taketoday.http.client.reactive.ReactorClientHttpConnector) WebClientResponseException(cn.taketoday.web.reactive.function.client.WebClientResponseException) HttpStatus(cn.taketoday.http.HttpStatus) MediaType(cn.taketoday.http.MediaType) MockResponse(okhttp3.mockwebserver.MockResponse) Collections(java.util.Collections) MockResponse(okhttp3.mockwebserver.MockResponse) 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