1 /** 2 * Copyright (C) 2013 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.google.inject.servlet; 17 18 import junit.framework.AssertionFailedError; 19 import static org.easymock.EasyMock.createMock; 20 import static org.easymock.EasyMock.expect; 21 import static org.easymock.EasyMock.replay; 22 import static org.easymock.EasyMock.verify; 23 24 import junit.framework.TestCase; 25 26 import javax.servlet.http.Cookie; 27 import javax.servlet.http.HttpServletRequest; 28 29 public class ContinuingHttpServletRequestTest extends TestCase { 30 31 private static final String TEST_VALUE_1 = "testValue1"; 32 private static final String TEST_VALUE_2 = "testValue2"; 33 private static final int DEFAULT_MAX_AGE = new Cookie("", "").getMaxAge(); 34 testReturnNullCookiesIfDelegateHasNoNull()35 public void testReturnNullCookiesIfDelegateHasNoNull() { 36 HttpServletRequest delegate = createMock(HttpServletRequest.class); 37 expect(delegate.getCookies()).andStubReturn(null); 38 39 replay(delegate); 40 41 assertNull(new ContinuingHttpServletRequest(delegate).getCookies()); 42 43 verify(delegate); 44 } 45 testReturnDelegateCookies()46 public void testReturnDelegateCookies() { 47 Cookie[] cookies = new Cookie[]{ 48 new Cookie("testName1", TEST_VALUE_1), 49 new Cookie("testName2", "testValue2") 50 }; 51 HttpServletRequest delegate = createMock(HttpServletRequest.class); 52 expect(delegate.getCookies()).andStubReturn(cookies); 53 54 replay(delegate); 55 56 ContinuingHttpServletRequest continuingRequest = new ContinuingHttpServletRequest( 57 delegate); 58 59 assertCookieArraysEqual(cookies, continuingRequest.getCookies()); 60 61 // Now mutate the original cookies, this shouldnt be reflected in the continued request. 62 cookies[0].setValue("INVALID"); 63 cookies[1].setValue("INVALID"); 64 cookies[1].setMaxAge(123); 65 66 try { 67 assertCookieArraysEqual(cookies, continuingRequest.getCookies()); 68 fail(); 69 } catch (AssertionFailedError e) { 70 // Expected. 71 } 72 73 // Verify that they remain equal to the original values. 74 assertEquals(TEST_VALUE_1, continuingRequest.getCookies()[0].getValue()); 75 assertEquals(TEST_VALUE_2, continuingRequest.getCookies()[1].getValue()); 76 assertEquals(DEFAULT_MAX_AGE, continuingRequest.getCookies()[1].getMaxAge()); 77 78 // Perform a snapshot of the snapshot. 79 ContinuingHttpServletRequest furtherContinuingRequest = new ContinuingHttpServletRequest( 80 continuingRequest); 81 82 // The cookies should be fixed. 83 assertCookieArraysEqual(continuingRequest.getCookies(), furtherContinuingRequest.getCookies()); 84 85 verify(delegate); 86 } 87 assertCookieArraysEqual(Cookie[] one, Cookie[] two)88 private static void assertCookieArraysEqual(Cookie[] one, Cookie[] two) { 89 assertEquals(one.length, two.length); 90 for (int i = 0; i < one.length; i++) { 91 Cookie cookie = one[i]; 92 assertCookieEquality(cookie, two[i]); 93 } 94 } 95 assertCookieEquality(Cookie one, Cookie two)96 private static void assertCookieEquality(Cookie one, Cookie two) { 97 assertEquals(one.getName(), two.getName()); 98 assertEquals(one.getComment(), two.getComment()); 99 assertEquals(one.getDomain(), two.getDomain()); 100 assertEquals(one.getPath(), two.getPath()); 101 assertEquals(one.getValue(), two.getValue()); 102 assertEquals(one.getMaxAge(), two.getMaxAge()); 103 assertEquals(one.getSecure(), two.getSecure()); 104 } 105 } 106