cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhais...@apache.org
Subject [4/8] git commit: updated refs/heads/master to 869a83f
Date Fri, 14 Aug 2015 06:54:45 GMT
utils: add HttpUtils test

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
(cherry picked from commit 0fcd9cad1e4a687c275095d6e27bdfb3eff4add8)
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>


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

Branch: refs/heads/master
Commit: 1d190fd0bb637bde14454fa5d4b4def1b4d0fe6a
Parents: da72ba6
Author: Rohit Yadav <rohit.yadav@shapeblue.com>
Authored: Wed Aug 12 14:02:57 2015 +0530
Committer: Rohit Yadav <rohit.yadav@shapeblue.com>
Committed: Fri Aug 14 11:59:31 2015 +0530

----------------------------------------------------------------------
 utils/src/com/cloud/utils/HttpUtils.java      | 32 +++++---
 utils/test/com/cloud/utils/HttpUtilsTest.java | 94 ++++++++++++++++++++++
 2 files changed, 113 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1d190fd0/utils/src/com/cloud/utils/HttpUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/HttpUtils.java b/utils/src/com/cloud/utils/HttpUtils.java
index 379b117..d2e844a 100644
--- a/utils/src/com/cloud/utils/HttpUtils.java
+++ b/utils/src/com/cloud/utils/HttpUtils.java
@@ -52,18 +52,6 @@ public class HttpUtils {
         }
     }
 
-    public static String findCookie(final Cookie[] cookies, final String key) {
-        if (cookies == null || key == null || key.isEmpty()) {
-            return null;
-        }
-        for (Cookie cookie: cookies) {
-            if (cookie != null && cookie.getName().equals(key)) {
-                return cookie.getValue();
-            }
-        }
-        return null;
-    }
-
     public static void writeHttpResponse(final HttpServletResponse resp, final String response,
                                          final Integer responseCode, final String responseType,
final String jsonContentType) {
         try {
@@ -92,10 +80,28 @@ public class HttpUtils {
         }
     }
 
+    public static String findCookie(final Cookie[] cookies, final String key) {
+        if (cookies == null || key == null || key.isEmpty()) {
+            return null;
+        }
+        for (Cookie cookie: cookies) {
+            if (cookie != null && cookie.getName().equals(key)) {
+                return cookie.getValue();
+            }
+        }
+        return null;
+    }
+
     public static boolean validateSessionKey(final HttpSession session, final Map<String,
Object[]> params, final Cookie[] cookies, final String sessionKeyString) {
+        if (session == null || sessionKeyString == null) {
+            return false;
+        }
         final String sessionKey = (String) session.getAttribute(sessionKeyString);
         final String sessionKeyFromCookie = HttpUtils.findCookie(cookies, sessionKeyString);
-        final String[] sessionKeyFromParams = (String[]) params.get(sessionKeyString);
+        String[] sessionKeyFromParams = null;
+        if (params != null) {
+            sessionKeyFromParams = (String[]) params.get(sessionKeyString);
+        }
         if ((sessionKey == null)
                 || (sessionKeyFromParams == null && sessionKeyFromCookie == null)
                 || (sessionKeyFromParams != null && !sessionKey.equals(sessionKeyFromParams[0]))

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1d190fd0/utils/test/com/cloud/utils/HttpUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/HttpUtilsTest.java b/utils/test/com/cloud/utils/HttpUtilsTest.java
new file mode 100644
index 0000000..e10a5a3
--- /dev/null
+++ b/utils/test/com/cloud/utils/HttpUtilsTest.java
@@ -0,0 +1,94 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.utils;
+
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpSession;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class HttpUtilsTest {
+
+    @Test
+    public void findCookieTest() {
+        Cookie[] cookies = null;
+        String cookieName = null;
+
+        // null test
+        assertNull(HttpUtils.findCookie(cookies, cookieName));
+        cookieName = "";
+        assertNull(HttpUtils.findCookie(cookies, cookieName));
+
+        // value test
+        cookieName = "daakuBandar";
+        cookies = new Cookie[]{new Cookie(cookieName, "someValue")};
+        assertNull(HttpUtils.findCookie(cookies, "aalasiLangur"));
+        assertNotNull(HttpUtils.findCookie(cookies, cookieName));
+    }
+
+    @Test
+    public void validateSessionKeyTest() {
+        HttpSession session = null;
+        Map<String, Object[]> params = null;
+        String sessionKeyString = null;
+        Cookie[] cookies = null;
+        final String sessionKeyValue = "randomUniqueSessionID";
+
+        // session and sessionKeyString null test
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+        sessionKeyString =  "sessionkey";
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+
+        // param and cookie null test
+        session = new MockHttpSession();
+        session.setAttribute(sessionKeyString, sessionKeyValue);
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+
+        // param null, cookies not null test
+        params = null;
+        cookies = new Cookie[]{new Cookie(sessionKeyString, sessionKeyValue)};
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, "randomString"));
+        assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+
+        // param not null, cookies null test
+        params = new HashMap<String, Object[]>();
+        params.put(sessionKeyString, new String[]{"randomString"});
+        cookies = null;
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+        params.put(sessionKeyString, new String[]{sessionKeyValue});
+        assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+
+        // both param and cookies not null test
+        params = new HashMap<String, Object[]>();
+        cookies = new Cookie[]{new Cookie(sessionKeyString, sessionKeyValue)};
+        params.put(sessionKeyString, new String[]{"incorrectValue"});
+        assertFalse(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+        params.put(sessionKeyString, new String[]{sessionKeyValue});
+        assertTrue(HttpUtils.validateSessionKey(session, params, cookies, sessionKeyString));
+    }
+}


Mime
View raw message