KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestWebappMultiPostMethod


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMultiPostMethod.java,v 1.4.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2003-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.methods.MultipartPostMethod;
38 import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;
39 import org.apache.commons.httpclient.methods.multipart.FilePart;
40 import org.apache.commons.httpclient.methods.multipart.StringPart;
41
42 /**
43  * Webapp tests specific to the MultiPostMethod.
44  *
45  * @author <a HREF="oleg@ural.ru">Oleg Kalnichevski</a>
46  */

47 public class TestWebappMultiPostMethod extends TestWebappBase {
48
49     HttpClient httpClient;
50     final String JavaDoc paramsPath = "/" + getWebappContext() + "/params";
51     final String JavaDoc bodyPath = "/" + getWebappContext() + "/body";
52
53     public TestWebappMultiPostMethod(String JavaDoc testName) {
54         super(testName);
55     }
56
57     public static Test suite() {
58         TestSuite suite = new TestSuite(TestWebappMultiPostMethod.class);
59         return suite;
60     }
61
62     public static void main(String JavaDoc args[]) {
63         String JavaDoc[] testCaseName = { TestWebappMultiPostMethod.class.getName() };
64         junit.textui.TestRunner.main(testCaseName);
65     }
66
67     public void setUp() {
68         httpClient = createHttpClient();
69     }
70
71     // ------------------------------------------------------------------ Tests
72

73     /**
74      * Test that the body consisting of a string part can be posted.
75      */

76
77     public void testPostStringPart() throws Exception JavaDoc {
78         MultipartPostMethod method = new MultipartPostMethod(bodyPath);
79         method.addPart(new StringPart("param", "Hello", "ISO-8859-1"));
80
81         httpClient.executeMethod(method);
82
83         assertEquals(200,method.getStatusCode());
84         String JavaDoc body = method.getResponseBodyAsString();
85         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param\"") >= 0);
86         assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
87         assertTrue(body.indexOf("Content-Transfer-Encoding: 8bit") >= 0);
88         assertTrue(body.indexOf("Hello") >= 0);
89     }
90
91
92     /**
93      * Test that the body consisting of a file part can be posted.
94      */

95     public void testPostFilePart() throws Exception JavaDoc {
96         MultipartPostMethod method = new MultipartPostMethod(bodyPath);
97         byte[] content = "Hello".getBytes();
98         method.addPart(
99           new FilePart(
100             "param1",
101             new ByteArrayPartSource("filename.txt", content),
102             "text/plain",
103             "ISO-8859-1"));
104
105         httpClient.executeMethod(method);
106
107         assertEquals(200,method.getStatusCode());
108         String JavaDoc body = method.getResponseBodyAsString();
109         assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0);
110         assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0);
111         assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
112         assertTrue(body.indexOf("Hello") >= 0);
113     }
114 }
115
116
Popular Tags