001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session.util;
006:
007: import com.tc.logging.TCLogging;
008: import com.tc.object.bytecode.Manager;
009: import com.tc.properties.TCPropertiesImpl;
010: import com.terracotta.session.SessionId;
011:
012: import javax.servlet.http.Cookie;
013: import javax.servlet.http.MockHttpServletRequest;
014: import javax.servlet.http.MockHttpServletResponse;
015:
016: import junit.framework.TestCase;
017:
018: public class DefaultCookieWriterTest extends TestCase {
019:
020: private final String cookieName = "SomeCookieName";
021: private final String idValue = "SomeSessionId";
022:
023: private DefaultCookieWriter writer;
024: private MockHttpServletRequest req;
025: private MockHttpServletResponse res;
026: private SessionId id;
027:
028: public final void setUp() {
029: writer = makeCookieWriter(true, true);
030: req = new MockHttpServletRequest();
031: res = new MockHttpServletResponse();
032: id = new DefaultSessionId(idValue, idValue, idValue,
033: Manager.LOCK_TYPE_WRITE, false);
034: }
035:
036: public final void testConstructor() {
037: // test default c-tor
038: DefaultCookieWriter w = DefaultCookieWriter
039: .makeInstance(new ConfigProperties(null,
040: TCPropertiesImpl.getProperties(), TCLogging
041: .getTestingLogger(getClass())));
042: assertEquals(ConfigProperties.defaultCookieName, w.cookieName);
043:
044: }
045:
046: public final void testCreateCookie() {
047: final Cookie c = writer.createCookie(req, id);
048: checkCookie(cookieName, idValue, req.getContextPath(), c);
049: }
050:
051: public final void testWriteCookie() {
052: Cookie cookie = writer.writeCookie(req, res, id);
053: assertNotNull(cookie);
054: Cookie[] cookies = res.getCookies();
055: assertNotNull(cookies);
056: assertEquals(1, cookies.length);
057: checkCookie(cookieName, idValue, req.getContextPath(),
058: cookies[0]);
059: }
060:
061: public final void testWriteCookieDisabled() {
062: Cookie cookie;
063: Cookie[] cookies;
064:
065: writer = makeCookieWriter(false, true);
066: cookie = writer.writeCookie(req, res, id);
067: assertNull(cookie);
068: cookies = res.getCookies();
069: assertNotNull(cookies);
070: assertEquals(0, cookies.length);
071:
072: writer = makeCookieWriter(true, false);
073: cookie = writer.writeCookie(req, res, id);
074: assertNull(cookie);
075: cookies = res.getCookies();
076: assertNotNull(cookies);
077: assertEquals(0, cookies.length);
078:
079: writer = makeCookieWriter(false, false);
080: cookie = writer.writeCookie(req, res, id);
081: assertNull(cookie);
082: cookies = res.getCookies();
083: assertNotNull(cookies);
084: assertEquals(0, cookies.length);
085: }
086:
087: private DefaultCookieWriter makeCookieWriter(
088: boolean trackingEnabled, boolean cookiesEnabled) {
089: return new DefaultCookieWriter(trackingEnabled, cookiesEnabled,
090: true, cookieName, null,
091: ConfigProperties.defaultCookiePath, null, -1, false);
092: }
093:
094: public final void testGetCookiePath() {
095: // when path specified in c-tor is ConfigProperties.defaultCookiePath, request.getContextPath should be returned
096: assertEquals(req.getContextPath(), writer.getCookiePath(req));
097: // in case an override is specified it should be used instead
098: final String pathOverride = "/SomePath";
099: writer = new DefaultCookieWriter(true, true, true, cookieName,
100: null, pathOverride, null, -1, false);
101: assertEquals(pathOverride, writer.getCookiePath(req));
102: }
103:
104: public final void testUrlRewrite() {
105: final String requestUrl = "http://localhost:8080/some_page.jsp";
106: req.setRequestUrl(requestUrl);
107: req.setRequestedSessionId(id.getExternalId());
108: req.setSidFromCookie(false);
109: req.setSidValid(true);
110: req.setScheme("http");
111: req.setServerName("localhost");
112: req.setServerPort(8080);
113: final String actual = writer.encodeRedirectURL("/", req);
114: final String expected = "/;" + this .cookieName.toLowerCase()
115: + "=" + id.getExternalId();
116: assertEquals(expected, actual);
117: }
118:
119: private final void checkCookie(final String cName,
120: final String cVal, final String path, Cookie c) {
121: assertEquals(cName, c.getName());
122: assertEquals(cVal, c.getValue());
123: assertEquals(path, c.getPath());
124: }
125:
126: }
|