001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.net;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.net.Socket;
024: import java.net.InetSocketAddress;
025: import java.net.Proxy;
026: import java.net.SocketAddress;
027: import java.net.URL;
028: import java.net.URLConnection;
029:
030: import tests.support.Support_Configuration;
031: import tests.support.resource.Support_Resources;
032: import junit.framework.TestCase;
033:
034: /*
035: * This test is designed for collecting all the testcases which needs a proxy
036: * server. They will be moved to corresponding test class when ProxyHandler of
037: * Jetty is ready in the future.
038: *
039: */
040:
041: public class ExcludedProxyTest extends TestCase {
042: /**
043: * @tests java.net.HttpURLConnection#usingProxy()
044: */
045: public void test_usingProxy() throws Exception {
046: try {
047: System.setProperty("http.proxyHost",
048: Support_Configuration.ProxyServerTestHost);
049:
050: URL u1 = new URL("http://"
051: + Support_Configuration.HomeAddress);
052: URLConnection conn1 = u1.openConnection();
053: conn1.getInputStream();
054:
055: boolean exception = false;
056: try {
057: System.setProperty("http.proxyPort", "81");
058: URL u3 = new URL("http://"
059: + Support_Configuration.InetTestAddress);
060: URLConnection conn3 = u3.openConnection();
061: conn3.getInputStream();
062: fail("Should throw IOException");
063: } catch (IOException e) {
064: // expected
065: }
066:
067: System.setProperty("http.proxyPort", "80");
068:
069: URL u2 = new URL("http://"
070: + Support_Configuration.ProxyServerTestHost
071: + "/cgi-bin/test.pl");
072: java.net.HttpURLConnection conn2 = (java.net.HttpURLConnection) u2
073: .openConnection();
074: conn2.setDoOutput(true);
075: conn2.setRequestMethod("POST");
076: OutputStream out2 = conn2.getOutputStream();
077: String posted2 = "this is a test";
078: out2.write(posted2.getBytes());
079: out2.close();
080: conn2.getResponseCode();
081: InputStream is2 = conn2.getInputStream();
082: String response2 = "";
083: byte[] b2 = new byte[1024];
084: int count2 = 0;
085: while ((count2 = is2.read(b2)) > 0)
086: response2 += new String(b2, 0, count2);
087: assertTrue("Response to POST method invalid", response2
088: .equals(posted2));
089:
090: String posted4 = "just a test";
091: URL u4 = new URL("http://"
092: + Support_Configuration.ProxyServerTestHost
093: + "/cgi-bin/test.pl");
094: java.net.HttpURLConnection conn4 = (java.net.HttpURLConnection) u4
095: .openConnection();
096: conn4.setDoOutput(true);
097: conn4.setRequestMethod("POST");
098: conn4.setRequestProperty("Content-length", String
099: .valueOf(posted4.length()));
100: OutputStream out = conn4.getOutputStream();
101: out.write(posted4.getBytes());
102: out.close();
103: conn4.getResponseCode();
104: InputStream is = conn4.getInputStream();
105: String response = "";
106: byte[] b4 = new byte[1024];
107: int count = 0;
108: while ((count = is.read(b4)) > 0)
109: response += new String(b4, 0, count);
110: assertTrue("Response to POST method invalid", response
111: .equals(posted4));
112: } finally {
113: System.setProperties(null);
114: }
115: }
116:
117: /**
118: * @tests java.net.SocketImpl#SocketImpl()
119: */
120: public void test_Constructor() {
121: try {
122: try {
123: System.setProperty("socksProxyHost",
124: Support_Configuration.SocksServerTestHost);
125: System
126: .setProperty(
127: "socksProxyPort",
128: String
129: .valueOf(Support_Configuration.SocksServerTestPort));
130: Socket s = new Socket(
131: Support_Configuration.HomeAddress, 80);
132: OutputStream os = s.getOutputStream();
133: os.write("GET / HTTP/1.0\r\n\r\n".getBytes());
134: s.getInputStream();
135: } catch (IOException e) {
136: fail("Could not open socket: "
137: + Support_Configuration.HomeAddress + " " + e);
138: }
139: boolean exception = false;
140: try {
141: System.setProperty("socksProxyHost",
142: Support_Configuration.SocksServerTestHost);
143: System
144: .setProperty(
145: "socksProxyPort",
146: String
147: .valueOf(Support_Configuration.SocksServerTestPort + 1));
148: Socket s = new Socket(
149: Support_Configuration.HomeAddress, 80);
150: OutputStream os = s.getOutputStream();
151: os.write("GET / HTTP/1.0\r\n\r\n".getBytes());
152: s.getInputStream();
153: } catch (IOException e) {
154: exception = true;
155: }
156: assertTrue("Exception should have been thrown", exception);
157: } finally {
158: System.setProperties(null);
159: }
160: }
161:
162: /**
163: * @tests java.net.URL#openConnection(Proxy)
164: */
165: public void test_openConnectionLjava_net_Proxy() throws IOException {
166: SocketAddress addr1 = new InetSocketAddress(
167: Support_Configuration.ProxyServerTestHost, 808);
168: SocketAddress addr2 = new InetSocketAddress(
169: Support_Configuration.ProxyServerTestHost, 1080);
170: Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
171: Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr2);
172: Proxy proxyList[] = { proxy1, proxy2 };
173: for (int i = 0; i < proxyList.length; ++i) {
174: String posted = "just a test";
175: URL u = new URL("http://"
176: + Support_Configuration.ProxyServerTestHost
177: + "/cgi-bin/test.pl");
178: java.net.HttpURLConnection conn = (java.net.HttpURLConnection) u
179: .openConnection(proxyList[i]);
180: conn.setDoOutput(true);
181: conn.setRequestMethod("POST");
182: conn.setRequestProperty("Content-length", String
183: .valueOf(posted.length()));
184: OutputStream out = conn.getOutputStream();
185: out.write(posted.getBytes());
186: out.close();
187: conn.getResponseCode();
188: InputStream is = conn.getInputStream();
189: String response = "";
190: byte[] b = new byte[1024];
191: int count = 0;
192: while ((count = is.read(b)) > 0) {
193: response += new String(b, 0, count);
194: }
195: assertTrue("Response to POST method invalid", response
196: .equals(posted));
197: }
198:
199: URL httpUrl = new URL("http://abc.com");
200: URL jarUrl = new URL("jar:"
201: + Support_Resources
202: .getResourceURL("/JUC/lf.jar!/plus.bmp"));
203: URL ftpUrl = new URL("ftp://"
204: + Support_Configuration.FTPTestAddress + "/nettest.txt");
205: URL fileUrl = new URL("file://abc");
206: URL[] urlList = { httpUrl, jarUrl, ftpUrl, fileUrl };
207: for (int i = 0; i < urlList.length; ++i) {
208: try {
209: urlList[i].openConnection(null);
210: } catch (IllegalArgumentException iae) {
211: // expected
212: }
213: }
214: // should not throw exception
215: fileUrl.openConnection(Proxy.NO_PROXY);
216: }
217: }
|