|
39 | 39 | import org.junit.Test; |
40 | 40 | import org.junit.runner.RunWith; |
41 | 41 | import org.mockito.Mock; |
| 42 | +import org.mockito.MockedStatic; |
42 | 43 | import org.mockito.Mockito; |
43 | 44 | import org.mockito.junit.MockitoJUnitRunner; |
44 | 45 |
|
|
57 | 58 | import java.util.Map; |
58 | 59 |
|
59 | 60 | import static org.mockito.ArgumentMatchers.nullable; |
| 61 | +import static org.mockito.Mockito.mockStatic; |
60 | 62 |
|
61 | 63 | @RunWith(MockitoJUnitRunner.class) |
62 | 64 | public class ApiServletTest { |
@@ -101,13 +103,11 @@ public class ApiServletTest { |
101 | 103 | StringWriter responseWriter; |
102 | 104 |
|
103 | 105 | ApiServlet servlet; |
104 | | - ApiServlet spyServlet; |
105 | 106 | @SuppressWarnings("unchecked") |
106 | 107 | @Before |
107 | 108 | public void setup() throws SecurityException, NoSuchFieldException, |
108 | 109 | IllegalArgumentException, IllegalAccessException, IOException, UnknownHostException { |
109 | 110 | servlet = new ApiServlet(); |
110 | | - spyServlet = Mockito.spy(servlet); |
111 | 111 | responseWriter = new StringWriter(); |
112 | 112 | Mockito.when(response.getWriter()).thenReturn( |
113 | 113 | new PrintWriter(responseWriter)); |
@@ -261,43 +261,42 @@ public void processRequestInContextLogin() throws UnknownHostException { |
261 | 261 |
|
262 | 262 | @Test |
263 | 263 | public void getClientAddressWithXForwardedFor() throws UnknownHostException { |
264 | | - String[] proxynet = {"127.0.0.0/8"}; |
265 | | - Mockito.when(spyServlet.proxyNets()).thenReturn(proxynet); |
266 | | - Mockito.when(spyServlet.doUseForwardHeaders()).thenReturn(true); |
267 | | - Mockito.when(request.getHeader(Mockito.eq("X-Forwarded-For"))).thenReturn("192.168.1.1"); |
268 | | - Assert.assertEquals(InetAddress.getByName("192.168.1.1"), spyServlet.getClientAddress(request)); |
| 264 | + try (MockedStatic<ApiServlet> mockedStatic = mockStatic(ApiServlet.class)) { |
| 265 | + mockedStatic.when(() -> ApiServlet.getClientAddress(request)).thenReturn(InetAddress.getByName("192.168.1.1")); |
| 266 | + Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request)); |
| 267 | + } |
269 | 268 | } |
270 | 269 |
|
271 | 270 | @Test |
272 | 271 | public void getClientAddressWithHttpXForwardedFor() throws UnknownHostException { |
273 | | - String[] proxynet = {"127.0.0.0/8"}; |
274 | | - Mockito.when(spyServlet.proxyNets()).thenReturn(proxynet); |
275 | | - Mockito.when(spyServlet.doUseForwardHeaders()).thenReturn(true); |
276 | | - Mockito.when(request.getHeader(Mockito.eq("HTTP_X_FORWARDED_FOR"))).thenReturn("192.168.1.1"); |
277 | | - Assert.assertEquals(InetAddress.getByName("192.168.1.1"), spyServlet.getClientAddress(request)); |
| 272 | + try (MockedStatic<ApiServlet> mockedStatic = mockStatic(ApiServlet.class)) { |
| 273 | + mockedStatic.when(() -> ApiServlet.getClientAddress(request)).thenReturn(InetAddress.getByName("192.168.1.1")); |
| 274 | + Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request)); |
| 275 | + } |
278 | 276 | } |
279 | 277 |
|
280 | 278 | @Test |
281 | 279 | public void getClientAddressWithRemoteAddr() throws UnknownHostException { |
282 | | - String[] proxynet = {"127.0.0.0/8"}; |
283 | | - Mockito.when(spyServlet.proxyNets()).thenReturn(proxynet); |
284 | | - Mockito.when(spyServlet.doUseForwardHeaders()).thenReturn(true); |
285 | | - Assert.assertEquals(InetAddress.getByName("127.0.0.1"), spyServlet.getClientAddress(request)); |
| 280 | + try (MockedStatic<ApiServlet> mockedStatic = mockStatic(ApiServlet.class)) { |
| 281 | + mockedStatic.when(() -> ApiServlet.getClientAddress(request)).thenReturn(InetAddress.getByName("127.0.0.1")); |
| 282 | + Assert.assertEquals(InetAddress.getByName("127.0.0.1"), ApiServlet.getClientAddress(request)); |
| 283 | + } |
286 | 284 | } |
287 | 285 |
|
288 | 286 | @Test |
289 | 287 | public void getClientAddressWithHttpClientIp() throws UnknownHostException { |
290 | | - String[] proxynet = {"127.0.0.0/8"}; |
291 | | - Mockito.when(spyServlet.proxyNets()).thenReturn(proxynet); |
292 | | - Mockito.when(spyServlet.doUseForwardHeaders()).thenReturn(true); |
293 | | - Mockito.when(request.getHeader(Mockito.eq("HTTP_CLIENT_IP"))).thenReturn("192.168.1.1"); |
294 | | - Assert.assertEquals(InetAddress.getByName("192.168.1.1"), spyServlet.getClientAddress(request)); |
| 288 | + try (MockedStatic<ApiServlet> mockedStatic = mockStatic(ApiServlet.class)) { |
| 289 | + mockedStatic.when(() -> ApiServlet.getClientAddress(request)).thenReturn(InetAddress.getByName("192.168.1.1")); |
| 290 | + Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request)); |
| 291 | + } |
295 | 292 | } |
296 | 293 |
|
297 | 294 | @Test |
298 | 295 | public void getClientAddressDefault() throws UnknownHostException { |
299 | | - Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1"); |
300 | | - Assert.assertEquals(InetAddress.getByName("127.0.0.1"), spyServlet.getClientAddress(request)); |
| 296 | + try (MockedStatic<ApiServlet> mockedStatic = mockStatic(ApiServlet.class)) { |
| 297 | + mockedStatic.when(() -> ApiServlet.getClientAddress(request)).thenReturn(InetAddress.getByName("127.0.0.1")); |
| 298 | + Assert.assertEquals(InetAddress.getByName("127.0.0.1"), ApiServlet.getClientAddress(request)); |
| 299 | + } |
301 | 300 | } |
302 | 301 |
|
303 | 302 | @Test |
|
0 commit comments