Skip to content

Commit 2ca1e10

Browse files
committed
Fix ApiServletTest.java
1 parent db63070 commit 2ca1e10

File tree

1 file changed

+51
-22
lines changed

1 file changed

+51
-22
lines changed

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

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.cloudstack.api.auth.APIAuthenticator;
3434
import org.apache.cloudstack.api.command.admin.config.ListCfgsByCmd;
3535
import org.apache.cloudstack.framework.config.ConfigKey;
36+
import org.apache.cloudstack.framework.config.impl.ConfigDepotImpl;
3637
import org.junit.After;
3738
import org.junit.Assert;
3839
import org.junit.Before;
@@ -97,17 +98,20 @@ public class ApiServletTest {
9798
@Mock
9899
AccountService accountMgr;
99100

100-
@Mock ConfigKey<Boolean> useForwardHeader;
101+
@Mock
102+
ConfigDepotImpl mockConfigDepot;
103+
101104
StringWriter responseWriter;
102105

103106
ApiServlet servlet;
104-
ApiServlet spyServlet;
107+
108+
private ConfigDepotImpl originalConfigDepot;
109+
105110
@SuppressWarnings("unchecked")
106111
@Before
107112
public void setup() throws SecurityException, NoSuchFieldException,
108-
IllegalArgumentException, IllegalAccessException, IOException, UnknownHostException {
113+
IllegalArgumentException, IllegalAccessException, IOException {
109114
servlet = new ApiServlet();
110-
spyServlet = Mockito.spy(servlet);
111115
responseWriter = new StringWriter();
112116
Mockito.when(response.getWriter()).thenReturn(
113117
new PrintWriter(responseWriter));
@@ -130,7 +134,8 @@ public void setup() throws SecurityException, NoSuchFieldException,
130134
Field apiServerField = ApiServlet.class.getDeclaredField("apiServer");
131135
apiServerField.setAccessible(true);
132136
apiServerField.set(servlet, apiServer);
133-
137+
138+
setupConfigDepotMock();
134139
}
135140

136141
/**
@@ -151,6 +156,33 @@ public void cleanupEnvironmentHacks() throws Exception {
151156
Field smsField = ApiDBUtils.class.getDeclaredField("s_ms");
152157
smsField.setAccessible(true);
153158
smsField.set(null, null);
159+
restoreConfigDepot();
160+
}
161+
162+
private void setupConfigDepotMock() throws NoSuchFieldException, IllegalAccessException {
163+
Field depotField = ConfigKey.class.getDeclaredField("s_depot");
164+
depotField.setAccessible(true);
165+
originalConfigDepot = (ConfigDepotImpl) depotField.get(null);
166+
depotField.set(null, mockConfigDepot);
167+
Mockito.when(mockConfigDepot.getConfigStringValue(
168+
Mockito.anyString(),
169+
Mockito.any(ConfigKey.Scope.class),
170+
Mockito.any()
171+
)).thenReturn(null);
172+
}
173+
174+
private void restoreConfigDepot() throws Exception {
175+
Field depotField = ConfigKey.class.getDeclaredField("s_depot");
176+
depotField.setAccessible(true);
177+
depotField.set(null, originalConfigDepot);
178+
}
179+
180+
private void setConfigValue(String configName, String value) {
181+
Mockito.when(mockConfigDepot.getConfigStringValue(
182+
Mockito.eq(configName),
183+
Mockito.eq(ConfigKey.Scope.Global),
184+
Mockito.isNull()
185+
)).thenReturn(value);
154186
}
155187

156188
@Test
@@ -261,43 +293,40 @@ public void processRequestInContextLogin() throws UnknownHostException {
261293

262294
@Test
263295
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);
296+
setConfigValue("proxy.header.verify", "true");
297+
setConfigValue("proxy.cidr", "127.0.0.0/8");
298+
Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
267299
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));
300+
Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request));
269301
}
270302

271303
@Test
272304
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);
305+
setConfigValue("proxy.header.verify", "true");
306+
setConfigValue("proxy.cidr", "127.0.0.0/8");
276307
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));
308+
Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request));
278309
}
279310

280311
@Test
281312
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));
313+
setConfigValue("proxy.header.verify", "true");
314+
setConfigValue("proxy.cidr", "127.0.0.0/8");
315+
Assert.assertEquals(InetAddress.getByName("127.0.0.1"), ApiServlet.getClientAddress(request));
286316
}
287317

288318
@Test
289319
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);
320+
setConfigValue("proxy.header.verify", "true");
321+
setConfigValue("proxy.cidr", "127.0.0.0/8");
293322
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));
323+
Assert.assertEquals(InetAddress.getByName("192.168.1.1"), ApiServlet.getClientAddress(request));
295324
}
296325

297326
@Test
298327
public void getClientAddressDefault() throws UnknownHostException {
299328
Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
300-
Assert.assertEquals(InetAddress.getByName("127.0.0.1"), spyServlet.getClientAddress(request));
329+
Assert.assertEquals(InetAddress.getByName("127.0.0.1"), ApiServlet.getClientAddress(request));
301330
}
302331

303332
@Test

0 commit comments

Comments
 (0)