Search in sources :

Example 6 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class CookieRoutePredicateFactoryTests method noCookiesForYou.

@Test
public void noCookiesForYou() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    Predicate<ServerWebExchange> predicate = new CookieRoutePredicateFactory().apply(new Config().setName("mycookie").setRegexp("ch.p"));
    assertThat(predicate.test(exchange)).isFalse();
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Config(org.springframework.cloud.gateway.handler.predicate.CookieRoutePredicateFactory.Config) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 7 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class XForwardedHeadersFilterTests method xForwardedHeadersDoNotExist.

@Test
public void xForwardedHeadersDoNotExist() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(HttpHeaders.HOST, "myhost").build();
    XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER);
    assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("10.0.0.1");
    assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost:8080");
    assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("8080");
    assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 8 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class XForwardedHeadersFilterTests method defaultPort.

@Test
public void defaultPort() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(HttpHeaders.HOST, "myhost").build();
    XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER);
    assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("10.0.0.1");
    assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost");
    assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("80");
    assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 9 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class XForwardedHeadersFilterTests method appendsValues.

@Test
public void appendsValues() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(X_FORWARDED_FOR_HEADER, "192.168.0.2").header(X_FORWARDED_HOST_HEADER, "example.com").header(X_FORWARDED_PORT_HEADER, "443").header(X_FORWARDED_PROTO_HEADER, "https").build();
    XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER);
    assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("192.168.0.2,10.0.0.1");
    assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("example.com,localhost:8080");
    assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("443,8080");
    assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("https,http");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 10 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class XForwardedHeadersFilterTests method allDisabled.

@Test
public void allDisabled() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).build();
    XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
    filter.setForEnabled(false);
    filter.setHostEnabled(false);
    filter.setPortEnabled(false);
    filter.setProtoEnabled(false);
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers).isEmpty();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.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