Skip to content

Commit fa52ffa

Browse files
committed
unit test for getClientAddress
Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 55c449a commit fa52ffa

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

server/src/com/cloud/api/ApiServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
309309
}
310310

311311
//This method will try to get login IP of user even if servlet is behind reverseProxy or loadBalancer
312-
private String getClientAddress(HttpServletRequest request) {
312+
static String getClientAddress(HttpServletRequest request) {
313313
String ip = null;
314314
ip = request.getHeader("X-Forwarded-For");
315315
ip = getCorrectIPAddress(ip);
@@ -339,7 +339,7 @@ private String getClientAddress(HttpServletRequest request) {
339339
return ip;
340340
}
341341

342-
private String getCorrectIPAddress(String ip) {
342+
private static String getCorrectIPAddress(String ip) {
343343
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
344344
return null;
345345
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import org.apache.cloudstack.api.auth.APIAuthenticationManager;
2020
import org.apache.cloudstack.api.auth.APIAuthenticationType;
2121
import org.apache.cloudstack.api.auth.APIAuthenticator;
22+
2223
import com.cloud.server.ManagementServer;
2324
import com.cloud.user.Account;
2425
import com.cloud.user.AccountService;
2526
import com.cloud.user.User;
27+
2628
import org.apache.cloudstack.api.ApiConstants;
2729
import org.junit.After;
2830
import org.junit.Assert;
@@ -36,6 +38,7 @@
3638
import javax.servlet.http.HttpServletRequest;
3739
import javax.servlet.http.HttpServletResponse;
3840
import javax.servlet.http.HttpSession;
41+
3942
import java.io.IOException;
4043
import java.io.PrintWriter;
4144
import java.io.StringWriter;
@@ -81,6 +84,7 @@ public class ApiServletTest {
8184

8285
ApiServlet servlet;
8386

87+
@SuppressWarnings("unchecked")
8488
@Before
8589
public void setup() throws SecurityException, NoSuchFieldException,
8690
IllegalArgumentException, IllegalAccessException, IOException {
@@ -192,6 +196,7 @@ public void processRequestInContextAuthorizedGet() {
192196
Mockito.any(StringBuilder.class));
193197
}
194198

199+
@SuppressWarnings("unchecked")
195200
@Test
196201
public void processRequestInContextLogout() {
197202
Mockito.when(request.getMethod()).thenReturn("GET");
@@ -234,4 +239,35 @@ public void processRequestInContextLogin() {
234239
Mockito.verify(authenticator).authenticate(Mockito.anyString(), Mockito.anyMap(), Mockito.isA(HttpSession.class),
235240
Mockito.anyString(), Mockito.anyString(), Mockito.isA(StringBuilder.class), Mockito.isA(HttpServletResponse.class));
236241
}
242+
243+
@Test
244+
public void getClientAddressWithXForwardedFor() {
245+
Mockito.when(request.getHeader(Mockito.eq("X-Forwarded-For"))).thenReturn("192.168.1.1");
246+
Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
247+
}
248+
249+
@Test
250+
public void getClientAddressWithHttpXForwardedFor() {
251+
Mockito.when(request.getHeader(Mockito.eq("HTTP_X_FORWARDED_FOR"))).thenReturn("192.168.1.1");
252+
Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
253+
}
254+
255+
@Test
256+
public void getClientAddressWithXRemoteAddr() {
257+
Mockito.when(request.getHeader(Mockito.eq("Remote_Addr"))).thenReturn("192.168.1.1");
258+
Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
259+
}
260+
261+
@Test
262+
public void getClientAddressWithHttpClientIp() {
263+
Mockito.when(request.getHeader(Mockito.eq("HTTP_CLIENT_IP"))).thenReturn("192.168.1.1");
264+
Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
265+
}
266+
267+
@Test
268+
public void getClientAddressDefault() {
269+
Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
270+
Assert.assertEquals("127.0.0.1", ApiServlet.getClientAddress(request));
271+
}
272+
237273
}

0 commit comments

Comments
 (0)