getResponseCode.java revision 2244
0N/A/*
0N/A * Copyright 2003-2004 Sun Microsystems, Inc. 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.
873N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
733N/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.*;
0N/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;
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 {
0N/A try {
0N/A ResponseCache.setDefault(new MyResponseCache());
0N/A FNPrefix = System.getProperty("test.src", ".")+"/";
0N/A new getResponseCode();
0N/A } finally{
0N/A ResponseCache.setDefault(null);
0N/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));
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}
0N/A