102 103 static void read (InputStream is) throws IOException { 104 int c; 105 System.out.println ("reading"); 106 while ((c=is.read()) != -1) { 107 System.out.write (c); 108 } 109 System.out.println (""); 110 System.out.println ("finished reading"); 111 } 112 113 static void client (String u) throws Exception { 114 URL url = new URL (u); 115 System.out.println ("client opening connection to: " + u); 116 URLConnection urlc = url.openConnection (); 117 InputStream is = urlc.getInputStream (); 118 read (is); 119 is.close(); 120 } 121 122 static HttpServer server; 123 124 public static void main (String[] args) throws Exception { 125 firstTime = args[0].equals ("first"); 126 MyAuthenticator auth = new MyAuthenticator (); 127 Authenticator.setDefault (auth); 128 CacheImpl cache; 129 try { 130 if (firstTime) { 131 server = new HttpServer (new B4933582(), 1, 10, 0); 132 cache = new CacheImpl (server.getLocalPort()); 133 } else { 134 cache = new CacheImpl (); 135 server = new HttpServer(new B4933582(), 1, 10, cache.getPort()); 136 } 137 AuthCacheValue.setAuthCache (cache); 138 System.out.println ("Server: listening on port: " + server.getLocalPort()); 139 client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html"); 140 } catch (Exception e) { 141 if (server != null) { 142 server.terminate(); 143 } 144 throw e; 145 } 146 int f = auth.getCount(); 147 if (firstTime && f != 1) { 148 except ("Authenticator was called "+f+" times. Should be 1"); 149 } 150 if (!firstTime && f != 0) { 151 except ("Authenticator was called "+f+" times. Should be 0"); 152 } 153 server.terminate(); 154 } 155 | 102 103 static void read (InputStream is) throws IOException { 104 int c; 105 System.out.println ("reading"); 106 while ((c=is.read()) != -1) { 107 System.out.write (c); 108 } 109 System.out.println (""); 110 System.out.println ("finished reading"); 111 } 112 113 static void client (String u) throws Exception { 114 URL url = new URL (u); 115 System.out.println ("client opening connection to: " + u); 116 URLConnection urlc = url.openConnection (); 117 InputStream is = urlc.getInputStream (); 118 read (is); 119 is.close(); 120 } 121 122 static TestHttpServer server; 123 124 public static void main (String[] args) throws Exception { 125 firstTime = args[0].equals ("first"); 126 MyAuthenticator auth = new MyAuthenticator (); 127 Authenticator.setDefault (auth); 128 CacheImpl cache; 129 try { 130 if (firstTime) { 131 server = new TestHttpServer (new B4933582(), 1, 10, 0); 132 cache = new CacheImpl (server.getLocalPort()); 133 } else { 134 cache = new CacheImpl (); 135 server = new TestHttpServer(new B4933582(), 1, 10, cache.getPort()); 136 } 137 AuthCacheValue.setAuthCache (cache); 138 System.out.println ("Server: listening on port: " + server.getLocalPort()); 139 client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html"); 140 } catch (Exception e) { 141 if (server != null) { 142 server.terminate(); 143 } 144 throw e; 145 } 146 int f = auth.getCount(); 147 if (firstTime && f != 1) { 148 except ("Authenticator was called "+f+" times. Should be 1"); 149 } 150 if (!firstTime && f != 0) { 151 except ("Authenticator was called "+f+" times. Should be 0"); 152 } 153 server.terminate(); 154 } 155 |