1 5 package com.terracotta.session.util; 6 7 import com.tc.object.bytecode.Manager; 8 import com.tc.properties.TCPropertiesImpl; 9 import com.terracotta.session.SessionId; 10 11 import javax.servlet.http.Cookie ; 12 import javax.servlet.http.MockHttpServletRequest ; 13 import javax.servlet.http.MockHttpServletResponse ; 14 15 import junit.framework.TestCase; 16 17 public class DefaultCookieWriterTest extends TestCase { 18 19 private final String cookieName = "SomeCookieName"; 20 private final String idValue = "SomeSessionId"; 21 22 private DefaultCookieWriter writer; 23 private MockHttpServletRequest req; 24 private MockHttpServletResponse res; 25 private SessionId id; 26 27 public final void setUp() { 28 writer = new DefaultCookieWriter(true, true, true, cookieName, null, ConfigProperties.defaultCookiePath, null, -1, 29 false); 30 req = new MockHttpServletRequest (); 31 res = new MockHttpServletResponse (); 32 id = new DefaultSessionId(idValue, idValue, idValue, Manager.LOCK_TYPE_WRITE); 33 } 34 35 public final void testConstructor() { 36 DefaultCookieWriter w = DefaultCookieWriter.makeInstance(new ConfigProperties(null, TCPropertiesImpl 38 .getProperties())); 39 assertEquals(ConfigProperties.defaultCookieName, w.cookieName); 40 41 } 42 43 public final void testCreateCookie() { 44 final Cookie c = writer.createCookie(req, res, id); 45 checkCookie(cookieName, idValue, req.getContextPath(), c); 46 } 47 48 public final void testWriteCookie() { 49 writer.writeCookie(req, res, id); 50 Cookie [] cookies = res.getCookies(); 51 assertNotNull(cookies); 52 assertEquals(1, cookies.length); 53 checkCookie(cookieName, idValue, req.getContextPath(), cookies[0]); 54 } 55 56 public final void testGetCookiePath() { 57 assertEquals(req.getContextPath(), writer.getCookiePath(req)); 59 final String pathOverride = "/SomePath"; 61 writer = new DefaultCookieWriter(true, true, true, cookieName, null, pathOverride, null, -1, false); 62 assertEquals(pathOverride, writer.getCookiePath(req)); 63 } 64 65 public final void testUrlRewrite() { 66 final String requestUrl = "http://localhost:8080/some_page.jsp"; 67 req.setRequestUrl(requestUrl); 68 req.setRequestedSessionId(id.getExternalId()); 69 req.setSidFromCookie(false); 70 req.setSidValid(true); 71 req.setScheme("http"); 72 req.setServerName("localhost"); 73 req.setServerPort(8080); 74 final String actual = writer.encodeRedirectURL("/", req); 75 final String expected = "/;" + this.cookieName.toLowerCase() + "=" + id.getExternalId(); 76 assertEquals(expected, actual); 77 } 78 79 private final void checkCookie(final String cName, final String cVal, final String path, Cookie c) { 80 assertEquals(cName, c.getName()); 81 assertEquals(cVal, c.getValue()); 82 assertEquals(path, c.getPath()); 83 } 84 85 } 86 | Popular Tags |