You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by bh...@apache.org on 2015/08/12 10:42:44 UTC
[3/3] git commit: updated refs/heads/4.5-samlfixes to 1527ad6
utils: add HttpUtils test
Signed-off-by: Rohit Yadav <ro...@shapeblue.com>
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/0887f64e
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/0887f64e
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/0887f64e
Branch: refs/heads/4.5-samlfixes
Commit: 0887f64e451c6afe5f15e203a0c7d66f27c9b29d
Parents: 69f39d5
Author: Rohit Yadav <ro...@shapeblue.com>
Authored: Wed Aug 12 14:02:57 2015 +0530
Committer: Rohit Yadav <ro...@shapeblue.com>
Committed: Wed Aug 12 14:09:36 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/0887f64e/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/0887f64e/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));
+ }
+}