View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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   * Unit tests for {@link ProtocolVersionParser}.
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 }