Skip to content

Commit 63ce1c6

Browse files
committed
Fix ApiServletTest.java
1 parent db63070 commit 63ce1c6

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

server/src/test/java/com/cloud/api/ApiServletTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.Test;
4040
import org.junit.runner.RunWith;
4141
import org.mockito.Mock;
42+
import org.mockito.MockedStatic;
4243
import org.mockito.Mockito;
4344
import org.mockito.junit.MockitoJUnitRunner;
4445

@@ -57,6 +58,7 @@
5758
import java.util.Map;
5859

5960
import static org.mockito.ArgumentMatchers.nullable;
61+
import static org.mockito.Mockito.mockStatic;
6062

6163
@RunWith(MockitoJUnitRunner.class)
6264
public class ApiServletTest {
@@ -101,13 +103,11 @@ public class ApiServletTest {
101103
StringWriter responseWriter;
102104

103105
ApiServlet servlet;
104-
ApiServlet spyServlet;
105106
@SuppressWarnings("unchecked")
106107
@Before
107108
public void setup() throws SecurityException, NoSuchFieldException,
108109
IllegalArgumentException, IllegalAccessException, IOException, UnknownHostException {
109110
servlet = new ApiServlet();
110-
spyServlet = Mockito.spy(servlet);
111111
responseWriter = new StringWriter();
112112
Mockito.when(response.getWriter()).thenReturn(
113113
new PrintWriter(responseWriter));
@@ -261,43 +261,42 @@ public void processRequestInContextLogin() throws UnknownHostException {
261261

262262
@Test
263263
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+
}
269268
}
270269

271270
@Test
272271
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+
}
278276
}
279277

280278
@Test
281279
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+
}
286284
}
287285

288286
@Test
289287
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+
}
295292
}
296293

297294
@Test
298295
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+
}
301300
}
302301

303302
@Test

0 commit comments

Comments
 (0)