Path: blob/master/test/jdk/java/net/URLConnection/Responses.java
41149 views
/*1* Copyright (c) 2002, 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 463569826* @summary Check that HttpURLConnection.getResponseCode returns -1 for27* malformed status-lines in the http response.28*/29import java.net.*;30import java.io.*;31import static java.net.Proxy.NO_PROXY;3233public class Responses {3435/*36* Test cases :-37* "Response from server" expected getResponse() and38* getResponseMessage() results39*/40static Object[][] getTests() {41return new Object[][] {42{ "HTTP/1.1 200 OK", "200", "OK" },43{ "HTTP/1.1 404 ", "404", null },44{ "HTTP/1.1 200", "200", null },45{ "HTTP/1.1", "-1", null },46{ "Invalid", "-1", null },47{ null, "-1" , null },48};49}5051/*52* Simple http server used by test53*54* GET /<n> HTTP/1.x results in http response with the status line55* set to geTests()[<n>][0] -- eg: GET /2 results in a response of56* "HTTP/1.1 404 "57*/58static class HttpServer implements Runnable {59final ServerSocket ss;60volatile boolean shutdown;6162public HttpServer() {63try {64InetAddress loopback = InetAddress.getLoopbackAddress();65ss = new ServerSocket();66ss.bind(new InetSocketAddress(loopback, 0));67} catch (IOException ioe) {68throw new Error("Unable to create ServerSocket: " + ioe);69}70}7172public int port() {73return ss.getLocalPort();74}7576public String authority() {77InetAddress address = ss.getInetAddress();78String hostaddr = address.isAnyLocalAddress()79? "localhost" : address.getHostAddress();80if (hostaddr.indexOf(':') > -1) {81hostaddr = "[" + hostaddr + "]";82}83return hostaddr + ":" + port();84}8586public void shutdown() throws IOException {87shutdown = true;88ss.close();89}9091public void run() {92Object[][] tests = getTests();9394try {95while(!shutdown) {96Socket s = ss.accept();9798BufferedReader in = new BufferedReader(99new InputStreamReader(100s.getInputStream()));101String req = in.readLine();102int pos1 = req.indexOf(' ');103int pos2 = req.indexOf(' ', pos1+1);104105int i = Integer.parseInt(req.substring(pos1+2, pos2));106System.out.println("Server replying to >" + tests[i][0] + "<");107108PrintStream out = new PrintStream(109new BufferedOutputStream(110s.getOutputStream() ));111112out.print( tests[i][0] );113out.print("\r\n");114out.print("Content-Length: 0\r\n");115out.print("Connection: close\r\n");116out.print("\r\n");117out.flush();118119s.shutdownOutput();120s.close();121}122} catch (Exception e) {123if (!shutdown) {124e.printStackTrace();125}126}127}128}129130131public static void main(String args[]) throws Exception {132133/* start the http server */134HttpServer svr = new HttpServer();135(new Thread(svr)).start();136137String authority = svr.authority();138System.out.println("Server listening on: " + authority);139140/*141* Iterate through each test case and check that getResponseCode142* returns the expected result.143*/144int failures = 0;145Object tests[][] = getTests();146for (int i=0; i<tests.length; i++) {147148System.out.println("******************");149System.out.println("Test with response: >" + tests[i][0] + "<");150151URL url = new URL("http://" + authority + "/" + i);152HttpURLConnection http = (HttpURLConnection)url.openConnection(NO_PROXY);153154try {155156// test getResponseCode157//158int expectedCode = Integer.parseInt((String)tests[i][1]);159int actualCode = http.getResponseCode();160if (actualCode != expectedCode) {161System.out.println("getResponseCode returned: " + actualCode +162", expected: " + expectedCode);163failures++;164continue;165}166167// test getResponseMessage168//169String expectedPhrase = (String)tests[i][2];170String actualPhrase = http.getResponseMessage();171if (actualPhrase == null && expectedPhrase == null) {172continue;173}174if (!actualPhrase.equals(expectedPhrase)) {175System.out.println("getResponseMessage returned: " +176actualPhrase + ", expected: " + expectedPhrase);177}178} catch (IOException e) {179System.err.println("Test failed for >" + tests[i][0] + "<: " + e);180e.printStackTrace();181failures++;182}183}184185/* shutdown http server */186svr.shutdown();187188if (failures > 0) {189throw new Exception(failures + " sub-test(s) failed.");190}191}192}193194195