1616
1717package com .google .common .net ;
1818
19+ import static com .google .common .truth .Truth .assertThat ;
20+
1921import com .google .common .base .Ascii ;
2022import com .google .common .base .Joiner ;
2123import com .google .common .base .Splitter ;
2224import com .google .common .collect .ImmutableBiMap ;
25+ import com .google .common .collect .ImmutableList ;
2326import com .google .common .collect .ImmutableSet ;
2427import com .google .common .collect .Lists ;
2528import java .lang .reflect .Field ;
@@ -55,25 +58,25 @@ public void testConstantNameMatchesString() throws Exception {
5558 ImmutableSet .of (
5659 "CH" , "ID" , "DNT" , "DNS" , "DPR" , "ECT" , "GPC" , "HTTP2" , "IP" , "MD5" , "P3P" , "RTT" , "TE" ,
5760 "UA" , "UID" , "URL" , "WWW" , "XSS" );
58- assertConstantNameMatchesString (HttpHeaders .class , specialCases , uppercaseAcronyms );
59- }
6061
61- // Visible for other tests to use
62- static void assertConstantNameMatchesString (
63- Class <?> clazz ,
64- ImmutableBiMap <String , String > specialCases ,
65- ImmutableSet <String > uppercaseAcronyms )
66- throws IllegalAccessException {
67- for (Field field : relevantFields (clazz )) {
62+ for (Field field : httpHeadersFields ()) {
6863 assertEquals (
6964 upperToHttpHeaderName (field .getName (), specialCases , uppercaseAcronyms ), field .get (null ));
7065 }
7166 }
7267
73- // Visible for other tests to use
74- static ImmutableSet <Field > relevantFields (Class <?> cls ) {
68+ // Tests that there are no duplicate HTTP header names
69+ public void testNoDuplicateFields () throws Exception {
70+ ImmutableList .Builder <String > httpHeaders = ImmutableList .builder ();
71+ for (Field field : httpHeadersFields ()) {
72+ httpHeaders .add ((String ) field .get (null ));
73+ }
74+ assertThat (httpHeaders .build ()).containsNoDuplicates ();
75+ }
76+
77+ private static ImmutableSet <Field > httpHeadersFields () {
7578 ImmutableSet .Builder <Field > builder = ImmutableSet .builder ();
76- for (Field field : cls .getDeclaredFields ()) {
79+ for (Field field : HttpHeaders . class .getDeclaredFields ()) {
7780 /*
7881 * Coverage mode generates synthetic fields. If we ever add private
7982 * fields, they will cause similar problems, and we may want to switch
@@ -86,9 +89,6 @@ static ImmutableSet<Field> relevantFields(Class<?> cls) {
8689 return builder .build ();
8790 }
8891
89- private static final Splitter SPLITTER = Splitter .on ('_' );
90- private static final Joiner JOINER = Joiner .on ('-' );
91-
9292 private static String upperToHttpHeaderName (
9393 String constantName ,
9494 ImmutableBiMap <String , String > specialCases ,
@@ -97,12 +97,12 @@ private static String upperToHttpHeaderName(
9797 return specialCases .get (constantName );
9898 }
9999 List <String > parts = Lists .newArrayList ();
100- for (String part : SPLITTER .split (constantName )) {
100+ for (String part : Splitter . on ( '_' ) .split (constantName )) {
101101 if (!uppercaseAcronyms .contains (part )) {
102102 part = part .charAt (0 ) + Ascii .toLowerCase (part .substring (1 ));
103103 }
104104 parts .add (part );
105105 }
106- return JOINER .join (parts );
106+ return Joiner . on ( '-' ) .join (parts );
107107 }
108108}
0 commit comments