HttpHeadersUtils { public static HttpHeaders setHeader(HttpHeaders headers, String key, String value) { ClassInfo classInfo = headers.getClassInfo(); FieldInfo fieldInfo = classInfo.getFieldInfo(key); Object headerValue; if (fieldInfo == null) { headerValue = value; } else { Class<?> elementyType = fieldInfo.getType(); if (List.class.isAssignableFrom(fieldInfo.getType())) { elementyType = "Age".equalsIgnoreCase(key) || "Content-Length".equalsIgnoreCase(key) ? Long.class : String.class; } headerValue = ImmutableList.of(fromString(elementyType, value)); } headers.set(key, headerValue); return headers; }  static String getUserAgentValue(Map<String, String> headersMap); static HttpHeaders setHeaders(HttpHeaders headers, Map<String, String> headersMap); static HttpHeaders setHeader(HttpHeaders headers, String key, String value);  }
@Test public void testSetHeader() { HttpHeaders headers = new HttpHeaders(); assertEquals(headers, HttpHeadersUtils.setHeader(headers, "Custom-Header", "CustomHeader")); HttpHeadersUtils.setHeader(headers, "accept", "Accept"); HttpHeadersUtils.setHeader(headers, "accept-encoding", "AcceptEncoding"); HttpHeadersUtils.setHeader(headers, "Authorization", "Authorization"); HttpHeadersUtils.setHeader(headers, "cache-Control", "CacheControl"); HttpHeadersUtils.setHeader(headers, "Content-Encoding", "ContentEncoding"); HttpHeadersUtils.setHeader(headers, "Content-Length", "123"); HttpHeadersUtils.setHeader(headers, "Content-MD5", "ContentMD5"); HttpHeadersUtils.setHeader(headers, "Content-Range", "ContentRange"); HttpHeadersUtils.setHeader(headers, "Content-Type", "ContentType"); HttpHeadersUtils.setHeader(headers, "Cookie", "Cookie"); HttpHeadersUtils.setHeader(headers, "Date", "Date"); HttpHeadersUtils.setHeader(headers, "ETag", "ETag"); HttpHeadersUtils.setHeader(headers, "Expires", "Expires"); HttpHeadersUtils.setHeader(headers, "If-Modified-Since", "IfModifiedSince"); HttpHeadersUtils.setHeader(headers, "If-Match", "IfMatch"); HttpHeadersUtils.setHeader(headers, "If-None-Match", "IfNoneMatch"); HttpHeadersUtils.setHeader(headers, "If-Unmodified-Since", "IfUnmodifiedSince"); HttpHeadersUtils.setHeader(headers, "If-Range", "IfRange"); HttpHeadersUtils.setHeader(headers, "Last-Modified", "LastModified"); HttpHeadersUtils.setHeader(headers, "Location", "Location"); HttpHeadersUtils.setHeader(headers, "Mime-Version", "MimeVersion"); HttpHeadersUtils.setHeader(headers, "Range", "Range"); HttpHeadersUtils.setHeader(headers, "Retry-After", "RetryAfter"); HttpHeadersUtils.setHeader(headers, "User-Agent", "UserAgent"); HttpHeadersUtils.setHeader(headers, "WWW-Authenticate", "Authenticate"); HttpHeadersUtils.setHeader(headers, "Age", "456"); assertEquals("CustomHeader", headers.get("Custom-Header")); assertEquals("CustomHeader", headers.get("custom-header")); assertEquals("Accept", headers.getAccept()); assertEquals("AcceptEncoding", headers.getAcceptEncoding()); assertEquals("Authorization", headers.getAuthorization()); assertEquals("CacheControl", headers.getCacheControl()); assertEquals("ContentEncoding", headers.getContentEncoding()); assertEquals(Long.valueOf(123L), headers.getContentLength()); assertEquals("ContentMD5", headers.getContentMD5()); assertEquals("ContentRange", headers.getContentRange()); assertEquals("ContentType", headers.getContentType()); assertEquals("Cookie", headers.getCookie()); assertEquals("Date", headers.getDate()); assertEquals("ETag", headers.getETag()); assertEquals("Expires", headers.getExpires()); assertEquals("IfModifiedSince", headers.getIfModifiedSince()); assertEquals("IfMatch", headers.getIfMatch()); assertEquals("IfNoneMatch", headers.getIfNoneMatch()); assertEquals("IfUnmodifiedSince", headers.getIfUnmodifiedSince()); assertEquals("IfRange", headers.getIfRange()); assertEquals("LastModified", headers.getLastModified()); assertEquals("Location", headers.getLocation()); assertEquals("MimeVersion", headers.getMimeVersion()); assertEquals("Range", headers.getRange()); assertEquals("RetryAfter", headers.getRetryAfter()); assertEquals("UserAgent", headers.getUserAgent()); assertEquals(ImmutableList.of("UserAgent"), headers.get("User-Agent")); assertEquals(ImmutableList.of("UserAgent"), headers.get("user-agent")); assertEquals("Authenticate", headers.getAuthenticate()); assertEquals(Long.valueOf(456L), headers.getAge()); }