1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.message;
29
30 import org.apache.http.Header;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.HttpVersion;
33 import org.apache.http.RequestLine;
34 import org.apache.http.StatusLine;
35 import org.apache.http.util.CharArrayBuffer;
36 import org.junit.Assert;
37 import org.junit.Test;
38
39
40
41
42 public class TestBasicLineFormatter {
43
44 @Test
45 public void testHttpVersionFormatting() throws Exception {
46 final String s = BasicLineFormatter.formatProtocolVersion
47 (HttpVersion.HTTP_1_1, null);
48 Assert.assertEquals("HTTP/1.1", s);
49 }
50
51 @Test
52 public void testHttpVersionFormattingInvalidInput() throws Exception {
53 try {
54 BasicLineFormatter.formatProtocolVersion
55 (null, BasicLineFormatter.INSTANCE);
56 Assert.fail("IllegalArgumentException should habe been thrown");
57 } catch (final IllegalArgumentException ex) {
58
59 }
60 try {
61 BasicLineFormatter.INSTANCE.appendProtocolVersion
62 (new CharArrayBuffer(10), (HttpVersion) null);
63 Assert.fail("IllegalArgumentException should habe been thrown");
64 } catch (final IllegalArgumentException ex) {
65
66 }
67 }
68
69
70 @Test
71 public void testRLFormatting() throws Exception {
72 final RequestLine requestline = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
73 final String s = BasicLineFormatter.formatRequestLine(requestline, null);
74 Assert.assertEquals("GET /stuff HTTP/1.1", s);
75 }
76
77 @Test
78 public void testRLFormattingInvalidInput() throws Exception {
79 try {
80 BasicLineFormatter.formatRequestLine
81 (null, BasicLineFormatter.INSTANCE);
82 Assert.fail("IllegalArgumentException should habe been thrown");
83 } catch (final IllegalArgumentException ex) {
84
85 }
86 try {
87 BasicLineFormatter.INSTANCE.formatRequestLine
88 (new CharArrayBuffer(10), (RequestLine) null);
89 Assert.fail("IllegalArgumentException should habe been thrown");
90 } catch (final IllegalArgumentException ex) {
91
92 }
93 }
94
95
96
97 @Test
98 public void testSLFormatting() throws Exception {
99 StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
100 String s = BasicLineFormatter.formatStatusLine(statusline, null);
101 Assert.assertEquals("HTTP/1.1 200 OK", s);
102 statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
103 s = BasicLineFormatter.formatStatusLine(statusline, null);
104 Assert.assertEquals("HTTP/1.1 200 ", s);
105
106
107 }
108
109 @Test
110 public void testSLFormattingInvalidInput() throws Exception {
111 try {
112 BasicLineFormatter.formatStatusLine
113 (null, BasicLineFormatter.INSTANCE);
114 Assert.fail("IllegalArgumentException should habe been thrown");
115 } catch (final IllegalArgumentException ex) {
116
117 }
118 try {
119 BasicLineFormatter.INSTANCE.formatStatusLine
120 (new CharArrayBuffer(10), (StatusLine) null);
121 Assert.fail("IllegalArgumentException should habe been thrown");
122 } catch (final IllegalArgumentException ex) {
123
124 }
125 }
126
127
128 @Test
129 public void testHeaderFormatting() throws Exception {
130 final Header header1 = new BasicHeader("name", "value");
131 String s = BasicLineFormatter.formatHeader(header1, null);
132 Assert.assertEquals("name: value", s);
133 final Header header2 = new BasicHeader("name", null);
134 s = BasicLineFormatter.formatHeader(header2, null);
135 Assert.assertEquals("name: ", s);
136 }
137
138 @Test
139 public void testHeaderFormattingInvalidInput() throws Exception {
140 try {
141 BasicLineFormatter.formatHeader
142 (null, BasicLineFormatter.INSTANCE);
143 Assert.fail("IllegalArgumentException should habe been thrown");
144 } catch (final IllegalArgumentException ex) {
145
146 }
147 try {
148 BasicLineFormatter.INSTANCE.formatHeader
149 (new CharArrayBuffer(10), (Header) null);
150 Assert.fail("IllegalArgumentException should habe been thrown");
151 } catch (final IllegalArgumentException ex) {
152
153 }
154 }
155
156 @Test
157 public void testHeaderFormattingRequestSplitting() throws Exception {
158 final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops");
159 final String s = BasicLineFormatter.formatHeader(header, null);
160 Assert.assertFalse(s.contains("\n"));
161 Assert.assertEquals("Host: apache.org Oops: oops", s);
162 }
163 }