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.hc.core5.http.message;
29
30 import org.apache.hc.core5.http.Header;
31 import org.apache.hc.core5.http.HttpStatus;
32 import org.apache.hc.core5.http.HttpVersion;
33 import org.apache.hc.core5.http.Method;
34 import org.apache.hc.core5.util.CharArrayBuffer;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38
39
40
41
42 public class TestBasicLineFormatter {
43
44 private BasicLineFormatter formatter;
45
46 @Before
47 public void setup() {
48 this.formatter = BasicLineFormatter.INSTANCE;
49 }
50
51 @Test
52 public void testHttpVersionFormatting() throws Exception {
53 final CharArrayBuffer buf = new CharArrayBuffer(64);
54 this.formatter.formatProtocolVersion(buf, HttpVersion.HTTP_1_1);
55 Assert.assertEquals("HTTP/1.1", buf.toString());
56 }
57
58 @Test
59 public void testRLFormatting() throws Exception {
60 final RequestLine requestline = new RequestLine(Method.GET.name(), "/stuff", HttpVersion.HTTP_1_1);
61 final CharArrayBuffer buf = new CharArrayBuffer(64);
62 this.formatter.formatRequestLine(buf, requestline);
63 Assert.assertEquals("GET /stuff HTTP/1.1", buf.toString());
64 }
65
66 @Test
67 public void testRLFormattingInvalidInput() throws Exception {
68 final CharArrayBuffer buf = new CharArrayBuffer(64);
69 final RequestLine requestline = new RequestLine(Method.GET.name(), "/stuff", HttpVersion.HTTP_1_1);
70 try {
71 this.formatter.formatRequestLine(null, requestline);
72 Assert.fail("NullPointerException should habe been thrown");
73 } catch (final NullPointerException ex) {
74
75 }
76 try {
77 this.formatter.formatRequestLine(buf, null);
78 Assert.fail("NullPointerException should habe been thrown");
79 } catch (final NullPointerException ex) {
80
81 }
82 }
83
84 @Test
85 public void testSLFormatting() throws Exception {
86 final CharArrayBuffer buf = new CharArrayBuffer(64);
87 final StatusLine statusline1 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
88 this.formatter.formatStatusLine(buf, statusline1);
89 Assert.assertEquals("HTTP/1.1 200 OK", buf.toString());
90
91 buf.clear();
92 final StatusLine statusline2 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
93 this.formatter.formatStatusLine(buf, statusline2);
94 Assert.assertEquals("HTTP/1.1 200 ", buf.toString());
95
96
97 }
98
99 @Test
100 public void testSLFormattingInvalidInput() throws Exception {
101 final CharArrayBuffer buf = new CharArrayBuffer(64);
102 final StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
103 try {
104 this.formatter.formatStatusLine(null, statusline);
105 Assert.fail("NullPointerException should habe been thrown");
106 } catch (final NullPointerException ex) {
107
108 }
109 try {
110 this.formatter.formatStatusLine(buf, null);
111 Assert.fail("NullPointerException should habe been thrown");
112 } catch (final NullPointerException ex) {
113
114 }
115 }
116
117 @Test
118 public void testHeaderFormatting() throws Exception {
119 final CharArrayBuffer buf = new CharArrayBuffer(64);
120 final Header header1 = new BasicHeader("name", "value");
121 this.formatter.formatHeader(buf, header1);
122 Assert.assertEquals("name: value", buf.toString());
123
124 buf.clear();
125 final Header header2 = new BasicHeader("name", null);
126 this.formatter.formatHeader(buf, header2);
127 Assert.assertEquals("name: ", buf.toString());
128 }
129
130 @Test
131 public void testHeaderFormattingInvalidInput() throws Exception {
132 final CharArrayBuffer buf = new CharArrayBuffer(64);
133 final Header header = new BasicHeader("name", "value");
134 try {
135 this.formatter.formatHeader(null, header);
136 Assert.fail("NullPointerException should habe been thrown");
137 } catch (final NullPointerException ex) {
138
139 }
140 try {
141 this.formatter.formatHeader(buf, null);
142 Assert.fail("NullPointerException should habe been thrown");
143 } catch (final NullPointerException ex) {
144
145 }
146 }
147
148 @Test
149 public void testHeaderFormattingRequestSplitting() throws Exception {
150 final CharArrayBuffer buf = new CharArrayBuffer(64);
151 final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops");
152 formatter.formatHeader(buf, header);
153 final String s = buf.toString();
154 Assert.assertFalse(s.contains("\n"));
155 Assert.assertEquals("Host: apache.org Oops: oops", s);
156 }
157 }