summaryrefslogtreecommitdiff
path: root/test/java.net/URLTest.java
blob: 82d0929f471f5945a2a2270b7496d52040b9c2b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Test URL's */

import java.net.*;
import java.io.*;

public class URLTest
{

public static void
main(String argv[])
{
  System.out.println("Starting URL tests");

  /* Simple URL test */

  System.out.println("Test 1: Simple URL test");

  try
    { 
      URL url = new URL("http", "www.fsf.org", 80, "/");

      if (!url.getProtocol().equals("http") ||
          !url.getHost().equals("www.fsf.org") ||
          url.getPort() != 80 ||
          !url.getFile().equals("/"))
      System.out.println("FAILED: Simple URL test");

      System.out.println("URL is: " + url.toString());

      URLConnection uc = url.openConnection();

      if (uc instanceof HttpURLConnection)
         System.out.println("Got the expected connection type");

      HttpURLConnection hc = (HttpURLConnection)uc;

      hc.connect();

      System.out.println("Dumping response headers");
      for (int i = 0; ; i++)
        {
          String key = hc.getHeaderFieldKey(i);
          if (key == null)
            break;

          System.out.println(key + ": " + hc.getHeaderField(i));
        }

      System.out.println("Dumping contents");
      BufferedReader br = new BufferedReader(new 
                              InputStreamReader(hc.getInputStream()));

      for (String str = br.readLine(); str != null; str = br.readLine())
        System.out.println(str);
       
      hc.disconnect();

      System.out.println("Content Type: " + hc.getContentType());
      System.out.println("Content Encoding: " + hc.getContentEncoding());
      System.out.println("Content Length: " + hc.getContentLength());
      System.out.println("Date: " + hc.getDate());
      System.out.println("Expiration: " + hc.getExpiration());
      System.out.println("Last Modified: " + hc.getLastModified());

      System.out.println("PASSED: Simple URL test");
    }
  catch(IOException e)
    {
      System.out.println("FAILED: Simple URL test: " + e);
    }

  // Parsing test
  System.out.println("Test 2: URL parsing test");
  try
    {
      URL url = new URL("http://www.urbanophile.com/arenn/trans/trans.html#mis");
      if (!url.toString().equals(
          "http://www.urbanophile.com/arenn/trans/trans.html#mis"))
        System.out.println("FAILED: Parse URL test: " + url.toString());
      else {
        System.out.println("Parsed ok: " + url.toString());
        url = new URL("http://www.foo.com:8080/#");
        if (!url.toString().equals("http://www.foo.com:8080/#"))
          System.out.println("FAILED: Parse URL test: " + url.toString());
        else {
          System.out.println("Parsed ok: " + url.toString());
          url = new URL("http://www.bar.com/test:file/");
          if (!url.toString().equals("http://www.bar.com/test:file/"))
            System.out.println("FAILED: Parse URL test: " + url.toString());
          else {
            System.out.println("Parsed ok: " + url.toString());
            url = new URL("http://www.gnu.org");
            if (!url.toString().equals("http://www.gnu.org/"))
              System.out.println("FAILED: Parse URL test: " + url.toString());
            else {
              System.out.println("Parsed ok: " + url.toString());
              url = new URL("HTTP://www.fsf.org/");
              if (!url.toString().equals("http://www.fsf.org/"))
                System.out.println("FAILED: Parse URL test: " + url.toString());
              else { 
                System.out.println("Parsed ok: " + url.toString());
                System.out.println("PASSED: URL parse test");
              }
            }
          }
        }
      }
    }
  catch (IOException e)
    {
      System.out.println("FAILED: URL parsing test: " + e);
    }

  // getContent test
  System.out.println("Test 3: getContent test");
  try
    {
      URL url = new URL("http://localhost/~arenn/services.txt");

      Object obj = url.getContent();
      System.out.println("Object type is: " + obj.getClass().getName());

      if (obj instanceof InputStream)
        {
          System.out.println("Got InputStream, so dumping contents");
          BufferedReader br = new BufferedReader(new 
                                  InputStreamReader((InputStream)obj));

          for (String str = br.readLine(); str != null; str = br.readLine())
             System.out.println(str);

          br.close();
        }
      else
        {
          System.out.println("FAILED: Object is not an InputStream");
        }

      System.out.println("PASSED: getContent test");
    }
  catch (IOException e)
    {
      System.out.println("FAILED: getContent test: " + e);
    }

  System.out.println("URL test complete");
}

}