1/* 2 * Copyright (c) 2003, 2010, 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 30import java.net.*; 31import java.util.*; 32import java.io.*; 33 34 35/** 36 * Request should get serviced by the cache handler. Response get 37 * saved through the cache handler. 38 */ 39public class getResponseCode { 40 static URL url; 41 static String FNPrefix; 42 static List<Closeable> resources = new ArrayList<>(); 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 try { 56 ResponseCache.setDefault(new MyResponseCache()); 57 FNPrefix = System.getProperty("test.src", ".")+"/"; 58 new getResponseCode(); 59 } finally{ 60 ResponseCache.setDefault(null); 61 for (Closeable c : resources) { 62 try { c.close(); } catch (IOException unused) {} 63 } 64 } 65 } 66 67 static class MyResponseCache extends ResponseCache { 68 public CacheResponse 69 get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders) 70 throws IOException { 71 return new MyResponse(FNPrefix+"file1.cache"); 72 } 73 public CacheRequest put(URI uri, URLConnection uconn) throws IOException {; 74 return null; 75 } 76 } 77 78 static class MyResponse extends CacheResponse { 79 FileInputStream fis; 80 Map<String,List<String>> headers; 81 public MyResponse(String filename) { 82 try { 83 fis = new FileInputStream(new File(filename)); 84 resources.add(fis); 85 headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject(); 86 } catch (Exception ex) { 87 throw new RuntimeException(ex.getMessage()); 88 } 89 } 90 public InputStream getBody() throws IOException { 91 return fis; 92 } 93 94 public Map<String,List<String>> getHeaders() throws IOException { 95 return headers; 96 } 97 } 98} 99