PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sdks/httpcomponents-client-4.1.2-src/httpcomponents-client-4.1.2/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java

https://github.com/steveclark7/Test1
Java | 281 lines | 187 code | 46 blank | 48 comment | 5 complexity | 0ce3e5528e3d2a33dd5d3c9262e3417c MD5 | raw file
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. * ====================================================================
  20. *
  21. * This software consists of voluntary contributions made by many
  22. * individuals on behalf of the Apache Software Foundation. For more
  23. * information on the Apache Software Foundation, please see
  24. * <http://www.apache.org/>.
  25. *
  26. */
  27. package org.apache.http.impl.cookie;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import org.apache.http.Header;
  31. import org.apache.http.cookie.ClientCookie;
  32. import org.apache.http.cookie.Cookie;
  33. import org.apache.http.cookie.CookieOrigin;
  34. import org.apache.http.cookie.CookieSpec;
  35. import org.apache.http.cookie.MalformedCookieException;
  36. import org.apache.http.cookie.SetCookie2;
  37. import org.apache.http.message.BasicHeader;
  38. import org.junit.Assert;
  39. import org.junit.Test;
  40. /**
  41. * Test cases for 'best match' cookie policy
  42. */
  43. public class TestCookieBestMatchSpec {
  44. @Test
  45. public void testCookieBrowserCompatParsing() throws Exception {
  46. CookieSpec cookiespec = new BestMatchSpec();
  47. CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);
  48. // Make sure the lenient (browser compatible) cookie parsing
  49. // and validation is used for Netscape style cookies
  50. Header header = new BasicHeader("Set-Cookie", "name=value;path=/;domain=domain.com");
  51. List<Cookie> cookies = cookiespec.parse(header, origin);
  52. for (int i = 0; i < cookies.size(); i++) {
  53. cookiespec.validate(cookies.get(i), origin);
  54. }
  55. }
  56. @Test
  57. public void testNetscapeCookieParsing() throws Exception {
  58. CookieSpec cookiespec = new BestMatchSpec();
  59. CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
  60. Header header = new BasicHeader("Set-Cookie",
  61. "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
  62. List<Cookie> cookies = cookiespec.parse(header, origin);
  63. cookiespec.validate(cookies.get(0), origin);
  64. Assert.assertEquals(1, cookies.size());
  65. header = new BasicHeader("Set-Cookie",
  66. "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; version=1");
  67. cookies = cookiespec.parse(header, origin);
  68. cookiespec.validate(cookies.get(0), origin);
  69. Assert.assertEquals(1, cookies.size());
  70. }
  71. @Test
  72. public void testCookieStandardCompliantParsing() throws Exception {
  73. CookieSpec cookiespec = new BestMatchSpec();
  74. CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);
  75. // Make sure the strict (RFC2965) cookie parsing
  76. // and validation is used for version 1 Set-Cookie2 headers
  77. Header header = new BasicHeader("Set-Cookie2", "name=value;path=/;domain=b.domain.com; version=1");
  78. List<Cookie> cookies = cookiespec.parse(header, origin);
  79. for (int i = 0; i < cookies.size(); i++) {
  80. cookiespec.validate(cookies.get(i), origin);
  81. }
  82. // Make sure the strict (RFC2109) cookie parsing
  83. // and validation is used for version 1 Set-Cookie headers
  84. header = new BasicHeader("Set-Cookie", "name=value;path=/;domain=.b.domain.com; version=1");
  85. cookies = cookiespec.parse(header, origin);
  86. for (int i = 0; i < cookies.size(); i++) {
  87. cookiespec.validate(cookies.get(i), origin);
  88. }
  89. header = new BasicHeader("Set-Cookie2", "name=value;path=/;domain=domain.com; version=1");
  90. try {
  91. cookies = cookiespec.parse(header, origin);
  92. cookiespec.validate(cookies.get(0), origin);
  93. Assert.fail("MalformedCookieException exception should have been thrown");
  94. } catch (MalformedCookieException e) {
  95. // expected
  96. }
  97. }
  98. @Test
  99. public void testCookieStandardCompliantParsingLocalHost() throws Exception {
  100. CookieSpec cookiespec = new BestMatchSpec();
  101. CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
  102. Header header = new BasicHeader("Set-Cookie", "special=\"abcdigh\"; Version=1");
  103. List<Cookie> cookies = cookiespec.parse(header, origin);
  104. for (int i = 0; i < cookies.size(); i++) {
  105. Cookie cookie = cookies.get(i);
  106. cookiespec.validate(cookie, origin);
  107. Assert.assertEquals("localhost", cookie.getDomain());
  108. Assert.assertFalse(cookie instanceof SetCookie2);
  109. }
  110. }
  111. @Test
  112. public void testCookieStandardCompliantParsingLocalHost2() throws Exception {
  113. CookieSpec cookiespec = new BestMatchSpec();
  114. CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
  115. Header header = new BasicHeader("Set-Cookie2", "special=\"abcdigh\"; Version=1");
  116. List<Cookie> cookies = cookiespec.parse(header, origin);
  117. for (int i = 0; i < cookies.size(); i++) {
  118. Cookie cookie = cookies.get(i);
  119. cookiespec.validate(cookie, origin);
  120. Assert.assertEquals("localhost.local", cookie.getDomain());
  121. Assert.assertTrue(cookie instanceof SetCookie2);
  122. }
  123. }
  124. @Test
  125. public void testCookieBrowserCompatMatch() throws Exception {
  126. CookieSpec cookiespec = new BestMatchSpec();
  127. CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);
  128. // Make sure the lenient (browser compatible) cookie matching
  129. // is used for Netscape style cookies
  130. BasicClientCookie cookie = new BasicClientCookie("name", "value");
  131. cookie.setDomain(".domain.com");
  132. cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
  133. cookie.setPath("/");
  134. cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
  135. Assert.assertTrue(cookiespec.match(cookie, origin));
  136. }
  137. @Test
  138. public void testCookieStandardCompliantMatch() throws Exception {
  139. CookieSpec cookiespec = new BestMatchSpec();
  140. CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);
  141. // Make sure the strict (RFC2965) cookie matching
  142. // is used for version 1 cookies
  143. BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
  144. cookie.setVersion(1);
  145. cookie.setDomain(".domain.com");
  146. cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
  147. cookie.setPath("/");
  148. cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
  149. Assert.assertFalse(cookiespec.match(cookie, origin));
  150. cookie.setDomain(".b.domain.com");
  151. Assert.assertTrue(cookiespec.match(cookie, origin));
  152. }
  153. @Test
  154. public void testCookieBrowserCompatFormatting() throws Exception {
  155. CookieSpec cookiespec = new BestMatchSpec();
  156. // Make sure the lenient (browser compatible) cookie formatting
  157. // is used for Netscape style cookies
  158. BasicClientCookie cookie1 = new BasicClientCookie("name1", "value1");
  159. cookie1.setDomain(".domain.com");
  160. cookie1.setAttribute(ClientCookie.DOMAIN_ATTR, cookie1.getDomain());
  161. cookie1.setPath("/");
  162. cookie1.setAttribute(ClientCookie.PATH_ATTR, cookie1.getPath());
  163. BasicClientCookie cookie2 = new BasicClientCookie("name2", "value2");
  164. cookie2.setVersion(1);
  165. cookie2.setDomain(".domain.com");
  166. cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, cookie2.getDomain());
  167. cookie2.setPath("/");
  168. cookie2.setAttribute(ClientCookie.PATH_ATTR, cookie2.getPath());
  169. List<Cookie> cookies = new ArrayList<Cookie>();
  170. cookies.add(cookie1);
  171. cookies.add(cookie2);
  172. List<Header> headers = cookiespec.formatCookies(cookies);
  173. Assert.assertNotNull(headers);
  174. Assert.assertEquals(1, headers.size());
  175. Header header = headers.get(0);
  176. Assert.assertEquals("name1=value1; name2=value2", header.getValue());
  177. }
  178. @Test
  179. public void testCookieStandardCompliantFormatting() throws Exception {
  180. CookieSpec cookiespec = new BestMatchSpec(null, true);
  181. // Make sure the strict (RFC2965) cookie formatting
  182. // is used for Netscape style cookies
  183. BasicClientCookie cookie1 = new BasicClientCookie("name1", "value1");
  184. cookie1.setVersion(1);
  185. cookie1.setDomain(".domain.com");
  186. cookie1.setAttribute(ClientCookie.DOMAIN_ATTR, cookie1.getDomain());
  187. cookie1.setPath("/");
  188. cookie1.setAttribute(ClientCookie.PATH_ATTR, cookie1.getPath());
  189. BasicClientCookie cookie2 = new BasicClientCookie("name2", "value2");
  190. cookie2.setVersion(1);
  191. cookie2.setDomain(".domain.com");
  192. cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, cookie2.getDomain());
  193. cookie2.setPath("/");
  194. cookie2.setAttribute(ClientCookie.PATH_ATTR, cookie2.getPath());
  195. List<Cookie> cookies = new ArrayList<Cookie>();
  196. cookies.add(cookie1);
  197. cookies.add(cookie2);
  198. List<Header> headers = cookiespec.formatCookies(cookies);
  199. Assert.assertNotNull(headers);
  200. Assert.assertEquals(1, headers.size());
  201. Header header = headers.get(0);
  202. Assert.assertEquals("$Version=1; name1=\"value1\"; $Path=\"/\"; $Domain=\".domain.com\"; " +
  203. "name2=\"value2\"; $Path=\"/\"; $Domain=\".domain.com\"",
  204. header.getValue());
  205. }
  206. @Test
  207. public void testInvalidInput() throws Exception {
  208. CookieSpec cookiespec = new BestMatchSpec();
  209. try {
  210. cookiespec.parse(null, null);
  211. Assert.fail("IllegalArgumentException must have been thrown");
  212. } catch (IllegalArgumentException ex) {
  213. // expected
  214. }
  215. try {
  216. cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null);
  217. Assert.fail("IllegalArgumentException must have been thrown");
  218. } catch (IllegalArgumentException ex) {
  219. // expected
  220. }
  221. try {
  222. cookiespec.formatCookies(null);
  223. Assert.fail("IllegalArgumentException must have been thrown");
  224. } catch (IllegalArgumentException ex) {
  225. // expected
  226. }
  227. try {
  228. List<Cookie> cookies = new ArrayList<Cookie>();
  229. cookiespec.formatCookies(cookies);
  230. Assert.fail("IllegalArgumentException must have been thrown");
  231. } catch (IllegalArgumentException ex) {
  232. // expected
  233. }
  234. }
  235. }