~ubuntu-branches/ubuntu/lucid/commons-httpclient/lucid

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/httpclient/TestTraceMethodLocal.java

  • Committer: Bazaar Package Importer
  • Author(s): Barry Hawkins
  • Date: 2005-11-25 13:12:23 UTC
  • Revision ID: james.westby@ubuntu.com-20051125131223-2g7eyo21pqgrohpo
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Header: $
 
3
 * $Revision: $
 
4
 * $Date: $
 
5
 * ====================================================================
 
6
 *
 
7
 *  Copyright 1999-2004 The Apache Software Foundation
 
8
 *
 
9
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
10
 *  you may not use this file except in compliance with the License.
 
11
 *  You may obtain a copy of the License at
 
12
 *
 
13
 *      http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 *  Unless required by applicable law or agreed to in writing, software
 
16
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
17
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
18
 *  See the License for the specific language governing permissions and
 
19
 *  limitations under the License.
 
20
 * ====================================================================
 
21
 *
 
22
 * This software consists of voluntary contributions made by many
 
23
 * individuals on behalf of the Apache Software Foundation.  For more
 
24
 * information on the Apache Software Foundation, please see
 
25
 * <http://www.apache.org/>.
 
26
 *
 
27
 * [Additional notices, if required by prior licensing conditions]
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.commons.httpclient;
 
32
 
 
33
import junit.framework.Test;
 
34
import junit.framework.TestSuite;
 
35
 
 
36
import org.apache.commons.httpclient.methods.TraceMethod;
 
37
 
 
38
/**
 
39
 * 
 
40
 * Simple tests of {@link TraceMethod}.
 
41
 * 
 
42
 * @author Sean C. Sullivan
 
43
 * @version $Id: TestGetMethodLocal.java,v 1.3 2002/02/04 15:26:43 dion Exp $
 
44
 * 
 
45
 */
 
46
public class TestTraceMethodLocal extends TestLocalHostBase {
 
47
 
 
48
 
 
49
    // ------------------------------------------------------------ Constructor
 
50
 
 
51
    public TestTraceMethodLocal(String testName) {
 
52
        super(testName);
 
53
    }
 
54
 
 
55
 
 
56
    // ------------------------------------------------------- TestCase Methods
 
57
 
 
58
 
 
59
    public static Test suite() {
 
60
        return new TestSuite(TestTraceMethodLocal.class);
 
61
    }
 
62
 
 
63
 
 
64
    // ------------------------------------------------------------------ Tests
 
65
 
 
66
 
 
67
 
 
68
    public void testExecute() {
 
69
        
 
70
        HttpClient client = createHttpClient();
 
71
 
 
72
        TraceMethod method = new TraceMethod("/");
 
73
 
 
74
        final String strTestHeaderName = "MyTestHeader";
 
75
        
 
76
        final String strTestHeaderValue = "This-is-a-test-value.";
 
77
        
 
78
        method.setRequestHeader(
 
79
                    strTestHeaderName, 
 
80
                    strTestHeaderValue);
 
81
        
 
82
        try {
 
83
            client.executeMethod(method);
 
84
 
 
85
            final int iResponseStatusCode = method.getStatusCode();
 
86
            assertEquals(200, iResponseStatusCode);
 
87
            
 
88
            Header[] requestHeaders = method.getRequestHeaders();
 
89
            assertTrue( requestHeaders.length > 0);
 
90
 
 
91
            Header[] responseHeaders = method.getResponseHeaders();
 
92
            assertNotNull(responseHeaders);
 
93
            
 
94
            //
 
95
            // note:  the reason that we convert the String's to lowercase is
 
96
            //        because some HTTP servers send a response body that contains 
 
97
            //        lower request headers
 
98
            //
 
99
            final String strResponseBody_lowercase = method.getResponseBodyAsString().toLowerCase();
 
100
            assertNotNull(strResponseBody_lowercase);
 
101
            assertTrue( strResponseBody_lowercase.length() > 0);
 
102
            
 
103
            assertTrue( strResponseBody_lowercase.indexOf(strTestHeaderName.toLowerCase()) != -1);
 
104
            assertTrue( strResponseBody_lowercase.indexOf(strTestHeaderValue.toLowerCase()) != -1);
 
105
            
 
106
        } catch (Throwable t) {
 
107
            t.printStackTrace();
 
108
            fail("Unable to execute method : " + t.toString());
 
109
        }
 
110
    }
 
111
    
 
112
    public void testRecycle() {
 
113
        HttpClient client = createHttpClient();
 
114
 
 
115
        TraceMethod method = new TraceMethod("/");
 
116
        
 
117
        try {
 
118
            client.executeMethod(method);
 
119
        } catch (Throwable t) {
 
120
            t.printStackTrace();
 
121
            fail("Unable to execute method : " + t.toString());
 
122
        }
 
123
 
 
124
        try {
 
125
            String data = method.getResponseBodyAsString();
 
126
            assertTrue("No data returned.",(data.length() > 0));
 
127
        } catch (Throwable t) {
 
128
            t.printStackTrace();
 
129
            fail("Unable to execute method : " + t.toString());
 
130
        }
 
131
 
 
132
        method.recycle();
 
133
        method.setPath("/");
 
134
 
 
135
        try {
 
136
            client.executeMethod(method);
 
137
        } catch (Throwable t) {
 
138
            t.printStackTrace();
 
139
            fail("Unable to execute method : " + t.toString());
 
140
        }
 
141
 
 
142
        try {
 
143
            String data = method.getResponseBodyAsString();
 
144
            assertTrue("No data returned.",(data.length() > 0));
 
145
        } catch (Throwable t) {
 
146
            t.printStackTrace();
 
147
            fail("Unable to execute method : " + t.toString());
 
148
        }
 
149
    }
 
150
 
 
151
    public static void main(String args[]) {
 
152
        String[] testCaseName = { TestTraceMethodLocal.class.getName() };
 
153
        junit.textui.TestRunner.main(testCaseName);
 
154
    }
 
155
    
 
156
 
 
157
}