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  import static org.junit.jupiter.api.Assertions.assertFalse;
30  
31  import java.time.Instant;
32  
33  import org.apache.hc.client5.http.utils.DateUtils;
34  import org.apache.hc.core5.http.HttpResponse;
35  import org.apache.hc.core5.http.HttpStatus;
36  import org.apache.hc.core5.http.support.BasicResponseBuilder;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  public class TestResponseCacheConformance {
41  
42      private ResponseCacheConformance impl;
43  
44      @BeforeEach
45      public void setUp() {
46          impl = ResponseCacheConformance.INSTANCE;
47      }
48  
49      private void shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
50              final String entityHeader, final String entityHeaderValue) throws Exception {
51          final HttpResponse response = BasicResponseBuilder.create(HttpStatus.SC_NOT_MODIFIED)
52                  .addHeader("Date", DateUtils.formatStandardDate(Instant.now()))
53                  .addHeader("Etag", "\"etag\"")
54                  .addHeader(entityHeader, entityHeaderValue)
55                  .build();
56          impl.process(response, null, null);
57          assertFalse(response.containsHeader(entityHeader));
58      }
59  
60      @Test
61      public void shouldStripContentEncodingFromOrigin304ResponseToStrongValidation() throws Exception {
62          shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
63                  "Content-Encoding", "gzip");
64      }
65  
66      @Test
67      public void shouldStripContentLanguageFromOrigin304ResponseToStrongValidation() throws Exception {
68          shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
69                  "Content-Language", "en");
70      }
71  
72      @Test
73      public void shouldStripContentLengthFromOrigin304ResponseToStrongValidation() throws Exception {
74          shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
75                  "Content-Length", "128");
76      }
77  
78      @Test
79      public void shouldStripContentMD5FromOrigin304ResponseToStrongValidation() throws Exception {
80          shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
81                  "Content-MD5", "Q2hlY2sgSW50ZWdyaXR5IQ==");
82      }
83  
84      @Test
85      public void shouldStripContentTypeFromOrigin304ResponseToStrongValidation() throws Exception {
86          shouldStripEntityHeaderFromOrigin304ResponseToStrongValidation(
87                  "Content-Type", "text/html;charset=utf-8");
88      }
89  
90  }