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 static org.easymock.EasyMock.createMock;
19 import static org.easymock.EasyMock.expect;
20 import static org.easymock.EasyMock.replay;
21 import static org.easymock.EasyMock.verify;
22 
23 import javax.servlet.http.Cookie;
24 import javax.servlet.http.HttpServletRequest;
25 import junit.framework.AssertionFailedError;
26 import junit.framework.TestCase;
27 
28 public class ContinuingHttpServletRequestTest extends TestCase {
29 
30   private static final String TEST_VALUE_1 = "testValue1";
31   private static final String TEST_VALUE_2 = "testValue2";
32   private static final int DEFAULT_MAX_AGE = new Cookie("", "").getMaxAge();
33 
testReturnNullCookiesIfDelegateHasNoNull()34   public void testReturnNullCookiesIfDelegateHasNoNull() {
35     HttpServletRequest delegate = createMock(HttpServletRequest.class);
36     expect(delegate.getCookies()).andStubReturn(null);
37 
38     replay(delegate);
39 
40     assertNull(new ContinuingHttpServletRequest(delegate).getCookies());
41 
42     verify(delegate);
43   }
44 
testReturnDelegateCookies()45   public void testReturnDelegateCookies() {
46     Cookie[] cookies =
47         new Cookie[] {new Cookie("testName1", TEST_VALUE_1), new Cookie("testName2", "testValue2")};
48     HttpServletRequest delegate = createMock(HttpServletRequest.class);
49     expect(delegate.getCookies()).andStubReturn(cookies);
50 
51     replay(delegate);
52 
53     ContinuingHttpServletRequest continuingRequest = new ContinuingHttpServletRequest(delegate);
54 
55     assertCookieArraysEqual(cookies, continuingRequest.getCookies());
56 
57     // Now mutate the original cookies, this shouldnt be reflected in the continued request.
58     cookies[0].setValue("INVALID");
59     cookies[1].setValue("INVALID");
60     cookies[1].setMaxAge(123);
61 
62     try {
63       assertCookieArraysEqual(cookies, continuingRequest.getCookies());
64       throw new Error();
65     } catch (AssertionFailedError e) {
66       // Expected.
67     }
68 
69     // Verify that they remain equal to the original values.
70     assertEquals(TEST_VALUE_1, continuingRequest.getCookies()[0].getValue());
71     assertEquals(TEST_VALUE_2, continuingRequest.getCookies()[1].getValue());
72     assertEquals(DEFAULT_MAX_AGE, continuingRequest.getCookies()[1].getMaxAge());
73 
74     // Perform a snapshot of the snapshot.
75     ContinuingHttpServletRequest furtherContinuingRequest =
76         new ContinuingHttpServletRequest(continuingRequest);
77 
78     // The cookies should be fixed.
79     assertCookieArraysEqual(continuingRequest.getCookies(), furtherContinuingRequest.getCookies());
80 
81     verify(delegate);
82   }
83 
assertCookieArraysEqual(Cookie[] one, Cookie[] two)84   private static void assertCookieArraysEqual(Cookie[] one, Cookie[] two) {
85     assertEquals(one.length, two.length);
86     for (int i = 0; i < one.length; i++) {
87       Cookie cookie = one[i];
88       assertCookieEquality(cookie, two[i]);
89     }
90   }
91 
assertCookieEquality(Cookie one, Cookie two)92   private static void assertCookieEquality(Cookie one, Cookie two) {
93     assertEquals(one.getName(), two.getName());
94     assertEquals(one.getComment(), two.getComment());
95     assertEquals(one.getDomain(), two.getDomain());
96     assertEquals(one.getPath(), two.getPath());
97     assertEquals(one.getValue(), two.getValue());
98     assertEquals(one.getMaxAge(), two.getMaxAge());
99     assertEquals(one.getSecure(), two.getSecure());
100   }
101 }
102