View Javadoc

1   // Copyright (C) 2005 - 2012 Philip Aston
2   // Copyright (C) 2005 - 2010 Hitoshi Amano
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package HTTPClient;
24  
25  import java.net.ProtocolException;
26  import java.text.DateFormat;
27  import java.util.Date;
28  import java.util.Locale;
29  import java.util.TimeZone;
30  
31  import junit.framework.TestCase;
32  import net.grinder.testutility.RandomStubFactory;
33  
34  
35  /**
36   * Unit tests for our modifications to {@link Cookie}.
37   *
38   * @author Philip Aston
39   */
40  public class TestCookie extends TestCase {
41  
42    private final RoRequestStubFactory m_roRequestStubFactory =
43      new RoRequestStubFactory();
44    private final RoRequest m_roRequest = m_roRequestStubFactory.getStub();
45  
46    public void testParse() throws Exception {
47      // No cookies, nothing to do.
48      Cookie.parse("", null);
49  
50      try {
51        Cookie.parse("foo", m_roRequest);
52        fail("Expected ProtocolException for bad set cookie header");
53      }
54      catch (final ProtocolException e) {
55      }
56  
57      final Cookie[] cookies = Cookie.parse("foo=bah", m_roRequest);
58      assertEquals(1, cookies.length);
59      assertEquals("host.local", cookies[0].getDomain());
60      assertEquals("foo", cookies[0].getName());
61      assertEquals("bah", cookies[0].getValue());
62      assertEquals("/path", cookies[0].getPath());
63      assertNull(cookies[0].expires());
64  
65      final Cookie[] cookies2 =
66        Cookie.parse("foo=bah;path=lah;expires=Sat, Mar 25 16:53:28 GMT 2006", m_roRequest);
67      assertEquals(1, cookies2.length);
68      assertEquals("host.local", cookies2[0].getDomain());
69      assertEquals("foo", cookies2[0].getName());
70      assertEquals("bah", cookies2[0].getValue());
71      assertEquals("lah", cookies2[0].getPath());
72  
73      final DateFormat df =
74        DateFormat.getDateTimeInstance(
75          DateFormat.SHORT, DateFormat.MEDIUM, Locale.UK);
76      df.setTimeZone(TimeZone.getTimeZone("GMT"));
77      final Date result = df.parse("25/03/06 16:53:28");
78      assertEquals(result, cookies2[0].expires());
79    }
80  
81    public void testWithMartinsBrokenDates() throws Exception {
82      final Cookie[] cookies =
83        Cookie.parse("foo=bah;expires=Friday, 01-01-2038, 00:00:00 GMT",
84                     m_roRequest);
85      assertEquals(1, cookies.length);
86      assertEquals("host.local", cookies[0].getDomain());
87      assertEquals("foo", cookies[0].getName());
88      assertEquals("bah", cookies[0].getValue());
89      assertEquals("/path", cookies[0].getPath());
90  
91      // Martin's test case date isn't valid. Our modification makes the
92      // parser ignore it (logging a warning to the HTTPClient logger), rather
93      // than fail.
94      assertNull(cookies[0].expires());
95    }
96  
97    public void testFixForBug982834() throws Exception {
98      Cookie.parse("foo=bah;expires=", m_roRequest);
99    }
100 
101   // For bug 3124963.
102   public void testCookie2Discard() throws Exception {
103     Cookie2.parse("dude=member1-13443427;Version=1;Path=/;Discard",
104                   m_roRequest);
105   }
106 
107   public void testDotNetHttpOnlyNonsense() throws Exception {
108     Cookie.parse(".ASPXANONYMOUS=AcbBC8KU9yE3MmQyMDA1Ni0wZDlmLTQ0MjktYWI2NS0zMTUwOGQwZmZhNTk1; expires=Wed, 16-Aug-2006 04:12:47 GMT; path=/;HttpOnly, language=en-US; path=/;HttpOnly",
109       m_roRequest);
110     Cookie.parse(".ASPXANONYMOUS=AcbBC8KU9yE3MmQyMDA1Ni0wZDlmLTQ0MjktYWI2NS0zMTUwOGQwZmZhNTk1; expires=Wed, 16-Aug-2006 04:12:47 GMT; path=/;httponly, language=en-US; path=/;httponly",
111       m_roRequest);
112     Cookie.parse(".ASPXANONYMOUS=AcbBC8KU9yE3MmQyMDA1Ni0wZDlmLTQ0MjktYWI2NS0zMTUwOGQwZmZhNTk1; httponly; expires=Wed, 16-Aug-2006 04:12:47 GMT; path=/;httponly, language=en-US; path=/",
113                  m_roRequest);
114   }
115 
116   public void testFixForBug1576609() throws Exception {
117     m_roRequestStubFactory.setHost("khan.idc.shaw.ca");
118 
119     final Cookie[] cookies =
120       Cookie.parse(
121         "ssogrp1-uwc=131088949714C3AFD48A39038260AD79;Domain=.shaw.ca;Path=/",
122         m_roRequest);
123 
124     assertEquals(1, cookies.length);
125 
126     final Cookie[] cookies2 =
127       Cookie.parse(
128         "ssogrp1-uwc=131088949714C3AFD48A39038260AD79;Domain=.ca;Path=/",
129         m_roRequest);
130 
131     assertEquals(0, cookies2.length);
132   }
133 
134   public void testFixForBug219() throws Exception {
135     m_roRequestStubFactory.setHost("foo.bah.com");
136 
137     final String[] cookiesWithGoodDomains = {
138       "k=v;Domain=.foo.bah.com",
139       "k=v;Domain=foo.bah.com",
140       "k=v;Domain=.bah.com",
141     };
142 
143     for (final String s : cookiesWithGoodDomains) {
144       final Cookie[] cookies = Cookie.parse(s, m_roRequest);
145 
146       assertEquals(s, 1, cookies.length);
147     }
148 
149     final String[] cookiesWithBadDomains = {
150       "k=v;Domain=..foo.bah.com",
151       "k=v;Domain=blah.foo.bah.com",
152       "k=v;Domain=ffoo.bah.com",
153       "k=v;Domain=.com",
154     };
155 
156     for (final String s : cookiesWithBadDomains) {
157       final Cookie[] cookies = Cookie.parse(s, m_roRequest);
158 
159       assertEquals(s, 0, cookies.length);
160     }
161   }
162 
163 
164   public static final class RoRequestStubFactory
165     extends RandomStubFactory<RoRequest> {
166 
167     private String m_host = "host";
168     private String m_requestURI = "/path/sub;blah=blah";
169 
170     public RoRequestStubFactory() {
171       super(RoRequest.class);
172     }
173 
174     public HTTPConnection override_getConnection(final Object proxy) {
175       return new HTTPConnection(m_host);
176     }
177 
178     public String override_getRequestURI(final Object proxy) {
179       return m_requestURI;
180     }
181 
182     public void setHost(final String host) {
183       m_host = host;
184     }
185 
186     public void setRequestURI(final String requestURI) {
187       m_requestURI = requestURI;
188     }
189   }
190 }