|
1 | | -/* |
2 | | - * Copyright 2013-2017 the original author or authors. |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - * |
16 | | - */ |
17 | | - |
18 | 1 | package org.springframework.cloud.gateway.filter; |
19 | 2 |
|
20 | 3 | import java.net.URI; |
21 | 4 | import java.util.Collections; |
| 5 | +import java.util.LinkedHashSet; |
22 | 6 |
|
| 7 | +import org.junit.Before; |
23 | 8 | import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
24 | 10 | import org.mockito.ArgumentCaptor; |
| 11 | +import org.mockito.InjectMocks; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.junit.MockitoJUnitRunner; |
25 | 14 | import org.springframework.cloud.client.DefaultServiceInstance; |
| 15 | +import org.springframework.cloud.client.ServiceInstance; |
26 | 16 | import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; |
| 17 | +import org.springframework.cloud.gateway.support.NotFoundException; |
27 | 18 | import org.springframework.http.HttpMethod; |
28 | 19 | import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
29 | 20 | import org.springframework.mock.web.server.MockServerWebExchange; |
30 | 21 | import org.springframework.web.server.ServerWebExchange; |
31 | 22 | import org.springframework.web.util.UriComponentsBuilder; |
32 | 23 |
|
33 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.ArgumentMatchers.eq; |
34 | 27 | import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 30 | +import static org.mockito.Mockito.verifyZeroInteractions; |
35 | 31 | import static org.mockito.Mockito.when; |
| 32 | +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR; |
36 | 33 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; |
37 | 34 |
|
38 | 35 | import reactor.core.publisher.Mono; |
39 | 36 |
|
40 | 37 | /** |
41 | 38 | * @author Spencer Gibb |
| 39 | + * @author Tim Ysewyn |
42 | 40 | */ |
| 41 | +@RunWith(MockitoJUnitRunner.class) |
43 | 42 | public class LoadBalancerClientFilterTests { |
44 | 43 |
|
| 44 | + private ServerWebExchange exchange; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private GatewayFilterChain chain; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private LoadBalancerClient loadBalancerClient; |
| 51 | + |
| 52 | + @InjectMocks |
| 53 | + private LoadBalancerClientFilter loadBalancerClientFilter; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setup() { |
| 57 | + exchange = MockServerWebExchange.from(MockServerHttpRequest.get("loadbalancerclient.org").build()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void shouldNotFilterWhenGatewayRequestUrlIsMissing() { |
| 62 | + loadBalancerClientFilter.filter(exchange, chain); |
| 63 | + |
| 64 | + verify(chain).filter(exchange); |
| 65 | + verifyNoMoreInteractions(chain); |
| 66 | + verifyZeroInteractions(loadBalancerClient); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void shouldNotFilterWhenGatewayRequestUrlSchemeIsNotLb() { |
| 71 | + URI uri = UriComponentsBuilder.fromUriString("http://myservice").build().toUri(); |
| 72 | + exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri); |
| 73 | + |
| 74 | + loadBalancerClientFilter.filter(exchange, chain); |
| 75 | + |
| 76 | + verify(chain).filter(exchange); |
| 77 | + verifyNoMoreInteractions(chain); |
| 78 | + verifyZeroInteractions(loadBalancerClient); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(expected = NotFoundException.class) |
| 82 | + public void shouldThrowExceptionWhenNoServiceInstanceIsFound() { |
| 83 | + URI uri = UriComponentsBuilder.fromUriString("lb://myservice").build().toUri(); |
| 84 | + exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri); |
| 85 | + |
| 86 | + loadBalancerClientFilter.filter(exchange, chain); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void shouldFilter() { |
| 91 | + URI url = UriComponentsBuilder.fromUriString("lb://myservice").build().toUri(); |
| 92 | + exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, url); |
| 93 | + |
| 94 | + ServiceInstance serviceInstance = new DefaultServiceInstance("myservice", "localhost", 8080, true); |
| 95 | + when(loadBalancerClient.choose("myservice")).thenReturn(serviceInstance); |
| 96 | + |
| 97 | + URI requestUrl = UriComponentsBuilder.fromUriString("https://localhost:8080").build().toUri(); |
| 98 | + when(loadBalancerClient.reconstructURI(any(ServiceInstance.class), any(URI.class))).thenReturn(requestUrl); |
| 99 | + |
| 100 | + loadBalancerClientFilter.filter(exchange, chain); |
| 101 | + |
| 102 | + assertThat((LinkedHashSet<URI>)exchange.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR)).contains(url); |
| 103 | + |
| 104 | + verify(loadBalancerClient).choose("myservice"); |
| 105 | + |
| 106 | + ArgumentCaptor<URI> urlArgumentCaptor = ArgumentCaptor.forClass(URI.class); |
| 107 | + verify(loadBalancerClient).reconstructURI(eq(serviceInstance), urlArgumentCaptor.capture()); |
| 108 | + |
| 109 | + URI uri = urlArgumentCaptor.getValue(); |
| 110 | + assertThat(uri).isNotNull(); |
| 111 | + assertThat(uri.toString()).isEqualTo("loadbalancerclient.org"); |
| 112 | + |
| 113 | + verifyNoMoreInteractions(loadBalancerClient); |
| 114 | + |
| 115 | + assertThat((URI)exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).isEqualTo(requestUrl); |
| 116 | + |
| 117 | + verify(chain).filter(exchange); |
| 118 | + verifyNoMoreInteractions(chain); |
| 119 | + } |
| 120 | + |
| 121 | + |
45 | 122 | @Test |
46 | 123 | public void happyPath() { |
47 | 124 | MockServerHttpRequest request = MockServerHttpRequest |
@@ -126,9 +203,9 @@ private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri) { |
126 | 203 |
|
127 | 204 | LoadBalancerClient loadBalancerClient = mock(LoadBalancerClient.class); |
128 | 205 | when(loadBalancerClient.choose("service1")). |
129 | | - thenReturn(new DefaultServiceInstance("service1", "service1-host1", 8081, |
| 206 | + thenReturn(new DefaultServiceInstance("service1", "service1-host1", 8081, |
130 | 207 | false, Collections.emptyMap())); |
131 | | - |
| 208 | + |
132 | 209 | LoadBalancerClientFilter filter = new LoadBalancerClientFilter(loadBalancerClient); |
133 | 210 | filter.filter(exchange, filterChain); |
134 | 211 |
|
|
0 commit comments