0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary getResponseCode() doesn't return correct value when using cached response
0N/A * @bug 4921268
0N/A * @author Yingxian Wang
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
2244N/A
0N/A
0N/A/**
0N/A * Request should get serviced by the cache handler. Response get
0N/A * saved through the cache handler.
0N/A */
0N/Apublic class getResponseCode {
0N/A static URL url;
0N/A static String FNPrefix;
2612N/A static List<Closeable> resources = new ArrayList<>();
0N/A
0N/A getResponseCode() throws Exception {
0N/A url = new URL("http://localhost/file1.cache");
0N/A HttpURLConnection http = (HttpURLConnection)url.openConnection();
0N/A int respCode = http.getResponseCode();
0N/A http.disconnect();
0N/A
0N/A if (respCode != 200) {
0N/A throw new RuntimeException("Response code should return 200, but it is returning "+respCode);
0N/A }
0N/A }
0N/A public static void main(String args[]) throws Exception {
2244N/A try {
2244N/A ResponseCache.setDefault(new MyResponseCache());
2244N/A FNPrefix = System.getProperty("test.src", ".")+"/";
2244N/A new getResponseCode();
2244N/A } finally{
2244N/A ResponseCache.setDefault(null);
2612N/A for (Closeable c : resources) {
2612N/A try { c.close(); } catch (IOException unused) {}
2612N/A }
2244N/A }
0N/A }
0N/A
0N/A static class MyResponseCache extends ResponseCache {
0N/A public CacheResponse
0N/A get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders)
0N/A throws IOException {
0N/A return new MyResponse(FNPrefix+"file1.cache");
0N/A }
0N/A public CacheRequest put(URI uri, URLConnection uconn) throws IOException {;
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A static class MyResponse extends CacheResponse {
0N/A FileInputStream fis;
0N/A Map<String,List<String>> headers;
0N/A public MyResponse(String filename) {
0N/A try {
0N/A fis = new FileInputStream(new File(filename));
2612N/A resources.add(fis);
0N/A headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject();
0N/A } catch (Exception ex) {
0N/A throw new RuntimeException(ex.getMessage());
0N/A }
0N/A }
0N/A public InputStream getBody() throws IOException {
0N/A return fis;
0N/A }
0N/A
0N/A public Map<String,List<String>> getHeaders() throws IOException {
0N/A return headers;
0N/A }
0N/A }
0N/A}