001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpMethodFundamentals.java,v 1.6 2004/11/06 23:47:58 olegk Exp $
003: * $Revision: 481225 $
004: * $Date: 2006-12-01 12:26:28 +0100 (Fri, 01 Dec 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: */
028:
029: package org.apache.commons.httpclient;
030:
031: import java.io.IOException;
032: import java.io.InputStreamReader;
033: import java.io.Reader;
034:
035: import org.apache.commons.httpclient.methods.GetMethod;
036: import org.apache.commons.httpclient.server.HttpService;
037: import org.apache.commons.httpclient.server.SimpleRequest;
038: import org.apache.commons.httpclient.server.SimpleResponse;
039:
040: import junit.framework.Test;
041: import junit.framework.TestSuite;
042:
043: /**
044: * Tests basic method functionality.
045: *
046: * @author Remy Maucherat
047: * @author Rodney Waldhoff
048: * @author Oleg Kalnichevski
049: *
050: * @version $Id: TestHttpMethodFundamentals.java 481225 2006-12-01 11:26:28Z oglueck $
051: */
052: public class TestHttpMethodFundamentals extends HttpClientTestBase {
053:
054: public TestHttpMethodFundamentals(final String testName)
055: throws IOException {
056: super (testName);
057: }
058:
059: public static Test suite() {
060: TestSuite suite = new TestSuite(
061: TestHttpMethodFundamentals.class);
062: ProxyTestDecorator.addTests(suite);
063: return suite;
064: }
065:
066: public static void main(String args[]) {
067: String[] testCaseName = { TestHttpMethodFundamentals.class
068: .getName() };
069: junit.textui.TestRunner.main(testCaseName);
070: }
071:
072: class ManyAService implements HttpService {
073:
074: public ManyAService() {
075: super ();
076: }
077:
078: public boolean process(final SimpleRequest request,
079: final SimpleResponse response) throws IOException {
080: HttpVersion httpversion = request.getRequestLine()
081: .getHttpVersion();
082: response.setStatusLine(httpversion, HttpStatus.SC_OK);
083: response
084: .addHeader(new Header("Content-Type", "text/plain"));
085: response.addHeader(new Header("Connection", "close"));
086: StringBuffer buffer = new StringBuffer(1024);
087: for (int i = 0; i < 1024; i++) {
088: buffer.append('A');
089: }
090: response.setBodyString(buffer.toString());
091: return true;
092: }
093: }
094:
095: class SimpleChunkedService implements HttpService {
096:
097: public SimpleChunkedService() {
098: super ();
099: }
100:
101: public boolean process(final SimpleRequest request,
102: final SimpleResponse response) throws IOException {
103: HttpVersion httpversion = request.getRequestLine()
104: .getHttpVersion();
105: response.setStatusLine(httpversion, HttpStatus.SC_OK);
106: response
107: .addHeader(new Header("Content-Type", "text/plain"));
108: response.addHeader(new Header("Content-Length", "garbage"));
109: response.addHeader(new Header("Transfer-Encoding",
110: "chunked"));
111: response.addHeader(new Header("Connection", "close"));
112: response.setBodyString("1234567890123");
113: return true;
114: }
115: }
116:
117: class EmptyResponseService implements HttpService {
118:
119: public EmptyResponseService() {
120: super ();
121: }
122:
123: public boolean process(final SimpleRequest request,
124: final SimpleResponse response) throws IOException {
125: HttpVersion httpversion = request.getRequestLine()
126: .getHttpVersion();
127: response.setStatusLine(httpversion, HttpStatus.SC_OK);
128: response
129: .addHeader(new Header("Content-Type", "text/plain"));
130: response.addHeader(new Header("Transfer-Encoding",
131: "chunked"));
132: response.addHeader(new Header("Connection", "close"));
133: return true;
134: }
135: }
136:
137: public void testHttpMethodBasePaths() throws Exception {
138: HttpMethod simple = new FakeHttpMethod();
139: String[] paths = { "/some/absolute/path",
140: "../some/relative/path", "/",
141: "/some/path/with?query=string" };
142:
143: for (int i = 0; i < paths.length; i++) {
144: simple.setPath(paths[i]);
145: assertEquals(paths[i], simple.getPath());
146: }
147: }
148:
149: public void testHttpMethodBaseDefaultPath() throws Exception {
150: HttpMethod simple = new FakeHttpMethod();
151: assertEquals("/", simple.getPath());
152:
153: simple.setPath("");
154: assertEquals("/", simple.getPath());
155:
156: simple.setPath(null);
157: assertEquals("/", simple.getPath());
158: }
159:
160: public void testHttpMethodBasePathConstructor() throws Exception {
161: HttpMethod simple = new FakeHttpMethod();
162: assertEquals("/", simple.getPath());
163:
164: simple = new FakeHttpMethod("");
165: assertEquals("/", simple.getPath());
166:
167: simple = new FakeHttpMethod("/some/path/");
168: assertEquals("/some/path/", simple.getPath());
169: }
170:
171: /**
172: * Tests response with a Trasfer-Encoding and Content-Length
173: */
174: public void testHttpMethodBaseTEandCL() throws Exception {
175: this .server.setHttpService(new SimpleChunkedService());
176:
177: GetMethod httpget = new GetMethod("/test/");
178: try {
179: this .client.executeMethod(httpget);
180: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
181: assertEquals("1234567890123", httpget
182: .getResponseBodyAsString());
183: assertTrue(this .client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
184: HttpConnection conn = this .client
185: .getHttpConnectionManager().getConnection(
186: this .client.getHostConfiguration());
187: assertNotNull(conn);
188: conn.assertNotOpen();
189: } finally {
190: httpget.releaseConnection();
191: }
192: }
193:
194: public void testConnectionAutoClose() throws Exception {
195: this .server.setHttpService(new ManyAService());
196:
197: GetMethod httpget = new GetMethod("/test/");
198: try {
199: this .client.executeMethod(httpget);
200: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
201: Reader response = new InputStreamReader(httpget
202: .getResponseBodyAsStream());
203: int c;
204: while ((c = response.read()) != -1) {
205: assertEquals((int) 'A', c);
206: }
207: assertTrue(this .client.getHttpConnectionManager() instanceof SimpleHttpConnectionManager);
208: HttpConnection conn = this .client
209: .getHttpConnectionManager().getConnection(
210: this .client.getHostConfiguration());
211: assertNotNull(conn);
212: conn.assertNotOpen();
213: } finally {
214: httpget.releaseConnection();
215: }
216: }
217:
218: public void testSetGetQueryString1() {
219: HttpMethod method = new GetMethod();
220: String qs1 = "name1=value1&name2=value2";
221: method.setQueryString(qs1);
222: assertEquals(qs1, method.getQueryString());
223: }
224:
225: public void testQueryURIEncoding() {
226: HttpMethod method = new GetMethod(
227: "http://server/servlet?foo=bar&baz=schmoo");
228: assertEquals("foo=bar&baz=schmoo", method.getQueryString());
229: }
230:
231: public void testSetGetQueryString2() {
232: HttpMethod method = new GetMethod();
233: NameValuePair[] q1 = new NameValuePair[] {
234: new NameValuePair("name1", "value1"),
235: new NameValuePair("name2", "value2") };
236: method.setQueryString(q1);
237: String qs1 = "name1=value1&name2=value2";
238: assertEquals(qs1, method.getQueryString());
239: }
240:
241: /**
242: * Make sure that its OK to call releaseConnection if the connection has not been.
243: */
244: public void testReleaseConnection() {
245: HttpMethod method = new GetMethod("http://bogus.url/path/");
246: method.releaseConnection();
247: }
248:
249: /**
250: * Tests empty body response
251: */
252: public void testEmptyBodyAsString() throws Exception {
253: this .server.setHttpService(new EmptyResponseService());
254:
255: GetMethod httpget = new GetMethod("/test/");
256: try {
257: this .client.executeMethod(httpget);
258: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
259: String response = httpget.getResponseBodyAsString();
260: assertNull(response);
261:
262: this .client.executeMethod(httpget);
263: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
264: response = httpget.getResponseBodyAsString(1);
265: assertNull(response);
266: } finally {
267: httpget.releaseConnection();
268: }
269: }
270:
271: public void testEmptyBodyAsByteArray() throws Exception {
272: this .server.setHttpService(new EmptyResponseService());
273:
274: GetMethod httpget = new GetMethod("/test/");
275: try {
276: this .client.executeMethod(httpget);
277: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
278: byte[] response = httpget.getResponseBody();
279: assertNull(response);
280: } finally {
281: httpget.releaseConnection();
282: }
283: }
284:
285: public void testLongBodyAsString() throws Exception {
286: this .server.setHttpService(new SimpleChunkedService());
287:
288: GetMethod httpget = new GetMethod("/test/");
289: try {
290: this .client.executeMethod(httpget);
291: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
292: try {
293: httpget.getResponseBodyAsString(5); // too small
294: } catch (HttpContentTooLargeException e) {
295: /* expected */
296: assertEquals(5, e.getMaxLength());
297: }
298:
299: this .client.executeMethod(httpget);
300: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
301: String response = httpget.getResponseBodyAsString(13); // exact size
302: assertEquals("1234567890123", response);
303:
304: this .client.executeMethod(httpget);
305: assertEquals(HttpStatus.SC_OK, httpget.getStatusCode());
306: response = httpget.getResponseBodyAsString(128); // plenty
307: assertEquals("1234567890123", response);
308: } finally {
309: httpget.releaseConnection();
310: }
311: }
312:
313: public void testUrlGetMethodWithPathQuery() {
314: GetMethod method = new GetMethod(
315: "http://www.fubar.com/path1/path2?query=string");
316: try {
317: assertEquals("Get URL",
318: "http://www.fubar.com/path1/path2?query=string",
319: method.getURI().toString());
320: } catch (URIException e) {
321: fail("trouble getting URI: " + e);
322: }
323: assertEquals("Get Path", "/path1/path2", method.getPath());
324: assertEquals("Get query string", "query=string", method
325: .getQueryString());
326:
327: }
328:
329: public void testUrlGetMethodWithPath() {
330: GetMethod method = new GetMethod(
331: "http://www.fubar.com/path1/path2");
332: try {
333: assertEquals("Get URL", "http://www.fubar.com/path1/path2",
334: method.getURI().toString());
335: } catch (URIException e) {
336: fail("trouble getting URI: " + e);
337: }
338: assertEquals("Get Path", "/path1/path2", method.getPath());
339: assertEquals("Get query string", null, method.getQueryString());
340: }
341:
342: public void testUrlGetMethod() {
343: GetMethod method = new GetMethod("http://www.fubar.com/");
344: try {
345: assertEquals("Get URL", "http://www.fubar.com/", method
346: .getURI().toString());
347: } catch (URIException e) {
348: fail("trouble getting URI: " + e);
349: }
350: assertEquals("Get Path", "/", method.getPath());
351: assertEquals("Get query string", null, method.getQueryString());
352:
353: }
354:
355: public void testUrlGetMethodWithInvalidProtocol() {
356: try {
357: GetMethod method = new GetMethod("crap://www.fubar.com/");
358: fail("The use of invalid protocol must have resulted in an IllegalStateException");
359: } catch (IllegalStateException expected) {
360: }
361: }
362: }
|