Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/Responses.java
38812 views
/*1* Copyright (c) 2002, 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.*;3132public class Responses {3334/*35* Test cases :-36* "Response from server" expected getResponse() and37* getResponseMessage() results38*/39static Object[][] getTests() {40return new Object[][] {41{ "HTTP/1.1 200 OK", "200", "OK" },42{ "HTTP/1.1 404 ", "404", null },43{ "HTTP/1.1 200", "200", null },44{ "HTTP/1.1", "-1", null },45{ "Invalid", "-1", null },46{ null, "-1" , null },47};48}4950/*51* Simple http server used by test52*53* GET /<n> HTTP/1.x results in http response with the status line54* set to geTests()[<n>][0] -- eg: GET /2 results in a response of55* "HTTP/1.1 404 "56*/57static class HttpServer implements Runnable {58ServerSocket ss;5960public HttpServer() {61try {62ss = new ServerSocket(0);63} catch (IOException ioe) {64throw new Error("Unable to create ServerSocket: " + ioe);65}66}6768public int port() {69return ss.getLocalPort();70}7172public void shutdown() throws IOException {73ss.close();74}7576public void run() {77Object[][] tests = getTests();7879try {80for (;;) {81Socket s = ss.accept();8283BufferedReader in = new BufferedReader(84new InputStreamReader(85s.getInputStream()));86String req = in.readLine();87int pos1 = req.indexOf(' ');88int pos2 = req.indexOf(' ', pos1+1);8990int i = Integer.parseInt(req.substring(pos1+2, pos2));9192PrintStream out = new PrintStream(93new BufferedOutputStream(94s.getOutputStream() ));9596out.print( tests[i][0] );97out.print("\r\n");98out.print("Content-Length: 0\r\n");99out.print("Connection: close\r\n");100out.print("\r\n");101out.flush();102103s.shutdownOutput();104s.close();105}106} catch (Exception e) {107}108}109}110111112public static void main(String args[]) throws Exception {113114/* start the http server */115HttpServer svr = new HttpServer();116(new Thread(svr)).start();117118int port = svr.port();119120/*121* Iterate through each test case and check that getResponseCode122* returns the expected result.123*/124int failures = 0;125Object tests[][] = getTests();126for (int i=0; i<tests.length; i++) {127128System.out.println("******************");129System.out.println("Test with response: >" + tests[i][0] + "<");130131URL url = new URL("http://localhost:" + port + "/" + i);132HttpURLConnection http = (HttpURLConnection)url.openConnection();133134try {135136// test getResponseCode137//138int expectedCode = Integer.parseInt((String)tests[i][1]);139int actualCode = http.getResponseCode();140if (actualCode != expectedCode) {141System.out.println("getResponseCode returned: " + actualCode +142", expected: " + expectedCode);143failures++;144continue;145}146147// test getResponseMessage148//149String expectedPhrase = (String)tests[i][2];150String actualPhrase = http.getResponseMessage();151if (actualPhrase == null && expectedPhrase == null) {152continue;153}154if (!actualPhrase.equals(expectedPhrase)) {155System.out.println("getResponseMessage returned: " +156actualPhrase + ", expected: " + expectedPhrase);157}158} catch (IOException e) {159e.printStackTrace();160failures++;161}162}163164/* shutdown http server */165svr.shutdown();166167if (failures > 0) {168throw new Exception(failures + " sub-test(s) failed.");169}170}171}172173174