Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/ResponseCacheTest.java
38811 views
/*1* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Unit test for java.net.ResponseCache25* @bug 483726726* @author Yingxian Wang27*/2829import java.net.*;30import java.util.*;31import java.io.*;32import sun.net.www.ParseUtil;33import javax.net.ssl.*;3435/**36* Request should get serviced by the cache handler. Response get37* saved through the cache handler.38*/39public class ResponseCacheTest implements Runnable {40ServerSocket ss;41static URL url1;42static URL url2;43static String FNPrefix, OutFNPrefix;44static List<Closeable> streams = new ArrayList<>();45static List<File> files = new ArrayList<>();4647/*48* Our "http" server to return a 404 */49public void run() {50Socket s = null;51FileInputStream fis = null;52try {53s = ss.accept();5455InputStream is = s.getInputStream ();56BufferedReader r = new BufferedReader(new InputStreamReader(is));57String x;58while ((x=r.readLine()) != null) {59if (x.length() ==0) {60break;61}62}63PrintStream out = new PrintStream(64new BufferedOutputStream(65s.getOutputStream() ));6667/* send file2.1 */68File file2 = new File(FNPrefix+"file2.1");69out.print("HTTP/1.1 200 OK\r\n");70out.print("Content-Type: text/html; charset=iso-8859-1\r\n");71out.print("Content-Length: "+file2.length()+"\r\n");72out.print("Connection: close\r\n");73out.print("\r\n");74fis = new FileInputStream(file2);75byte[] buf = new byte[(int)file2.length()];76int len;77while ((len = fis.read(buf)) != -1) {78out.print(new String(buf));79}8081out.flush();8283s.close();84ss.close();85} catch (Exception e) {86e.printStackTrace();87} finally {88try { ss.close(); } catch (IOException unused) {}89try { s.close(); } catch (IOException unused) {}90try { fis.close(); } catch (IOException unused) {}91}92}93static class NameVerifier implements HostnameVerifier {94public boolean verify(String hostname, SSLSession session) {95return true;96}97}98ResponseCacheTest() throws Exception {99/* start the server */100ss = new ServerSocket(0);101(new Thread(this)).start();102/* establish http connection to server */103url1 = new URL("http://localhost/file1.cache");104HttpURLConnection http = (HttpURLConnection)url1.openConnection();105InputStream is = null;106System.out.println("request headers: "+http.getRequestProperties());107System.out.println("responsecode is :"+http.getResponseCode());108Map<String,List<String>> headers1 = http.getHeaderFields();109try {110is = http.getInputStream();111} catch (IOException ioex) {112throw new RuntimeException(ioex.getMessage());113}114BufferedReader r = new BufferedReader(new InputStreamReader(is));115String x;116File fileout = new File(OutFNPrefix+"file1");117PrintStream out = new PrintStream(118new BufferedOutputStream(119new FileOutputStream(fileout)));120while ((x=r.readLine()) != null) {121out.print(x+"\n");122}123out.flush();124out.close();125126http.disconnect();127128// testing ResponseCacheHandler.put()129url2 = new URL("http://localhost:" +130Integer.toString(ss.getLocalPort())+"/file2.1");131http = (HttpURLConnection)url2.openConnection();132System.out.println("responsecode2 is :"+http.getResponseCode());133Map<String,List<String>> headers2 = http.getHeaderFields();134135try {136is = http.getInputStream();137} catch (IOException ioex) {138throw new RuntimeException(ioex.getMessage());139}140r = new BufferedReader(new InputStreamReader(is));141fileout = new File(OutFNPrefix+"file2.2");142out = new PrintStream(143new BufferedOutputStream(144new FileOutputStream(fileout)));145while ((x=r.readLine()) != null) {146out.print(x+"\n");147}148out.flush();149out.close();150151// assert (headers1 == headers2 && file1 == file2.2)152File file1 = new File(OutFNPrefix+"file1");153File file2 = new File(OutFNPrefix+"file2.2");154files.add(file1);155files.add(file2);156System.out.println("headers1"+headers1+"\nheaders2="+headers2);157if (!headers1.equals(headers2) || file1.length() != file2.length()) {158throw new RuntimeException("test failed");159}160}161162public static void main(String args[]) throws Exception {163try {164ResponseCache.setDefault(new MyResponseCache());165FNPrefix = System.getProperty("test.src", ".")+"/";166OutFNPrefix = System.getProperty("test.scratch", ".")+"/";167new ResponseCacheTest();168} finally{169ResponseCache.setDefault(null);170for (Closeable c: streams) {171try { c.close(); } catch (IOException unused) {}172}173for (File f: files) {174f.delete();175}176}177}178179static class MyResponseCache extends ResponseCache {180public CacheResponse181get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)182throws IOException {183if (uri.equals(ParseUtil.toURI(url1))) {184return new MyCacheResponse(FNPrefix+"file1.cache");185}186return null;187}188189public CacheRequest put(URI uri, URLConnection conn) throws IOException {190// save cache to file2.cache191// 1. serialize headers into file2.cache192// 2. write data to file2.cache193return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());194}195}196197static class MyCacheResponse extends CacheResponse {198FileInputStream fis;199Map<String,List<String>> headers;200public MyCacheResponse(String filename) {201try {202fis = new FileInputStream(new File(filename));203streams.add(fis);204ObjectInputStream ois = new ObjectInputStream(fis);205headers = (Map<String,List<String>>)ois.readObject();206} catch (Exception ex) {207// throw new RuntimeException(ex.getMessage());208}209}210211public InputStream getBody() throws IOException {212return fis;213}214215public Map<String,List<String>> getHeaders() throws IOException {216return headers;217}218}219220static class MyCacheRequest extends CacheRequest {221FileOutputStream fos;222public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {223try {224File file = new File(filename);225fos = new FileOutputStream(file);226streams.add(fos);227files.add(file);228ObjectOutputStream oos = new ObjectOutputStream(fos);229oos.writeObject(rspHeaders);230} catch (Exception ex) {231throw new RuntimeException(ex.getMessage());232}233}234public OutputStream getBody() throws IOException {235return fos;236}237238public void abort() {239// no op240}241}242243244}245246247