OAuthAuthorityUtils { public static final String extractHeaderToken(HttpServletRequest request) { Enumeration<String> headers = request.getHeaders(OAuthAuthorityConsts.AUTH_HEADER); while (headers.hasMoreElements()) { String value = headers.nextElement(); if ((value.toLowerCase().startsWith(OAuthAuthorityConsts.BEARER_TYPE))) { String authHeaderValue = value.substring(OAuthAuthorityConsts.BEARER_TYPE.length()).trim(); int commaIndex = authHeaderValue.indexOf(','); if (commaIndex > 0) { authHeaderValue = authHeaderValue.substring(0, commaIndex); } return authHeaderValue; } } return null; } private  OAuthAuthorityUtils(); static final String getProperty(String key, String def); static final Set<String> csvToSet(String csv, String delimiter); static final String extractHeaderToken(HttpServletRequest request);  }
@Test public void testExtractHeaderToken() { String tokenString = null; Enumeration<String> headers = null; HttpServletRequest request = Mockito.mock(HttpServletRequest.class); headers = Collections.enumeration(Arrays.asList("Bearer dummy_access_token_1")); Mockito.when(request.getHeaders("Authorization")).thenReturn(headers); tokenString = OAuthAuthorityUtils.extractHeaderToken(request); assertEquals(tokenString, "dummy_access_token_1"); headers = Collections.enumeration(Arrays.asList("bearer dummy_access_token_2")); Mockito.when(request.getHeaders("Authorization")).thenReturn(headers); tokenString = OAuthAuthorityUtils.extractHeaderToken(request); assertEquals(tokenString, "dummy_access_token_2"); headers = Collections.enumeration(Arrays.asList("Bearer dummy_access_token_3,dummy_access_token_4")); Mockito.when(request.getHeaders("Authorization")).thenReturn(headers); tokenString = OAuthAuthorityUtils.extractHeaderToken(request); assertEquals(tokenString, "dummy_access_token_3"); Mockito.when(request.getHeaders("Authorization")).thenReturn(Collections.emptyEnumeration()); tokenString = OAuthAuthorityUtils.extractHeaderToken(request); assertEquals(tokenString, null); headers = Collections.enumeration(Arrays.asList("Basic encoded_password")); Mockito.when(request.getHeaders("Authorization")).thenReturn(headers); tokenString = OAuthAuthorityUtils.extractHeaderToken(request); assertEquals(tokenString, null); }