Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/ResponseCache/getResponseCode.java
41149 views
1
/*
2
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* 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 static java.net.Proxy.NO_PROXY;
34
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
static List<Closeable> resources = new ArrayList<>();
44
45
getResponseCode() throws Exception {
46
url = new URL("http://localhost/file1.cache");
47
HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);
48
int respCode = http.getResponseCode();
49
http.disconnect();
50
51
if (respCode != 200) {
52
throw new RuntimeException("Response code should return 200, but it is returning "+respCode);
53
}
54
}
55
public static void main(String args[]) throws Exception {
56
try {
57
ResponseCache.setDefault(new MyResponseCache());
58
FNPrefix = System.getProperty("test.src", ".")+"/";
59
new getResponseCode();
60
} finally{
61
ResponseCache.setDefault(null);
62
for (Closeable c : resources) {
63
try { c.close(); } catch (IOException unused) {}
64
}
65
}
66
}
67
68
static class MyResponseCache extends ResponseCache {
69
public CacheResponse
70
get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)
71
throws IOException {
72
return new MyResponse(FNPrefix+"file1.cache");
73
}
74
public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;
75
return null;
76
}
77
}
78
79
static class MyResponse extends CacheResponse {
80
FileInputStream fis;
81
Map<String,List<String>> headers;
82
public MyResponse(String filename) {
83
try {
84
fis = new FileInputStream(new File(filename));
85
resources.add(fis);
86
headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();
87
} catch (Exception ex) {
88
throw new RuntimeException(ex.getMessage());
89
}
90
}
91
public InputStream getBody() throws IOException {
92
return fis;
93
}
94
95
public Map<String,List<String>> getHeaders() throws IOException {
96
return headers;
97
}
98
}
99
}
100
101