OSDN Git Service

Rewrite the HTTP connection pool used by HttpURLConnection.
[android-x86/dalvik.git] / libcore / luni / src / test / java / java / net / URLConnectionTest.java
1 /*
2  * Copyright (C) 2009 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
17 package java.net;
18
19 import java.io.BufferedReader;
20 import java.io.InputStreamReader;
21 import tests.support.Support_TestWebServer;
22
23 public class URLConnectionTest extends junit.framework.TestCase {
24     private int mPort;
25     private Support_TestWebServer mServer;
26     
27     @Override
28     public void setUp() throws Exception {
29         super.setUp();
30         mServer = new Support_TestWebServer();
31         mPort = mServer.initServer(0, false);
32     }
33     
34     @Override
35     public void tearDown() throws Exception {
36         super.tearDown();
37         mServer.close();
38     }
39     
40     private String readFirstLine() throws Exception {
41         URLConnection connection = new URL("http://localhost:" + mPort + "/test1").openConnection();
42         BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
43         String result = in.readLine();
44         in.close();
45         return result;
46     }
47     
48     // Check that if we don't read to the end of a response, the next request on the
49     // recycled connection doesn't get the unread tail of the first request's response.
50     // http://code.google.com/p/android/issues/detail?id=2939
51     public void test_2939() throws Exception {
52         mServer.setChunked(true);
53         mServer.setMaxChunkSize(8);
54         assertTrue(readFirstLine().equals("<html>"));
55         assertTrue(readFirstLine().equals("<html>"));
56         assertEquals(1, mServer.getNumAcceptedConnections());
57     }
58
59     public void testConnectionsArePooled() throws Exception {
60         readFirstLine();
61         readFirstLine();
62         readFirstLine();
63         assertEquals(1, mServer.getNumAcceptedConnections());
64     }
65 }