### Question:
JwtAuthenticator extends LoginAuthenticator { String getJwtFromBearerAuthorization(HttpServletRequest req) { String authorizationHeader = req.getHeader(HttpHeader.AUTHORIZATION.asString()); if (authorizationHeader == null || !authorizationHeader.startsWith(BEARER)) { return null; } else { return authorizationHeader.substring(BEARER.length()).trim(); } } JwtAuthenticator(String authenticationProviderUrl, String cookieName); @Override String getAuthMethod(); @Override void prepareRequest(ServletRequest request); @Override Authentication validateRequest(ServletRequest request, ServletResponse response, boolean mandatory); @Override boolean secureResponse(ServletRequest request, ServletResponse response, boolean mandatory, Authentication.User validatedUser); static final String JWT_TOKEN_REQUEST_ATTRIBUTE; }### Answer:
@Test public void testParseTokenFromAuthHeader() { JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN); HttpServletRequest request = mock(HttpServletRequest.class); expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(JwtAuthenticator.BEARER + " " + EXPECTED_TOKEN); replay(request); String actualToken = authenticator.getJwtFromBearerAuthorization(request); verify(request); assertEquals(EXPECTED_TOKEN, actualToken); }
@Test public void testParseTokenFromAuthHeaderNoBearer() { JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN); HttpServletRequest request = mock(HttpServletRequest.class); expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(BASIC_SCHEME + " " + EXPECTED_TOKEN); replay(request); String actualToken = authenticator.getJwtFromBearerAuthorization(request); verify(request); assertNull(actualToken); }