Search in sources :

Example 21 with MockServerHttpRequest

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

the class RequestPredicatesTests method pathEncoded.

@Test
public void pathEncoded() {
    URI uri = URI.create("https://localhost/foo%20bar");
    RequestPredicate predicate = RequestPredicates.path("/foo bar");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, uri).build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 22 with MockServerHttpRequest

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

the class RequestPredicatesTests method method.

@Test
public void method() {
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    HttpMethod httpMethod = HttpMethod.GET;
    RequestPredicate predicate = RequestPredicates.method(httpMethod);
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
    mockRequest = MockServerHttpRequest.post("https://example.com").build();
    request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 23 with MockServerHttpRequest

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

the class RequestPredicatesTests method headersCors.

@Test
public void headersCors() {
    RequestPredicate predicate = RequestPredicates.headers(headers -> false);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.options("https://example.com").header("Origin", "https://example.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 24 with MockServerHttpRequest

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

the class RequestPredicatesTests method pathExtension.

@Test
public void pathExtension() {
    RequestPredicate predicate = RequestPredicates.pathExtension("txt");
    URI uri = URI.create("https://localhost/file.txt");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.method(HttpMethod.GET, uri).build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
    uri = URI.create("https://localhost/FILE.TXT");
    mockRequest = MockServerHttpRequest.method(HttpMethod.GET, uri).build();
    request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
    predicate = RequestPredicates.pathExtension("bar");
    assertThat(predicate.test(request)).isFalse();
    uri = URI.create("https://localhost/file.foo");
    mockRequest = MockServerHttpRequest.method(HttpMethod.GET, uri).build();
    request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 25 with MockServerHttpRequest

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

the class RequestPredicatesTests method path.

@Test
public void path() {
    URI uri = URI.create("https://localhost/path");
    RequestPredicate predicate = RequestPredicates.path("/p*");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get(uri.toString()).build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
    mockRequest = MockServerHttpRequest.head("https://example.com").build();
    request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23