• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.net;
18 
19 import com.google.common.base.Ascii;
20 import com.google.common.base.Joiner;
21 import com.google.common.base.Splitter;
22 import com.google.common.collect.ImmutableBiMap;
23 import com.google.common.collect.ImmutableSet;
24 import com.google.common.collect.Lists;
25 import java.lang.reflect.Field;
26 import java.util.List;
27 import junit.framework.TestCase;
28 
29 /**
30  * Tests for the HttpHeaders class.
31  *
32  * @author Kurt Alfred Kluever
33  */
34 public class HttpHeadersTest extends TestCase {
35 
testConstantNameMatchesString()36   public void testConstantNameMatchesString() throws Exception {
37     // Special case some of the weird HTTP Header names...
38     ImmutableBiMap<String, String> specialCases =
39         ImmutableBiMap.of(
40             "ETAG",
41             "ETag",
42             "SOURCE_MAP",
43             "SourceMap",
44             "X_WEBKIT_CSP",
45             "X-WebKit-CSP",
46             "X_WEBKIT_CSP_REPORT_ONLY",
47             "X-WebKit-CSP-Report-Only");
48     ImmutableSet<String> uppercaseAcronyms =
49         ImmutableSet.of(
50             "ID", "DNT", "DNS", "HTTP2", "IP", "MD5", "P3P", "TE", "UID", "URL", "WWW", "XSS");
51     assertConstantNameMatchesString(HttpHeaders.class, specialCases, uppercaseAcronyms);
52   }
53 
54   // Visible for other tests to use
assertConstantNameMatchesString( Class<?> clazz, ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms)55   static void assertConstantNameMatchesString(
56       Class<?> clazz,
57       ImmutableBiMap<String, String> specialCases,
58       ImmutableSet<String> uppercaseAcronyms)
59       throws IllegalAccessException {
60     for (Field field : relevantFields(clazz)) {
61       assertEquals(
62           upperToHttpHeaderName(field.getName(), specialCases, uppercaseAcronyms), field.get(null));
63     }
64   }
65 
66   // Visible for other tests to use
relevantFields(Class<?> cls)67   static ImmutableSet<Field> relevantFields(Class<?> cls) {
68     ImmutableSet.Builder<Field> builder = ImmutableSet.builder();
69     for (Field field : cls.getDeclaredFields()) {
70       /*
71        * Coverage mode generates synthetic fields.  If we ever add private
72        * fields, they will cause similar problems, and we may want to switch
73        * this check to isAccessible().
74        */
75       if (!field.isSynthetic() && field.getType() == String.class) {
76         builder.add(field);
77       }
78     }
79     return builder.build();
80   }
81 
82   private static final Splitter SPLITTER = Splitter.on('_');
83   private static final Joiner JOINER = Joiner.on('-');
84 
upperToHttpHeaderName( String constantName, ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms)85   private static String upperToHttpHeaderName(
86       String constantName,
87       ImmutableBiMap<String, String> specialCases,
88       ImmutableSet<String> uppercaseAcronyms) {
89     if (specialCases.containsKey(constantName)) {
90       return specialCases.get(constantName);
91     }
92     List<String> parts = Lists.newArrayList();
93     for (String part : SPLITTER.split(constantName)) {
94       if (!uppercaseAcronyms.contains(part)) {
95         part = part.charAt(0) + Ascii.toLowerCase(part.substring(1));
96       }
97       parts.add(part);
98     }
99     return JOINER.join(parts);
100   }
101 }
102