blob: 77be52a336f9a6f44f11812c0ff09db44c6c8f2b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 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/**
25 * @test
26 * @bug 4473092
27 * @summary Method throws IOException when object should be returned
28 */
29import java.net.*;
30import java.io.*;
31
32public class HttpResponseCode implements Runnable {
33 ServerSocket ss;
34 /*
35 * Our "http" server
36 */
37 public void run() {
38 try {
39 Socket s = ss.accept();
40
41 BufferedReader in = new BufferedReader(
42 new InputStreamReader(s.getInputStream()) );
43 String req = in.readLine();
44
45 PrintStream out = new PrintStream(
46 new BufferedOutputStream(
47 s.getOutputStream() ));
48
49
50 /* send the header */
51 out.print("HTTP/1.1 403 Forbidden\r\n");
52 out.print("\r\n");
53 out.flush();
54
55 s.close();
56 ss.close();
57 } catch (Exception e) {
58 e.printStackTrace();
59 }
60 }
61
62 HttpResponseCode() throws Exception {
63 /* start the server */
64 ss = new ServerSocket(0);
65 (new Thread(this)).start();
66
67 /* establish http connection to server */
68 String url = "http://localhost:" +
69 Integer.toString(ss.getLocalPort()) +
70 "/missing.nothtml";
71 URLConnection uc = new URL(url).openConnection();
72 int respCode1 = ((HttpURLConnection)uc).getResponseCode();
73 ((HttpURLConnection)uc).disconnect();
74 int respCode2 = ((HttpURLConnection)uc).getResponseCode();
75 if (respCode1 != 403 || respCode2 != 403) {
76 throw new RuntimeException("Testing Http response code failed");
77 }
78 }
79
80 public static void main(String args[]) throws Exception {
81 new HttpResponseCode();
82 }
83}