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.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 public class TestBasicLineFormatter {
43
44 private BasicLineFormatter formatter;
45
46 @BeforeEach
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 Assertions.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 Assertions.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 Assertions.assertThrows(NullPointerException.class, () ->
71 formatter.formatRequestLine(null, requestline));
72 Assertions.assertThrows(NullPointerException.class, () ->
73 formatter.formatRequestLine(buf, null));
74 }
75
76 @Test
77 public void testSLFormatting() throws Exception {
78 final CharArrayBuffer buf = new CharArrayBuffer(64);
79 final StatusLine statusline1 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
80 this.formatter.formatStatusLine(buf, statusline1);
81 Assertions.assertEquals("HTTP/1.1 200 OK", buf.toString());
82
83 buf.clear();
84 final StatusLine statusline2 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
85 this.formatter.formatStatusLine(buf, statusline2);
86 Assertions.assertEquals("HTTP/1.1 200 ", buf.toString());
87
88
89 }
90
91 @Test
92 public void testSLFormattingInvalidInput() throws Exception {
93 final CharArrayBuffer buf = new CharArrayBuffer(64);
94 final StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
95 Assertions.assertThrows(NullPointerException.class, () ->
96 formatter.formatStatusLine(null, statusline));
97 Assertions.assertThrows(NullPointerException.class, () ->
98 formatter.formatStatusLine(buf, null));
99 }
100
101 @Test
102 public void testHeaderFormatting() throws Exception {
103 final CharArrayBuffer buf = new CharArrayBuffer(64);
104 final Header header1 = new BasicHeader("name", "value");
105 this.formatter.formatHeader(buf, header1);
106 Assertions.assertEquals("name: value", buf.toString());
107
108 buf.clear();
109 final Header header2 = new BasicHeader("name", null);
110 this.formatter.formatHeader(buf, header2);
111 Assertions.assertEquals("name: ", buf.toString());
112 }
113
114 @Test
115 public void testHeaderFormattingInvalidInput() throws Exception {
116 final CharArrayBuffer buf = new CharArrayBuffer(64);
117 final Header header = new BasicHeader("name", "value");
118 Assertions.assertThrows(NullPointerException.class, () ->
119 formatter.formatHeader(null, header));
120 Assertions.assertThrows(NullPointerException.class, () ->
121 formatter.formatHeader(buf, null));
122 }
123
124 @Test
125 public void testHeaderFormattingRequestSplitting() throws Exception {
126 final CharArrayBuffer buf = new CharArrayBuffer(64);
127 final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops");
128 formatter.formatHeader(buf, header);
129 final String s = buf.toString();
130 Assertions.assertFalse(s.contains("\n"));
131 Assertions.assertEquals("Host: apache.org Oops: oops", s);
132 }
133 }