2
|
1 |
/*
|
|
2 |
* Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @summary getResponseCode() doesn't return correct value when using cached response
|
|
26 |
* @bug 4921268
|
|
27 |
* @author Yingxian Wang
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
|
31 |
import java.util.*;
|
|
32 |
import java.io.*;
|
|
33 |
import java.nio.*;
|
|
34 |
import sun.net.www.ParseUtil;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Request should get serviced by the cache handler. Response get
|
|
38 |
* saved through the cache handler.
|
|
39 |
*/
|
|
40 |
public class getResponseCode {
|
|
41 |
static URL url;
|
|
42 |
static String FNPrefix;
|
|
43 |
|
|
44 |
getResponseCode() throws Exception {
|
|
45 |
url = new URL("http://localhost/file1.cache");
|
|
46 |
HttpURLConnection http = (HttpURLConnection)url.openConnection();
|
|
47 |
int respCode = http.getResponseCode();
|
|
48 |
http.disconnect();
|
|
49 |
|
|
50 |
if (respCode != 200) {
|
|
51 |
throw new RuntimeException("Response code should return 200, but it is returning "+respCode);
|
|
52 |
}
|
|
53 |
}
|
|
54 |
public static void main(String args[]) throws Exception {
|
|
55 |
ResponseCache.setDefault(new MyResponseCache());
|
|
56 |
FNPrefix = System.getProperty("test.src", ".")+"/";
|
|
57 |
new getResponseCode();
|
|
58 |
}
|
|
59 |
|
|
60 |
static class MyResponseCache extends ResponseCache {
|
|
61 |
public CacheResponse
|
|
62 |
get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)
|
|
63 |
throws IOException {
|
|
64 |
return new MyResponse(FNPrefix+"file1.cache");
|
|
65 |
}
|
|
66 |
public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;
|
|
67 |
return null;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
static class MyResponse extends CacheResponse {
|
|
72 |
FileInputStream fis;
|
|
73 |
Map<String,List<String>> headers;
|
|
74 |
public MyResponse(String filename) {
|
|
75 |
try {
|
|
76 |
fis = new FileInputStream(new File(filename));
|
|
77 |
headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();
|
|
78 |
} catch (Exception ex) {
|
|
79 |
throw new RuntimeException(ex.getMessage());
|
|
80 |
}
|
|
81 |
}
|
|
82 |
public InputStream getBody() throws IOException {
|
|
83 |
return fis;
|
|
84 |
}
|
|
85 |
|
|
86 |
public Map<String,List<String>> getHeaders() throws IOException {
|
|
87 |
return headers;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|