~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/test/java/org/apache/http/message/TestBasicLineFormatter.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/message/TestBasicLineFormatter.java $
 
3
 * $Revision: 744517 $
 
4
 * $Date: 2009-02-14 17:39:33 +0100 (Sat, 14 Feb 2009) $
 
5
 * ====================================================================
 
6
 * Licensed to the Apache Software Foundation (ASF) under one
 
7
 * or more contributor license agreements.  See the NOTICE file
 
8
 * distributed with this work for additional information
 
9
 * regarding copyright ownership.  The ASF licenses this file
 
10
 * to you under the Apache License, Version 2.0 (the
 
11
 * "License"); you may not use this file except in compliance
 
12
 * with the License.  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,
 
17
 * software distributed under the License is distributed on an
 
18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
19
 * KIND, either express or implied.  See the License for the
 
20
 * specific language governing permissions and limitations
 
21
 * under the License.
 
22
 * ====================================================================
 
23
 *
 
24
 * This software consists of voluntary contributions made by many
 
25
 * individuals on behalf of the Apache Software Foundation.  For more
 
26
 * information on the Apache Software Foundation, please see
 
27
 * <http://www.apache.org/>.
 
28
 *
 
29
 */
 
30
 
 
31
package org.apache.http.message;
 
32
 
 
33
import junit.framework.Test;
 
34
import junit.framework.TestCase;
 
35
import junit.framework.TestSuite;
 
36
 
 
37
import org.apache.http.Header;
 
38
import org.apache.http.HttpStatus;
 
39
import org.apache.http.HttpVersion;
 
40
import org.apache.http.RequestLine;
 
41
import org.apache.http.StatusLine;
 
42
import org.apache.http.util.CharArrayBuffer;
 
43
 
 
44
/**
 
45
 * Tests for {@link BasicLineFormatter}.
 
46
 *
 
47
 *
 
48
 * @version $Revision: 744517 $
 
49
 */
 
50
public class TestBasicLineFormatter extends TestCase {
 
51
 
 
52
    // ------------------------------------------------------------ Constructor
 
53
    public TestBasicLineFormatter(String testName) {
 
54
        super(testName);
 
55
    }
 
56
 
 
57
    // ------------------------------------------------------------------- Main
 
58
    public static void main(String args[]) {
 
59
        String[] testCaseName = { TestBasicLineFormatter.class.getName() };
 
60
        junit.textui.TestRunner.main(testCaseName);
 
61
    }
 
62
 
 
63
    // ------------------------------------------------------- TestCase Methods
 
64
 
 
65
    public static Test suite() {
 
66
        return new TestSuite(TestBasicLineFormatter.class);
 
67
    }
 
68
 
 
69
 
 
70
 
 
71
    public void testHttpVersionFormatting() throws Exception {
 
72
        String s = BasicLineFormatter.formatProtocolVersion
 
73
            (HttpVersion.HTTP_1_1, null);
 
74
        assertEquals("HTTP/1.1", s);
 
75
    }
 
76
    
 
77
    public void testHttpVersionFormattingInvalidInput() throws Exception {
 
78
        try {
 
79
            BasicLineFormatter.formatProtocolVersion
 
80
                (null, BasicLineFormatter.DEFAULT);
 
81
            fail("IllegalArgumentException should habe been thrown");
 
82
        } catch (IllegalArgumentException ex) {
 
83
            // expected
 
84
        }
 
85
        try {
 
86
            BasicLineFormatter.DEFAULT.appendProtocolVersion
 
87
                (new CharArrayBuffer(10), (HttpVersion) null);
 
88
            fail("IllegalArgumentException should habe been thrown");
 
89
        } catch (IllegalArgumentException ex) {
 
90
            // expected
 
91
        }
 
92
    }
 
93
 
 
94
 
 
95
    public void testRLFormatting() throws Exception {
 
96
        RequestLine requestline = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
 
97
        String s = BasicLineFormatter.formatRequestLine(requestline, null);
 
98
        assertEquals("GET /stuff HTTP/1.1", s);
 
99
    }
 
100
    
 
101
    public void testRLFormattingInvalidInput() throws Exception {
 
102
        try {
 
103
            BasicLineFormatter.formatRequestLine
 
104
                (null, BasicLineFormatter.DEFAULT);
 
105
            fail("IllegalArgumentException should habe been thrown");
 
106
        } catch (IllegalArgumentException ex) {
 
107
            // expected
 
108
        }
 
109
        try {
 
110
            BasicLineFormatter.DEFAULT.formatRequestLine
 
111
                (new CharArrayBuffer(10), (RequestLine) null);
 
112
            fail("IllegalArgumentException should habe been thrown");
 
113
        } catch (IllegalArgumentException ex) {
 
114
            // expected
 
115
        }
 
116
    }
 
117
 
 
118
 
 
119
    
 
120
    public void testSLFormatting() throws Exception {
 
121
        StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
 
122
        String s = BasicLineFormatter.formatStatusLine(statusline, null);
 
123
        assertEquals("HTTP/1.1 200 OK", s);
 
124
        statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
 
125
        s = BasicLineFormatter.formatStatusLine(statusline, null);
 
126
        assertEquals("HTTP/1.1 200 ", s);
 
127
        // see "testSLParseSuccess" in TestBasicLineParser:
 
128
        // trailing space is correct
 
129
    }
 
130
    
 
131
    public void testSLFormattingInvalidInput() throws Exception {
 
132
        try {
 
133
            BasicLineFormatter.formatStatusLine
 
134
                (null, BasicLineFormatter.DEFAULT);
 
135
            fail("IllegalArgumentException should habe been thrown");
 
136
        } catch (IllegalArgumentException ex) {
 
137
            // expected
 
138
        }
 
139
        try {
 
140
            BasicLineFormatter.DEFAULT.formatStatusLine
 
141
                (new CharArrayBuffer(10), (StatusLine) null);
 
142
            fail("IllegalArgumentException should habe been thrown");
 
143
        } catch (IllegalArgumentException ex) {
 
144
            // expected
 
145
        }
 
146
    }
 
147
 
 
148
 
 
149
    public void testHeaderFormatting() throws Exception {
 
150
        Header header1 = new BasicHeader("name", "value");
 
151
        String s = BasicLineFormatter.formatHeader(header1, null);
 
152
        assertEquals("name: value", s);
 
153
        Header header2 = new BasicHeader("name", null);
 
154
        s = BasicLineFormatter.formatHeader(header2, null); 
 
155
        assertEquals("name: ", s);
 
156
    }
 
157
    
 
158
    public void testHeaderFormattingInvalidInput() throws Exception {
 
159
        try {
 
160
            BasicLineFormatter.formatHeader
 
161
                (null, BasicLineFormatter.DEFAULT);
 
162
            fail("IllegalArgumentException should habe been thrown");
 
163
        } catch (IllegalArgumentException ex) {
 
164
            // expected
 
165
        }
 
166
        try {
 
167
            BasicLineFormatter.DEFAULT.formatHeader
 
168
                (new CharArrayBuffer(10), (Header) null);
 
169
            fail("IllegalArgumentException should habe been thrown");
 
170
        } catch (IllegalArgumentException ex) {
 
171
            // expected
 
172
        }
 
173
    }
 
174
     
 
175
}