Path: blob/master/test/jdk/sun/net/www/protocol/http/B6660405.java
41159 views
/*1* Copyright (c) 2008, 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/*24* @test25* @bug 666040526* @modules jdk.httpserver27* @library /test/lib28* @run main/othervm B666040529* @run main/othervm -Djava.net.preferIPv6Addresses=true B666040530* @summary HttpURLConnection returns the wrong InputStream B666040531*/3233import java.net.*;34import java.util.*;35import java.io.*;36import com.sun.net.httpserver.*;37import java.util.concurrent.Executors;38import java.util.concurrent.ExecutorService;39import jdk.test.lib.net.URIBuilder;404142public class B666040543{44com.sun.net.httpserver.HttpServer httpServer;45ExecutorService executorService;4647static class MyCacheResponse extends CacheResponse {48private byte[] buf = new byte[1024];4950public MyCacheResponse() {51}5253@Override54public Map<String, List<String>> getHeaders() throws IOException55{56Map<String, List<String>> h = new HashMap<String, List<String>>();57ArrayList<String> l = new ArrayList<String>();58l.add("HTTP/1.1 200 OK");59h.put(null, l);60l = new ArrayList<String>();61l.add("1024");62h.put("Content-Length", l);63return h;64}6566@Override67public InputStream getBody() throws IOException68{69return new ByteArrayInputStream(buf);70}7172}73static class MyResponseCache extends ResponseCache {7475public MyResponseCache() {76}7778@Override79public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders)80throws IOException81{82if (uri.getPath().equals("/redirect/index.html")) {83return new MyCacheResponse();84}85return null;86}8788@Override89public CacheRequest put(URI uri, URLConnection conn) throws IOException90{91return null;92}9394}9596public static void main(String[] args) throws Exception97{98new B6660405();99}100101public B6660405() throws Exception {102startHttpServer();103doClient();104}105106void doClient() throws Exception {107ResponseCache.setDefault(new MyResponseCache());108InetSocketAddress address = httpServer.getAddress();109110// GET Request111URL url = URIBuilder.newBuilder()112.scheme("http")113.host(address.getAddress())114.port(address.getPort())115.path("/test/index.html")116.toURL();117118HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);119int code = uc.getResponseCode();120System.err.println("response code = " + code);121int l = uc.getContentLength();122System.err.println("content-length = " + l);123if (l != 1024) {124throw new AssertionError("Bad content length: " + l);125}126127InputStream in = uc.getInputStream();128int i = 0;129// Read till end of stream130do {131l--;132i = in.read();133} while (i != -1);134in.close();135if (l != -1) {136throw new AssertionError("Only " + (1024 - (l + 1))137+ " bytes read from stream.");138}139140httpServer.stop(1);141executorService.shutdown();142}143144/**145* Http Server146*/147public void startHttpServer() throws IOException {148InetAddress loopback = InetAddress.getLoopbackAddress();149InetSocketAddress address = new InetSocketAddress(loopback,0);150httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);151152// create HttpServer context153HttpContext ctx = httpServer.createContext("/test/", new MyHandler());154155executorService = Executors.newCachedThreadPool();156httpServer.setExecutor(executorService);157httpServer.start();158}159160class MyHandler implements HttpHandler {161public void handle(HttpExchange t) throws IOException {162InputStream is = t.getRequestBody();163Headers reqHeaders = t.getRequestHeaders();164Headers resHeaders = t.getResponseHeaders();165166int i = 0;167// Read till end of stream168do {169i = is.read();170} while (i != -1);171is.close();172resHeaders.add("Location", "http://foo.bar/redirect/index.html");173t.sendResponseHeaders(302, -1);174t.close();175}176}177}178179180