duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | |
| 30 | import java.net.*; |
| 31 | import java.util.*; |
| 32 | import java.io.*; |
chegar | f514c56 | 2010-03-25 09:38:56 +0000 | [diff] [blame] | 33 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Request should get serviced by the cache handler. Response get |
| 37 | * saved through the cache handler. |
| 38 | */ |
| 39 | public class getResponseCode { |
| 40 | static URL url; |
| 41 | static String FNPrefix; |
| 42 | |
| 43 | getResponseCode() throws Exception { |
| 44 | url = new URL("http://localhost/file1.cache"); |
| 45 | HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
| 46 | int respCode = http.getResponseCode(); |
| 47 | http.disconnect(); |
| 48 | |
| 49 | if (respCode != 200) { |
| 50 | throw new RuntimeException("Response code should return 200, but it is returning "+respCode); |
| 51 | } |
| 52 | } |
| 53 | public static void main(String args[]) throws Exception { |
chegar | f514c56 | 2010-03-25 09:38:56 +0000 | [diff] [blame] | 54 | try { |
| 55 | ResponseCache.setDefault(new MyResponseCache()); |
| 56 | FNPrefix = System.getProperty("test.src", ".")+"/"; |
| 57 | new getResponseCode(); |
| 58 | } finally{ |
| 59 | ResponseCache.setDefault(null); |
| 60 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static class MyResponseCache extends ResponseCache { |
| 64 | public CacheResponse |
| 65 | get(URI uri, String rqstMethod, Map<String,List<String>> requestHeaders) |
| 66 | throws IOException { |
| 67 | return new MyResponse(FNPrefix+"file1.cache"); |
| 68 | } |
| 69 | public CacheRequest put(URI uri, URLConnection uconn) throws IOException {; |
| 70 | return null; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static class MyResponse extends CacheResponse { |
| 75 | FileInputStream fis; |
| 76 | Map<String,List<String>> headers; |
| 77 | public MyResponse(String filename) { |
| 78 | try { |
| 79 | fis = new FileInputStream(new File(filename)); |
| 80 | headers = (Map<String,List<String>>)new ObjectInputStream(fis).readObject(); |
| 81 | } catch (Exception ex) { |
| 82 | throw new RuntimeException(ex.getMessage()); |
| 83 | } |
| 84 | } |
| 85 | public InputStream getBody() throws IOException { |
| 86 | return fis; |
| 87 | } |
| 88 | |
| 89 | public Map<String,List<String>> getHeaders() throws IOException { |
| 90 | return headers; |
| 91 | } |
| 92 | } |
| 93 | } |