• 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 
26 import junit.framework.TestCase;
27 
28 import java.lang.reflect.Field;
29 import java.util.List;
30 
31 /**
32  * Tests for the HttpHeaders class.
33  *
34  * @author Kurt Alfred Kluever
35  */
36 public class HttpHeadersTest extends TestCase {
37 
testConstantNameMatchesString()38   public void testConstantNameMatchesString() throws Exception {
39     // Special case some of the weird HTTP Header names...
40     ImmutableBiMap<String, String> specialCases = ImmutableBiMap.of("ETAG", "ETag");
41     ImmutableSet<String> uppercaseAcronyms = ImmutableSet.of(
42         "ID", "DNT", "IP", "MD5", "P3P", "TE", "UID", "URL", "WWW", "XSS");
43     assertConstantNameMatchesString(HttpHeaders.class, specialCases, uppercaseAcronyms);
44   }
45 
46   // Visible for other tests to use
assertConstantNameMatchesString(Class<?> clazz, ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms)47   static void assertConstantNameMatchesString(Class<?> clazz,
48       ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms)
49       throws IllegalAccessException {
50     for (Field field : relevantFields(clazz)) {
51       assertEquals(upperToHttpHeaderName(field.getName(), specialCases, uppercaseAcronyms),
52           field.get(null));
53     }
54   }
55 
56   // Visible for other tests to use
relevantFields(Class<?> cls)57   static ImmutableSet<Field> relevantFields(Class<?> cls) {
58     ImmutableSet.Builder<Field> builder = ImmutableSet.builder();
59     for (Field field : cls.getDeclaredFields()) {
60       /*
61        * Coverage mode generates synthetic fields.  If we ever add private
62        * fields, they will cause similar problems, and we may want to switch
63        * this check to isAccessible().
64        */
65       if (!field.isSynthetic() && field.getType() == String.class) {
66         builder.add(field);
67       }
68     }
69     return builder.build();
70   }
71 
72   private static final Splitter SPLITTER = Splitter.on('_');
73   private static final Joiner JOINER = Joiner.on('-');
74 
upperToHttpHeaderName(String constantName, ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms)75   private static String upperToHttpHeaderName(String constantName,
76       ImmutableBiMap<String, String> specialCases, ImmutableSet<String> uppercaseAcronyms) {
77     if (specialCases.containsKey(constantName)) {
78       return specialCases.get(constantName);
79     }
80     List<String> parts = Lists.newArrayList();
81     for (String part : SPLITTER.split(constantName)) {
82       if (!uppercaseAcronyms.contains(part)) {
83         part = part.charAt(0) + Ascii.toLowerCase(part.substring(1));
84       }
85       parts.add(part);
86     }
87     return JOINER.join(parts);
88   }
89 }
90