cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ko...@apache.org
Subject [04/11] git commit: updated refs/heads/master to 31db58f
Date Sun, 23 Nov 2014 19:49:12 GMT
unit test for getClientAddress

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/fa52ffad
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/fa52ffad
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/fa52ffad

Branch: refs/heads/master
Commit: fa52ffad651142070b9e88fbbb36ed06b8a879dc
Parents: 55c449a
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Sat Nov 22 20:39:45 2014 +0100
Committer: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Committed: Sun Nov 23 19:48:57 2014 +0100

----------------------------------------------------------------------
 server/src/com/cloud/api/ApiServlet.java      |  4 +--
 server/test/com/cloud/api/ApiServletTest.java | 36 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa52ffad/server/src/com/cloud/api/ApiServlet.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiServlet.java b/server/src/com/cloud/api/ApiServlet.java
index 3d2e843..75f7ed2 100644
--- a/server/src/com/cloud/api/ApiServlet.java
+++ b/server/src/com/cloud/api/ApiServlet.java
@@ -309,7 +309,7 @@ public class ApiServlet extends HttpServlet {
     }
 
     //This method will try to get login IP of user even if servlet is behind reverseProxy
or loadBalancer
-    private String getClientAddress(HttpServletRequest request) {
+    static String getClientAddress(HttpServletRequest request) {
         String ip = null;
         ip = request.getHeader("X-Forwarded-For");
         ip = getCorrectIPAddress(ip);
@@ -339,7 +339,7 @@ public class ApiServlet extends HttpServlet {
         return ip;
     }
 
-    private String getCorrectIPAddress(String ip) {
+    private static String getCorrectIPAddress(String ip) {
         if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
             return null;
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/fa52ffad/server/test/com/cloud/api/ApiServletTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/ApiServletTest.java b/server/test/com/cloud/api/ApiServletTest.java
index 1a9c13d..2089477 100644
--- a/server/test/com/cloud/api/ApiServletTest.java
+++ b/server/test/com/cloud/api/ApiServletTest.java
@@ -19,10 +19,12 @@ package com.cloud.api;
 import org.apache.cloudstack.api.auth.APIAuthenticationManager;
 import org.apache.cloudstack.api.auth.APIAuthenticationType;
 import org.apache.cloudstack.api.auth.APIAuthenticator;
+
 import com.cloud.server.ManagementServer;
 import com.cloud.user.Account;
 import com.cloud.user.AccountService;
 import com.cloud.user.User;
+
 import org.apache.cloudstack.api.ApiConstants;
 import org.junit.After;
 import org.junit.Assert;
@@ -36,6 +38,7 @@ import org.mockito.runners.MockitoJUnitRunner;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
@@ -81,6 +84,7 @@ public class ApiServletTest {
 
     ApiServlet servlet;
 
+    @SuppressWarnings("unchecked")
     @Before
     public void setup() throws SecurityException, NoSuchFieldException,
             IllegalArgumentException, IllegalAccessException, IOException {
@@ -192,6 +196,7 @@ public class ApiServletTest {
                 Mockito.any(StringBuilder.class));
     }
 
+    @SuppressWarnings("unchecked")
     @Test
     public void processRequestInContextLogout() {
         Mockito.when(request.getMethod()).thenReturn("GET");
@@ -234,4 +239,35 @@ public class ApiServletTest {
         Mockito.verify(authenticator).authenticate(Mockito.anyString(), Mockito.anyMap(),
Mockito.isA(HttpSession.class),
                 Mockito.anyString(), Mockito.anyString(), Mockito.isA(StringBuilder.class),
Mockito.isA(HttpServletResponse.class));
     }
+
+    @Test
+    public void getClientAddressWithXForwardedFor() {
+        Mockito.when(request.getHeader(Mockito.eq("X-Forwarded-For"))).thenReturn("192.168.1.1");
+        Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
+    }
+
+    @Test
+    public void getClientAddressWithHttpXForwardedFor() {
+        Mockito.when(request.getHeader(Mockito.eq("HTTP_X_FORWARDED_FOR"))).thenReturn("192.168.1.1");
+        Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
+    }
+
+    @Test
+    public void getClientAddressWithXRemoteAddr() {
+        Mockito.when(request.getHeader(Mockito.eq("Remote_Addr"))).thenReturn("192.168.1.1");
+        Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
+    }
+
+    @Test
+    public void getClientAddressWithHttpClientIp() {
+        Mockito.when(request.getHeader(Mockito.eq("HTTP_CLIENT_IP"))).thenReturn("192.168.1.1");
+        Assert.assertEquals("192.168.1.1", ApiServlet.getClientAddress(request));
+    }
+
+    @Test
+    public void getClientAddressDefault() {
+        Mockito.when(request.getRemoteAddr()).thenReturn("127.0.0.1");
+        Assert.assertEquals("127.0.0.1", ApiServlet.getClientAddress(request));
+    }
+
 }


Mime
View raw message