blob: 7d768d8060e8ebe90604e8cbf9233f61835cb04d [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/* @test
25 * @summary Unit test for java.net.ResponseCache
26 * @bug 4837267
27 * @author Yingxian Wang
28 */
29
30import java.net.*;
31import java.util.*;
32import java.io.*;
33import java.nio.*;
34import sun.net.www.ParseUtil;
35import javax.net.ssl.*;
36
37/**
38 * Request should get serviced by the cache handler. Response get
39 * saved through the cache handler.
40 */
41public class ResponseCacheTest implements Runnable {
42 ServerSocket ss;
43 static URL url1;
44 static URL url2;
45 static String FNPrefix, OutFNPrefix;
46 /*
47 * Our "http" server to return a 404 */
48 public void run() {
49 try {
50 Socket s = ss.accept();
51
52 InputStream is = s.getInputStream ();
53 BufferedReader r = new BufferedReader(new InputStreamReader(is));
54 String x;
55 while ((x=r.readLine()) != null) {
56 if (x.length() ==0) {
57 break;
58 }
59 }
60 PrintStream out = new PrintStream(
61 new BufferedOutputStream(
62 s.getOutputStream() ));
63
64 /* send file2.1 */
65 File file2 = new File(FNPrefix+"file2.1");
66 out.print("HTTP/1.1 200 OK\r\n");
67 out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
68 out.print("Content-Length: "+file2.length()+"\r\n");
69 out.print("Connection: close\r\n");
70 out.print("\r\n");
71 FileInputStream fis = new FileInputStream(file2);
72 byte[] buf = new byte[(int)file2.length()];
73 int len;
74 while ((len = fis.read(buf)) != -1) {
75 out.print(new String(buf));
76 }
77
78 out.flush();
79
80 s.close();
81 ss.close();
82 } catch (Exception e) {
83 e.printStackTrace();
84 }
85 }
86static class NameVerifier implements HostnameVerifier {
87 public boolean verify(String hostname, SSLSession session) {
88 return true;
89 }
90 }
91 ResponseCacheTest() throws Exception {
92 /* start the server */
93 ss = new ServerSocket(0);
94 (new Thread(this)).start();
95 /* establish http connection to server */
96 url1 = new URL("http://localhost/file1.cache");
97 HttpURLConnection http = (HttpURLConnection)url1.openConnection();
98 InputStream is = null;
99 System.out.println("request headers: "+http.getRequestProperties());
100 System.out.println("responsecode is :"+http.getResponseCode());
101 Map<String,List<String>> headers1 = http.getHeaderFields();
102 try {
103 is = http.getInputStream();
104 } catch (IOException ioex) {
105 throw new RuntimeException(ioex.getMessage());
106 }
107 BufferedReader r = new BufferedReader(new InputStreamReader(is));
108 String x;
109 File fileout = new File(OutFNPrefix+"file1");
110 PrintStream out = new PrintStream(
111 new BufferedOutputStream(
112 new FileOutputStream(fileout)));
113 while ((x=r.readLine()) != null) {
114 out.print(x+"\n");
115 }
116 out.flush();
117 out.close();
118
119 http.disconnect();
120
121 // testing ResponseCacheHandler.put()
122 url2 = new URL("http://localhost:" +
123 Integer.toString(ss.getLocalPort())+"/file2.1");
124 http = (HttpURLConnection)url2.openConnection();
125 System.out.println("responsecode2 is :"+http.getResponseCode());
126 Map<String,List<String>> headers2 = http.getHeaderFields();
127
128 try {
129 is = http.getInputStream();
130 } catch (IOException ioex) {
131 throw new RuntimeException(ioex.getMessage());
132 }
133 r = new BufferedReader(new InputStreamReader(is));
134 fileout = new File(OutFNPrefix+"file2.2");
135 out = new PrintStream(
136 new BufferedOutputStream(
137 new FileOutputStream(fileout)));
138 while ((x=r.readLine()) != null) {
139 out.print(x+"\n");
140 }
141 out.flush();
142 out.close();
143
144 // assert (headers1 == headers2 && file1 == file2.2)
145 File file1 = new File(OutFNPrefix+"file1");
146 File file2 = new File(OutFNPrefix+"file2.2");
147 System.out.println("headers1"+headers1+"\nheaders2="+headers2);
148 if (!headers1.equals(headers2) || file1.length() != file2.length()) {
149 throw new RuntimeException("test failed");
150 }
151 }
152 public static void main(String args[]) throws Exception {
chegarf514c562010-03-25 09:38:56 +0000153 try {
154 ResponseCache.setDefault(new MyResponseCache());
155 FNPrefix = System.getProperty("test.src", ".")+"/";
156 OutFNPrefix = System.getProperty("test.scratch", ".")+"/";
157 new ResponseCacheTest();
158 } finally{
159 ResponseCache.setDefault(null);
160 }
duke6e45e102007-12-01 00:00:00 +0000161 }
162
163 static class MyResponseCache extends ResponseCache {
164 public CacheResponse
165 get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
166 throws IOException {
167 if (uri.equals(ParseUtil.toURI(url1))) {
168 return new MyCacheResponse(FNPrefix+"file1.cache");
169 }
170 return null;
171 }
172
173 public CacheRequest put(URI uri, URLConnection conn) throws IOException {
174 // save cache to file2.cache
175 // 1. serialize headers into file2.cache
176 // 2. write data to file2.cache
177 return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());
178 }
179 }
180
181 static class MyCacheResponse extends CacheResponse {
182 FileInputStream fis;
183 Map<String,List<String>> headers;
184 public MyCacheResponse(String filename) {
185 try {
186 fis = new FileInputStream(new File(filename));
187 ObjectInputStream ois = new ObjectInputStream(fis);
188 headers = (Map<String,List<String>>)ois.readObject();
189 } catch (Exception ex) {
190 // throw new RuntimeException(ex.getMessage());
191 }
192 }
193
194 public InputStream getBody() throws IOException {
195 return fis;
196 }
197
198 public Map<String,List<String>> getHeaders() throws IOException {
199 return headers;
200 }
201 }
202
203 static class MyCacheRequest extends CacheRequest {
204 FileOutputStream fos;
205 public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {
206 try {
207 File file = new File(filename);
208 fos = new FileOutputStream(file);
209 ObjectOutputStream oos = new ObjectOutputStream(fos);
210 oos.writeObject(rspHeaders);
211 } catch (Exception ex) {
212 throw new RuntimeException(ex.getMessage());
213 }
214 }
215 public OutputStream getBody() throws IOException {
216 return fos;
217 }
218
219 public void abort() {
220 // no op
221 }
222 }
223
224
225}