001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.services.impl;
016    
017    import org.apache.tapestry.BaseComponentTestCase;
018    import static org.easymock.EasyMock.expect;
019    import org.testng.annotations.Test;
020    
021    import javax.servlet.http.Cookie;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    /**
028     * Tests for {@link org.apache.tapestry.services.impl.CookieSourceImpl}. 
029     */
030    @Test
031    public class CookieSourceTest extends BaseComponentTestCase
032    {
033        // In seconds
034    
035        private static final int ONE_WEEK = 7 * 24 * 60 * 60;
036    
037        private static class ComparableCookie extends Cookie
038        {
039            public ComparableCookie(String name, String value, int maxAge)
040            {
041                super(name, value);
042                setMaxAge(maxAge);
043            }
044    
045            public boolean equals(Object obj)
046            {
047                Cookie c = (Cookie) obj;
048    
049                return equals(getName(), c.getName()) && equals(getValue(), c.getValue())
050                        && equals(getPath(), c.getPath()) && getMaxAge() == c.getMaxAge();
051            }
052    
053            private boolean equals(Object value, Object other)
054            {
055                return value == other || (value != null && value.equals(other));
056            }
057        }
058    
059        private HttpServletRequest newRequest(String[] nameValues)
060        {
061            Cookie[] cookies = null;
062    
063            if (nameValues != null)
064            {
065    
066                List l = new ArrayList();
067    
068                for (int i = 0; i < nameValues.length; i += 2)
069                {
070                    String name = nameValues[i];
071                    String value = nameValues[i + 1];
072    
073                    Cookie c = new Cookie(name, value);
074    
075                    l.add(c);
076                }
077    
078                cookies = (Cookie[]) l.toArray(new Cookie[l.size()]);
079            }
080    
081            HttpServletRequest request = newHttpRequest();
082    
083            expect(request.getCookies()).andReturn(cookies);
084    
085            return request;
086        }
087    
088        protected HttpServletRequest newHttpRequest()
089        {
090            return newMock(HttpServletRequest.class);
091        }
092    
093        private void attempt(String name, String expected, String[] nameValues)
094        {
095            HttpServletRequest request = newRequest(nameValues);
096    
097            CookieSourceImpl cs = new CookieSourceImpl();
098    
099            cs.setRequest(request);
100    
101            replay();
102    
103            String actual = cs.readCookieValue(name);
104    
105            assertEquals(actual, expected);
106    
107            verify();
108        }
109    
110        public void test_No_Cookies()
111        {
112            attempt("foo", null, null);
113        }
114    
115        public void test_Match()
116        {
117            attempt("fred", "flintstone", 
118                    new String[] { "barney", "rubble", "fred", "flintstone" });
119        }
120    
121        public void test_No_Match()
122        {
123            attempt("foo", null, new String[] { "bar", "baz" });
124        }
125    
126        public void test_Write_Cookie_Domain()
127        {
128            HttpServletRequest request = newHttpRequest();
129            HttpServletResponse response = newResponse();
130    
131            // Training
132    
133            trainGetContextPath(request, "/context");
134            
135            Cookie cookie = new ComparableCookie("foo", "bar", ONE_WEEK);
136            cookie.setPath("/context/");
137            cookie.setDomain("fobar.com");
138            
139            response.addCookie(cookie);
140            
141            replay();
142            
143            CookieSourceImpl cs = new CookieSourceImpl();
144            cs.setRequest(request);
145            cs.setResponse(response);
146            cs.setDefaultMaxAge(ONE_WEEK);
147            
148            cs.writeDomainCookieValue("foo", "bar", "fobar.com", ONE_WEEK);
149    
150            verify();
151        }
152    
153        public void test_Write_Cookie_With_Max_Age()
154        {
155            HttpServletRequest request = newHttpRequest();
156            HttpServletResponse response = newResponse();
157    
158            // Training
159    
160            trainGetContextPath(request, "/ctx");
161    
162            Cookie cookie = new ComparableCookie("foo", "bar", -1);
163            cookie.setPath("/ctx/");
164    
165            response.addCookie(cookie);
166    
167            replay();
168    
169            CookieSourceImpl cs = new CookieSourceImpl();
170            cs.setRequest(request);
171            cs.setResponse(response);
172            cs.setDefaultMaxAge(ONE_WEEK);
173    
174            cs.writeCookieValue("foo", "bar", -1);
175    
176            verify();
177        }
178    
179        public void test_Write_Cookie()
180        {
181            HttpServletRequest request = newHttpRequest();
182            HttpServletResponse response = newResponse();
183    
184            // Training
185            
186            trainGetContextPath(request, "/context");
187            
188            Cookie cookie = new ComparableCookie("foo", "bar", ONE_WEEK);
189            cookie.setPath("/context/");
190    
191            response.addCookie(cookie);
192    
193            replay();
194    
195            CookieSourceImpl cs = new CookieSourceImpl();
196            cs.setRequest(request);
197            cs.setResponse(response);
198            cs.setDefaultMaxAge(ONE_WEEK);
199    
200            cs.writeCookieValue("foo", "bar");
201    
202            verify();
203        }
204        
205        private void trainGetContextPath(HttpServletRequest request, String contextPath)
206        {
207            expect(request.getContextPath()).andReturn(contextPath);
208        }
209    
210        private HttpServletResponse newResponse()
211        {
212            return newMock(HttpServletResponse.class);
213        }
214    
215        public void test_Remove_Cookie()
216        {
217            HttpServletRequest request = newHttpRequest();
218            HttpServletResponse response = newResponse();
219    
220            // Training
221    
222            trainGetContextPath(request, "/context");
223    
224            Cookie cookie = new ComparableCookie("foo", null, 0);
225            cookie.setPath("/context/");
226    
227            response.addCookie(cookie);
228    
229            replay();
230    
231            CookieSourceImpl cs = new CookieSourceImpl();
232            cs.setRequest(request);
233            cs.setResponse(response);
234    
235            cs.removeCookieValue("foo");
236    
237            verify();
238        }
239    }