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  package org.apache.hc.client5.http.impl.cache;
28  
29  
30  import org.apache.hc.core5.http.HttpVersion;
31  import org.hamcrest.MatcherAssert;
32  import org.hamcrest.Matchers;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  public class TestViaCacheGenerator {
38  
39      private ViaCacheGenerator impl;
40  
41      @BeforeEach
42      public void setUp() {
43          impl = new ViaCacheGenerator();
44      }
45  
46      @Test
47      public void testViaValueGeneration() {
48          Assertions.assertEquals("1.1 localhost (Apache-HttpClient/UNAVAILABLE (cache))",
49                  impl.generateViaHeader(null, HttpVersion.DEFAULT));
50          Assertions.assertEquals("2.0 localhost (Apache-HttpClient/UNAVAILABLE (cache))",
51                  impl.generateViaHeader(null, HttpVersion.HTTP_2));
52      }
53  
54      @Test
55      public void testViaValueLookup() {
56          MatcherAssert.assertThat(impl.lookup(HttpVersion.DEFAULT),
57                  Matchers.startsWith("1.1 localhost (Apache-HttpClient/"));
58          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_1_0),
59                  Matchers.startsWith("1.0 localhost (Apache-HttpClient/"));
60          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_1_1),
61                  Matchers.startsWith("1.1 localhost (Apache-HttpClient/"));
62          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_2),
63                  Matchers.startsWith("2.0 localhost (Apache-HttpClient/"));
64          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_2_0),
65                  Matchers.startsWith("2.0 localhost (Apache-HttpClient/"));
66          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_1_0),
67                  Matchers.startsWith("1.0 localhost (Apache-HttpClient/"));
68          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_1_1),
69                  Matchers.startsWith("1.1 localhost (Apache-HttpClient/"));
70          MatcherAssert.assertThat(impl.lookup(HttpVersion.HTTP_2_0),
71                  Matchers.startsWith("2.0 localhost (Apache-HttpClient/"));
72          Assertions.assertEquals(3, impl.internalCache.size());
73      }
74  
75  }