Search in sources :

Example 1 with MockServerHttpRequest

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

the class ApiVersionWebFilterTest method setUp.

@Before
public void setUp() {
    MockServerHttpRequest request = MockServerHttpRequest.get(V2_API_PATH_PATTERN).header("header", "9.9").build();
    this.exchange = MockServerWebExchange.from(request);
    MockitoAnnotations.initMocks(this);
    exchange.getResponse().setStatusCode(HttpStatus.OK);
    when(chain.filter(exchange)).thenReturn(Mono.empty());
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Before(org.junit.Before)

Example 2 with MockServerHttpRequest

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

the class RewritePathGatewayFilterFactoryTests method testRewriteFilter.

private ServerWebExchange testRewriteFilter(String regex, String replacement, String actualPath, String expectedPath) {
    GatewayFilter filter = new RewritePathGatewayFilterFactory().apply(c -> c.setRegexp(regex).setReplacement(replacement));
    URI url = UriComponentsBuilder.fromUriString("http://localhost" + actualPath).build(true).toUri();
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, url).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    URI requestUrl = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(requestUrl).hasScheme("http").hasHost("localhost").hasNoPort().hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
    return webExchange;
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 3 with MockServerHttpRequest

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

the class SetPathGatewayFilterFactoryTests method testFilter.

private void testFilter(String template, String expectedPath, HashMap<String, String> variables) {
    GatewayFilter filter = new SetPathGatewayFilterFactory().apply(c -> c.setTemplate(template));
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost").build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    try {
        Constructor<PathMatchInfo> constructor = ReflectionUtils.accessibleConstructor(PathMatchInfo.class, Map.class, Map.class);
        constructor.setAccessible(true);
        PathMatchInfo pathMatchInfo = constructor.newInstance(variables, Collections.emptyMap());
        exchange.getAttributes().put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, pathMatchInfo);
    } catch (Exception e) {
        ReflectionUtils.rethrowRuntimeException(e);
    }
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) PathMatchInfo(org.springframework.web.util.pattern.PathPattern.PathMatchInfo) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 4 with MockServerHttpRequest

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

the class ForwardedHeadersFilterTests method forwardedHeaderDoesNotExist.

@Test
public void forwardedHeaderDoesNotExist() throws UnknownHostException {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(HttpHeaders.HOST, "myhost").build();
    ForwardedHeadersFilter filter = new ForwardedHeadersFilter();
    HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
    assertThat(headers.get(FORWARDED_HEADER)).hasSize(1);
    List<Forwarded> forwardeds = ForwardedHeadersFilter.parse(headers.get(FORWARDED_HEADER));
    assertThat(forwardeds).hasSize(1);
    Forwarded forwarded = forwardeds.get(0);
    assertThat(forwarded.getValues()).containsEntry("host", "myhost").containsEntry("proto", "http").containsEntry("for", "\"10.0.0.1:80\"");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Forwarded(org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded) Test(org.junit.Test)

Example 5 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)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)72 Test (org.junit.jupiter.api.Test)38 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