Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ResponseCache/getResponseCode.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 getResponseCode() doesn't return correct value when using cached response25* @bug 492126826* @author Yingxian Wang27*/2829import java.net.*;30import java.util.*;31import java.io.*;323334/**35* Request should get serviced by the cache handler. Response get36* saved through the cache handler.37*/38public class getResponseCode {39static URL url;40static String FNPrefix;41static List<Closeable> resources = new ArrayList<>();4243getResponseCode() throws Exception {44url = new URL("http://localhost/file1.cache");45HttpURLConnection http = (HttpURLConnection)url.openConnection();46int respCode = http.getResponseCode();47http.disconnect();4849if (respCode != 200) {50throw new RuntimeException("Response code should return 200, but it is returning "+respCode);51}52}53public static void main(String args[]) throws Exception {54try {55ResponseCache.setDefault(new MyResponseCache());56FNPrefix = System.getProperty("test.src", ".")+"/";57new getResponseCode();58} finally{59ResponseCache.setDefault(null);60for (Closeable c : resources) {61try { c.close(); } catch (IOException unused) {}62}63}64}6566static class MyResponseCache extends ResponseCache {67public CacheResponse68get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)69throws IOException {70return new MyResponse(FNPrefix+"file1.cache");71}72public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;73return null;74}75}7677static class MyResponse extends CacheResponse {78FileInputStream fis;79Map<String,List<String>> headers;80public MyResponse(String filename) {81try {82fis = new FileInputStream(new File(filename));83resources.add(fis);84headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();85} catch (Exception ex) {86throw new RuntimeException(ex.getMessage());87}88}89public InputStream getBody() throws IOException {90return fis;91}9293public Map<String,List<String>> getHeaders() throws IOException {94return headers;95}96}97}9899100