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;
29
30 import static org.hamcrest.MatcherAssert.assertThat;
31
32 import org.apache.hc.core5.http.message.ParserCursor;
33 import org.apache.hc.core5.util.Tokenizer;
34 import org.hamcrest.CoreMatchers;
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 TestProtocolVersionParser {
43
44 private ProtocolVersionParser impl;
45
46 @BeforeEach
47 public void setup() {
48 impl = new ProtocolVersionParser();
49 }
50
51 public ProtocolVersion parseStr(final String protocol, final String s) throws ParseException {
52 return impl.parse(protocol, null, s, new ParserCursor(0, s.length()), null);
53 }
54
55 public ProtocolVersion parseStr(final String protocol, final String s, final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
56 return impl.parse(protocol, null, s, new ParserCursor(0, s.length()), delimiterPredicate);
57 }
58
59 public ProtocolVersion parseStr(final String protocol, final String s, final Tokenizer.Cursor cursor, final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
60 return impl.parse(protocol, null, s, cursor, delimiterPredicate);
61 }
62
63 public ProtocolVersion parseStr(final String s) throws ParseException {
64 return impl.parse(s, new ParserCursor(0, s.length()), null);
65 }
66
67 public ProtocolVersion parseStr(final String s, final Tokenizer.Delimiter delimiterPredicate) throws ParseException {
68 return impl.parse(s, new ParserCursor(0, s.length()), delimiterPredicate);
69 }
70
71 @Test
72 public void testParseVersion() throws Exception {
73 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 0), parseStr("PROTO", "1 "));
74 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 1), parseStr("PROTO", "1.1 "));
75 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 20), parseStr("PROTO", "1.020 "));
76 Assertions.assertEquals(new ProtocolVersion("PROTO", 22, 356), parseStr("PROTO", "22.356 "));
77
78 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 0), parseStr("PROTO", "1, ", Tokenizer.delimiters(',')));
79 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 1), parseStr("PROTO", "1.1; ", Tokenizer.delimiters(',', ';')));
80 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 200), parseStr("PROTO", "1.200 blah; ", Tokenizer.delimiters(',')));
81 }
82
83 @Test
84 public void testParseVersionWithCursor() throws Exception {
85 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(2, 13);
86 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 20), parseStr("PROTO", " 1.20,0000,00000", cursor, Tokenizer.delimiters(',')));
87 assertThat(cursor.getPos(), CoreMatchers.equalTo(6));
88 }
89
90 @Test
91 public void testParseProtocolVersion() throws Exception {
92 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 0), parseStr("PROTO/1 "));
93 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 1), parseStr("PROTO/1.1 "));
94 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 20), parseStr("PROTO/1.020 "));
95 Assertions.assertEquals(new ProtocolVersion("PROTO", 22, 356), parseStr("PROTO/22.356 "));
96
97 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 0), parseStr("PROTO/1, ", Tokenizer.delimiters(',')));
98 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 1), parseStr("PROTO/1.1; ", Tokenizer.delimiters(',', ';')));
99 Assertions.assertEquals(new ProtocolVersion("PROTO", 1, 200), parseStr("PROTO/1.200 blah; ", Tokenizer.delimiters(',')));
100 }
101
102 @Test
103 public void testParseFailures() throws Exception {
104 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO", "blah"));
105 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO", "1.blah"));
106 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO", "1A.0"));
107 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO", ""));
108 Assertions.assertThrows(ParseException.class, () -> parseStr(""));
109 Assertions.assertThrows(ParseException.class, () -> parseStr(" "));
110 Assertions.assertThrows(ParseException.class, () -> parseStr(" /1.0"));
111 Assertions.assertThrows(ParseException.class, () -> parseStr(" / "));
112 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO"));
113 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO/"));
114 Assertions.assertThrows(ParseException.class, () -> parseStr("PROTO/ 1.0"));
115 }
116
117 }