test/java/net/URL/PerConnectionProxy.java

Print this page




   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4920526
  26  * @summary Needs per connection proxy support for URLs
  27  * @library ../../../sun/net/www/httptest/
  28  * @build ClosedChannelList HttpServer HttpTransaction HttpCallback
  29  * @compile PerConnectionProxy.java
  30  * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
  31  */
  32 
  33 import java.net.*;
  34 import java.io.*;
  35 import sun.net.www.*;
  36 
  37 public class PerConnectionProxy implements HttpCallback {
  38     static HttpServer server;
  39 
  40     public void request (HttpTransaction req) {
  41         req.setResponseEntityBody ("Hello .");
  42         try {
  43             req.sendResponse (200, "Ok");
  44             req.orderlyClose();
  45         } catch (IOException e) {
  46         }
  47     }
  48 
  49     public static void main(String[] args) {
  50         try {
  51             server = new HttpServer (new PerConnectionProxy(), 1, 10, 0);
  52             ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
  53             // start proxy server
  54             new Thread(pserver).start();
  55 
  56             URL url = new URL("http://localhost:"+server.getLocalPort());
  57 
  58             // for non existing proxy expect an IOException
  59             try {
  60                 InetSocketAddress isa = InetSocketAddress.createUnresolved("inexistent", 8080);
  61                 Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);
  62                 HttpURLConnection urlc = (HttpURLConnection)url.openConnection (proxy);
  63                 InputStream is = urlc.getInputStream ();
  64                 is.close();
  65                 throw new RuntimeException("non existing per connection proxy should lead to IOException");
  66             } catch (IOException ioex) {
  67                 // expected
  68             }
  69 
  70             // for NO_PROXY, expect direct connection
  71             try {




   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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4920526
  26  * @summary Needs per connection proxy support for URLs
  27  * @library ../../../sun/net/www/httptest/
  28  * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
  29  * @compile PerConnectionProxy.java
  30  * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 PerConnectionProxy
  31  */
  32 
  33 import java.net.*;
  34 import java.io.*;
  35 import sun.net.www.*;
  36 
  37 public class PerConnectionProxy implements HttpCallback {
  38     static TestHttpServer server;
  39 
  40     public void request (HttpTransaction req) {
  41         req.setResponseEntityBody ("Hello .");
  42         try {
  43             req.sendResponse (200, "Ok");
  44             req.orderlyClose();
  45         } catch (IOException e) {
  46         }
  47     }
  48 
  49     public static void main(String[] args) {
  50         try {
  51             server = new TestHttpServer (new PerConnectionProxy(), 1, 10, 0);
  52             ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
  53             // start proxy server
  54             new Thread(pserver).start();
  55 
  56             URL url = new URL("http://localhost:"+server.getLocalPort());
  57 
  58             // for non existing proxy expect an IOException
  59             try {
  60                 InetSocketAddress isa = InetSocketAddress.createUnresolved("inexistent", 8080);
  61                 Proxy proxy = new Proxy(Proxy.Type.HTTP, isa);
  62                 HttpURLConnection urlc = (HttpURLConnection)url.openConnection (proxy);
  63                 InputStream is = urlc.getInputStream ();
  64                 is.close();
  65                 throw new RuntimeException("non existing per connection proxy should lead to IOException");
  66             } catch (IOException ioex) {
  67                 // expected
  68             }
  69 
  70             // for NO_PROXY, expect direct connection
  71             try {