Path: blob/master/test/jdk/java/net/ResponseCache/getResponseCode.java
41149 views
/*1* Copyright (c) 2003, 2019, 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.*;32import static java.net.Proxy.NO_PROXY;333435/**36* Request should get serviced by the cache handler. Response get37* saved through the cache handler.38*/39public class getResponseCode {40static URL url;41static String FNPrefix;42static List<Closeable> resources = new ArrayList<>();4344getResponseCode() throws Exception {45url = new URL("http://localhost/file1.cache");46HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);47int respCode = http.getResponseCode();48http.disconnect();4950if (respCode != 200) {51throw new RuntimeException("Response code should return 200, but it is returning "+respCode);52}53}54public static void main(String args[]) throws Exception {55try {56ResponseCache.setDefault(new MyResponseCache());57FNPrefix = System.getProperty("test.src", ".")+"/";58new getResponseCode();59} finally{60ResponseCache.setDefault(null);61for (Closeable c : resources) {62try { c.close(); } catch (IOException unused) {}63}64}65}6667static class MyResponseCache extends ResponseCache {68public CacheResponse69get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)70throws IOException {71return new MyResponse(FNPrefix+"file1.cache");72}73public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;74return null;75}76}7778static class MyResponse extends CacheResponse {79FileInputStream fis;80Map<String,List<String>> headers;81public MyResponse(String filename) {82try {83fis = new FileInputStream(new File(filename));84resources.add(fis);85headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();86} catch (Exception ex) {87throw new RuntimeException(ex.getMessage());88}89}90public InputStream getBody() throws IOException {91return fis;92}9394public Map<String,List<String>> getHeaders() throws IOException {95return headers;96}97}98}99100101