PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/okhttp-tests/src/test/java/okhttp3/CookiesTest.java

https://gitlab.com/JoshLucid/okhttp
Java | 288 lines | 233 code | 39 blank | 16 comment | 2 complexity | bbfecab00a252338c04f1873f6c401c2 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  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 okhttp3;
  17. import java.io.IOException;
  18. import java.net.CookieHandler;
  19. import java.net.CookieManager;
  20. import java.net.HttpCookie;
  21. import java.net.HttpURLConnection;
  22. import java.net.InetAddress;
  23. import java.net.URI;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import okhttp3.mockwebserver.MockResponse;
  29. import okhttp3.mockwebserver.MockWebServer;
  30. import okhttp3.mockwebserver.RecordedRequest;
  31. import org.junit.Test;
  32. import static java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER;
  33. import static okhttp3.TestUtil.defaultClient;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertNull;
  36. import static org.junit.Assert.assertTrue;
  37. import static org.junit.Assert.fail;
  38. /** Derived from Android's CookiesTest. */
  39. public class CookiesTest {
  40. private OkHttpClient client = defaultClient();
  41. @Test
  42. public void testNetscapeResponse() throws Exception {
  43. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  44. client = client.newBuilder()
  45. .cookieJar(new JavaNetCookieJar(cookieManager))
  46. .build();
  47. MockWebServer server = new MockWebServer();
  48. server.start();
  49. HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
  50. server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
  51. + "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
  52. + "path=/path; "
  53. + "domain=" + urlWithIpAddress.host() + "; "
  54. + "secure"));
  55. get(urlWithIpAddress);
  56. List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  57. assertEquals(1, cookies.size());
  58. HttpCookie cookie = cookies.get(0);
  59. assertEquals("a", cookie.getName());
  60. assertEquals("android", cookie.getValue());
  61. assertEquals(null, cookie.getComment());
  62. assertEquals(null, cookie.getCommentURL());
  63. assertEquals(false, cookie.getDiscard());
  64. assertTrue(cookie.getMaxAge() > 100000000000L);
  65. assertEquals("/path", cookie.getPath());
  66. assertEquals(true, cookie.getSecure());
  67. assertEquals(0, cookie.getVersion());
  68. }
  69. @Test public void testRfc2109Response() throws Exception {
  70. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  71. client = client.newBuilder()
  72. .cookieJar(new JavaNetCookieJar(cookieManager))
  73. .build();
  74. MockWebServer server = new MockWebServer();
  75. server.start();
  76. HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
  77. server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
  78. + "Comment=this cookie is delicious; "
  79. + "Domain=" + urlWithIpAddress.host() + "; "
  80. + "Max-Age=60; "
  81. + "Path=/path; "
  82. + "Secure; "
  83. + "Version=1"));
  84. get(urlWithIpAddress);
  85. List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  86. assertEquals(1, cookies.size());
  87. HttpCookie cookie = cookies.get(0);
  88. assertEquals("a", cookie.getName());
  89. assertEquals("android", cookie.getValue());
  90. assertEquals(null, cookie.getCommentURL());
  91. assertEquals(false, cookie.getDiscard());
  92. assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
  93. assertEquals("/path", cookie.getPath());
  94. assertEquals(true, cookie.getSecure());
  95. }
  96. @Test public void testQuotedAttributeValues() throws Exception {
  97. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  98. client = client.newBuilder()
  99. .cookieJar(new JavaNetCookieJar(cookieManager))
  100. .build();
  101. MockWebServer server = new MockWebServer();
  102. server.start();
  103. HttpUrl urlWithIpAddress = urlWithIpAddress(server, "/path/foo");
  104. server.enqueue(new MockResponse().addHeader("Set-Cookie: a=\"android\"; "
  105. + "Comment=\"this cookie is delicious\"; "
  106. + "CommentURL=\"http://google.com/\"; "
  107. + "Discard; "
  108. + "Domain=" + urlWithIpAddress.host() + "; "
  109. + "Max-Age=60; "
  110. + "Path=\"/path\"; "
  111. + "Port=\"80,443," + server.getPort() + "\"; "
  112. + "Secure; "
  113. + "Version=\"1\""));
  114. get(urlWithIpAddress);
  115. List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  116. assertEquals(1, cookies.size());
  117. HttpCookie cookie = cookies.get(0);
  118. assertEquals("a", cookie.getName());
  119. assertEquals("android", cookie.getValue());
  120. assertEquals(60.0, cookie.getMaxAge(), 1.0); // Converting to a fixed date can cause rounding!
  121. assertEquals("/path", cookie.getPath());
  122. assertEquals(true, cookie.getSecure());
  123. }
  124. @Test public void testSendingCookiesFromStore() throws Exception {
  125. MockWebServer server = new MockWebServer();
  126. server.enqueue(new MockResponse());
  127. server.start();
  128. HttpUrl serverUrl = urlWithIpAddress(server, "/");
  129. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  130. HttpCookie cookieA = new HttpCookie("a", "android");
  131. cookieA.setDomain(serverUrl.host());
  132. cookieA.setPath("/");
  133. cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
  134. HttpCookie cookieB = new HttpCookie("b", "banana");
  135. cookieB.setDomain(serverUrl.host());
  136. cookieB.setPath("/");
  137. cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
  138. client = client.newBuilder()
  139. .cookieJar(new JavaNetCookieJar(cookieManager))
  140. .build();
  141. get(serverUrl);
  142. RecordedRequest request = server.takeRequest();
  143. assertEquals("a=android; b=banana", request.getHeader("Cookie"));
  144. }
  145. @Test public void cookieHandlerLikeAndroid() throws Exception {
  146. final MockWebServer server = new MockWebServer();
  147. server.enqueue(new MockResponse());
  148. server.start();
  149. final HttpUrl serverUrl = urlWithIpAddress(server, "/");
  150. CookieHandler androidCookieHandler = new CookieHandler() {
  151. @Override public Map<String, List<String>> get(URI uri, Map<String, List<String>> map)
  152. throws IOException {
  153. return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; "
  154. + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; "
  155. + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
  156. }
  157. @Override public void put(URI uri, Map<String, List<String>> map) throws IOException {
  158. }
  159. };
  160. client = client.newBuilder()
  161. .cookieJar(new JavaNetCookieJar(androidCookieHandler))
  162. .build();
  163. get(serverUrl);
  164. RecordedRequest request = server.takeRequest();
  165. assertEquals("a=android; b=banana", request.getHeader("Cookie"));
  166. }
  167. @Test public void receiveAndSendMultipleCookies() throws Exception {
  168. MockWebServer server = new MockWebServer();
  169. server.enqueue(new MockResponse()
  170. .addHeader("Set-Cookie", "a=android")
  171. .addHeader("Set-Cookie", "b=banana"));
  172. server.enqueue(new MockResponse());
  173. server.start();
  174. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  175. client = client.newBuilder()
  176. .cookieJar(new JavaNetCookieJar(cookieManager))
  177. .build();
  178. get(urlWithIpAddress(server, "/"));
  179. RecordedRequest request1 = server.takeRequest();
  180. assertNull(request1.getHeader("Cookie"));
  181. get(urlWithIpAddress(server, "/"));
  182. RecordedRequest request2 = server.takeRequest();
  183. assertEquals("a=android; b=banana", request2.getHeader("Cookie"));
  184. }
  185. @Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
  186. MockWebServer redirectTarget = new MockWebServer();
  187. redirectTarget.enqueue(new MockResponse().setBody("A"));
  188. redirectTarget.start();
  189. HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
  190. MockWebServer redirectSource = new MockWebServer();
  191. redirectSource.enqueue(new MockResponse()
  192. .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
  193. .addHeader("Location: " + redirectTargetUrl));
  194. redirectSource.start();
  195. HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
  196. CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  197. HttpCookie cookie = new HttpCookie("c", "cookie");
  198. cookie.setDomain(redirectSourceUrl.host());
  199. cookie.setPath("/");
  200. String portList = Integer.toString(redirectSource.getPort());
  201. cookie.setPortlist(portList);
  202. cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
  203. client = client.newBuilder()
  204. .cookieJar(new JavaNetCookieJar(cookieManager))
  205. .build();
  206. get(redirectSourceUrl);
  207. RecordedRequest request = redirectSource.takeRequest();
  208. assertEquals("c=cookie", request.getHeader("Cookie"));
  209. for (String header : redirectTarget.takeRequest().getHeaders().names()) {
  210. if (header.startsWith("Cookie")) {
  211. fail(header);
  212. }
  213. }
  214. }
  215. @Test public void testCookiesSentIgnoresCase() throws Exception {
  216. client = client.newBuilder()
  217. .cookieJar(new JavaNetCookieJar(new CookieManager() {
  218. @Override public Map<String, List<String>> get(URI uri,
  219. Map<String, List<String>> requestHeaders) throws IOException {
  220. Map<String, List<String>> result = new HashMap<>();
  221. result.put("COOKIE", Collections.singletonList("Bar=bar"));
  222. result.put("cooKIE2", Collections.singletonList("Baz=baz"));
  223. return result;
  224. }
  225. }))
  226. .build();
  227. MockWebServer server = new MockWebServer();
  228. server.enqueue(new MockResponse());
  229. server.start();
  230. get(server.url("/"));
  231. RecordedRequest request = server.takeRequest();
  232. assertEquals("Bar=bar; Baz=baz", request.getHeader("Cookie"));
  233. assertNull(request.getHeader("Cookie2"));
  234. assertNull(request.getHeader("Quux"));
  235. }
  236. private HttpUrl urlWithIpAddress(MockWebServer server, String path) throws Exception {
  237. return server.url(path)
  238. .newBuilder()
  239. .host(InetAddress.getByName(server.getHostName()).getHostAddress())
  240. .build();
  241. }
  242. private void get(HttpUrl url) throws Exception {
  243. Call call = client.newCall(new Request.Builder()
  244. .url(url)
  245. .build());
  246. Response response = call.execute();
  247. response.body().close();
  248. }
  249. }