• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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 
17 package libcore.java.net;
18 
19 import com.google.mockwebserver.MockResponse;
20 import com.google.mockwebserver.MockWebServer;
21 
22 import java.io.IOException;
23 import java.net.CookieHandler;
24 import java.net.URI;
25 import java.net.URL;
26 import java.net.URLConnection;
27 import java.util.Map;
28 import junit.framework.TestCase;
29 import tests.support.Support_Configuration;
30 
31 public class OldCookieHandlerTest extends TestCase {
32 
33     private MockWebServer server;
34 
35     @Override
setUp()36     public void setUp() throws Exception {
37         super.setUp();
38         server = new MockWebServer();
39     }
40 
41     @Override
tearDown()42     public void tearDown() throws Exception {
43         server.shutdown();
44         super.tearDown();
45     }
46 
test_CookieHandler()47     public void test_CookieHandler() {
48         assertNull(CookieHandler.getDefault());
49     }
50 
test_get_put()51     public void test_get_put() throws Exception {
52         MockCookieHandler mch = new MockCookieHandler();
53         CookieHandler defaultHandler = CookieHandler.getDefault();
54         try {
55             CookieHandler.setDefault(mch);
56 
57             server.play();
58             server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=\"android\"; "
59                     + "Comment=\"this cookie is delicious\"; "
60                     + "CommentURL=\"http://google.com/\"; "
61                     + "Discard; "
62                     + "Domain=\"" + server.getCookieDomain() + "\"; "
63                     + "Max-Age=\"60\"; "
64                     + "Path=\"/path\"; "
65                     + "Port=\"80,443," + server.getPort() + "\"; "
66                     + "Secure; "
67                     + "Version=\"1\""));
68 
69             URLConnection connection = server.getUrl("/path/foo").openConnection();
70             connection.getContent();
71 
72             assertTrue(mch.wasGetCalled());
73             assertTrue(mch.wasPutCalled());
74         } finally {
75             CookieHandler.setDefault(defaultHandler);
76         }
77     }
78 
79     private static class MockCookieHandler extends CookieHandler {
80         private boolean getCalled = false;
81         private boolean putCalled = false;
82 
get(URI uri, Map requestHeaders)83         public Map get(URI uri, Map requestHeaders) throws IOException {
84             getCalled = true;
85             return requestHeaders;
86         }
87 
put(URI uri, Map responseHeaders)88         public void put(URI uri, Map responseHeaders) throws IOException {
89             putCalled = true;
90         }
91 
wasGetCalled()92         public boolean wasGetCalled() {
93             return getCalled;
94         }
wasPutCalled()95         public boolean wasPutCalled() {
96             return putCalled;
97         }
98     }
99 }
100