Search in sources :

Example 36 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxTagsTests method uriTagValueIsRootWhenRequestHasNoPatternAndSlashPathInfo.

@Test
void uriTagValueIsRootWhenRequestHasNoPatternAndSlashPathInfo() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    Tag tag = WebFluxTags.uri(exchange);
    assertThat(tag.getValue()).isEqualTo("root");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Tag(io.micrometer.core.instrument.Tag) Test(org.junit.jupiter.api.Test)

Example 37 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxTagsTests method uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo.

@Test
void uriTagValueIsUnknownWhenRequestHasNoPatternAndNonRootPathInfo() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/example").build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    Tag tag = WebFluxTags.uri(exchange);
    assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Tag(io.micrometer.core.instrument.Tag) Test(org.junit.jupiter.api.Test)

Example 38 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class HttpHandlerConnectorTests method adaptRequest.

@Test
public void adaptRequest() {
    TestHttpHandler handler = new TestHttpHandler(response -> {
        response.setStatusCode(HttpStatus.OK);
        return response.setComplete();
    });
    new HttpHandlerConnector(handler).connect(HttpMethod.POST, URI.create("/custom-path"), request -> {
        request.getHeaders().put("custom-header", Arrays.asList("h0", "h1"));
        request.getCookies().add("custom-cookie", new HttpCookie("custom-cookie", "c0"));
        return request.writeWith(Mono.just(toDataBuffer("Custom body")));
    }).block(Duration.ofSeconds(5));
    MockServerHttpRequest request = (MockServerHttpRequest) handler.getSavedRequest();
    assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
    assertThat(request.getURI().toString()).isEqualTo("/custom-path");
    HttpHeaders headers = request.getHeaders();
    assertThat(headers.get("custom-header")).isEqualTo(Arrays.asList("h0", "h1"));
    assertThat(request.getCookies().getFirst("custom-cookie")).isEqualTo(new HttpCookie("custom-cookie", "c0"));
    assertThat(headers.get(HttpHeaders.COOKIE)).isEqualTo(Collections.singletonList("custom-cookie=c0"));
    DataBuffer buffer = request.getBody().blockFirst(Duration.ZERO);
    assertThat(buffer.toString(UTF_8)).isEqualTo("Custom body");
}
Also used : Arrays(java.util.Arrays) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Function(java.util.function.Function) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Test(org.junit.jupiter.api.Test) HttpCookie(org.springframework.http.HttpCookie) HttpStatus(org.springframework.http.HttpStatus) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) URI(java.net.URI) Collections(java.util.Collections) ResponseCookie(org.springframework.http.ResponseCookie) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpCookie(org.springframework.http.HttpCookie) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 39 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method annotatedResponseStatusCode.

@Test
void annotatedResponseStatusCode() {
    Exception error = new CustomException();
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), ErrorAttributeOptions.defaults());
    assertThat(attributes.get("error")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
    assertThat(attributes).doesNotContainKey("message");
    assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.jupiter.api.Test)

Example 40 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method processResponseStatusExceptionWithNoNestedCause.

@Test
void processResponseStatusExceptionWithNoNestedCause() {
    ResponseStatusException error = new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, "could not process request");
    this.errorAttributes = new DefaultErrorAttributes();
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    ServerRequest serverRequest = buildServerRequest(request, error);
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE));
    assertThat(attributes.get("status")).isEqualTo(406);
    assertThat(attributes.get("message")).isEqualTo("could not process request");
    assertThat(attributes.get("exception")).isEqualTo(ResponseStatusException.class.getName());
    assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
    assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4