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.impl.io;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33
34 import org.apache.hc.core5.http.Header;
35 import org.apache.hc.core5.http.ProtocolException;
36 import org.apache.hc.core5.http.io.SessionInputBuffer;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43 public class TestMessageParser {
44
45 @Test
46 public void testBasicHeaderParsing() throws Exception {
47 final String s =
48 "header1: stuff\r\n" +
49 "header2: stuff \r\n" +
50 "header3: stuff\r\n" +
51 " and more stuff\r\n" +
52 "\t and even more stuff\r\n" +
53 " \r\n" +
54 "\r\n";
55 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
56 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
57 final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
58 Assertions.assertNotNull(headers);
59 Assertions.assertEquals(3, headers.length);
60 Assertions.assertEquals("header1", headers[0].getName());
61 Assertions.assertEquals("stuff", headers[0].getValue());
62 Assertions.assertEquals("header2", headers[1].getName());
63 Assertions.assertEquals("stuff", headers[1].getValue());
64 Assertions.assertEquals("header3", headers[2].getName());
65 Assertions.assertEquals("stuff and more stuff and even more stuff", headers[2].getValue());
66 }
67
68 @Test
69 public void testParsingHeader() throws Exception {
70 final String s = "header1: stuff; param1 = value1; param2 = \"value 2\" \r\n" +
71 "\r\n";
72 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
73 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
74 final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
75 Assertions.assertNotNull(headers);
76 Assertions.assertEquals(1, headers.length);
77 Assertions.assertEquals("header1: stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
78 }
79
80 @Test
81 public void testParsingInvalidHeaders() throws Exception {
82 final String s1 = " stuff\r\n" +
83 "header1: stuff\r\n" +
84 "\r\n";
85 final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s1.getBytes(StandardCharsets.US_ASCII));
86 final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
87 Assertions.assertThrows(ProtocolException.class, () ->
88 AbstractMessageParser.parseHeaders(inBuffer1, inputStream1, -1, -1, null));
89 final String s2 = " : stuff\r\n" +
90 "header1: stuff\r\n" +
91 "\r\n";
92 final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s2.getBytes(StandardCharsets.US_ASCII));
93 final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
94 Assertions.assertThrows(ProtocolException.class, () ->
95 AbstractMessageParser.parseHeaders(inBuffer2, inputStream2, -1, -1, null));
96 }
97
98 @Test
99 public void testParsingMalformedFirstHeader() throws Exception {
100 final String s =
101 " header1: stuff\r\n" +
102 "header2: stuff \r\n";
103 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
104 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
105 final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
106 Assertions.assertNotNull(headers);
107 Assertions.assertEquals(2, headers.length);
108 Assertions.assertEquals("header1", headers[0].getName());
109 Assertions.assertEquals("stuff", headers[0].getValue());
110 Assertions.assertEquals("header2", headers[1].getName());
111 Assertions.assertEquals("stuff", headers[1].getValue());
112 }
113
114 @Test
115 public void testEmptyDataStream() throws Exception {
116 final String s = "";
117 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
118 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
119 final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
120 Assertions.assertNotNull(headers);
121 Assertions.assertEquals(0, headers.length);
122 }
123
124 @Test
125 public void testMaxHeaderCount() throws Exception {
126 final String s =
127 "header1: stuff\r\n" +
128 "header2: stuff \r\n" +
129 "header3: stuff\r\n" +
130 "\r\n";
131 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
132 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
133 Assertions.assertThrows(IOException.class, () ->
134 AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, -1, null));
135 }
136
137 @Test
138 public void testMaxHeaderCountForFoldedHeader() throws Exception {
139 final String s =
140 "header1: stuff\r\n" +
141 " stuff \r\n" +
142 " stuff\r\n" +
143 "\r\n";
144 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
145 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
146 Assertions.assertThrows(IOException.class, () ->
147 AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, 15, null));
148 }
149
150 }
151