@Test public void stopRetryAfterFailedAttempt() throws Exception { when(accountAuthenticator.getAccessToken()).thenAnswer(invocation -> invalidAccessToken); when(accountAuthenticator.getNewAccessToken(invalidAccessToken)) .thenAnswer(invocation -> validAccessToken); Response secondResponse = response.newBuilder().priorResponse(response).build(); Request request = requestRetryAuthenticator.authenticate(null, secondResponse); assertNull(request); }
@Nullable @Override public Request authenticate(@NonNull Route route, @NonNull Response response) throws IOException { if (response.priorResponse() != null) { return null; } final String invalidAccessToken = parseHeaderAccessToken(response); final String token; if (invalidAccessToken.isEmpty()) { token = authenticator.getAccessToken(); } else { token = authenticator.getNewAccessToken(invalidAccessToken); } final String authorization = Headers.AUTH_BEARER + token; return response.request() .newBuilder() .removeHeader(Headers.AUTHORIZATION) .addHeader(Headers.AUTHORIZATION, authorization) .build(); }
RequestRetryAuthenticator implements Authenticator { @Nullable @Override public Request authenticate(@NonNull Route route, @NonNull Response response) throws IOException { if (response.priorResponse() != null) { return null; } final String invalidAccessToken = parseHeaderAccessToken(response); final String token; if (invalidAccessToken.isEmpty()) { token = authenticator.getAccessToken(); } else { token = authenticator.getNewAccessToken(invalidAccessToken); } final String authorization = Headers.AUTH_BEARER + token; return response.request() .newBuilder() .removeHeader(Headers.AUTHORIZATION) .addHeader(Headers.AUTHORIZATION, authorization) .build(); } }
RequestRetryAuthenticator implements Authenticator { @Nullable @Override public Request authenticate(@NonNull Route route, @NonNull Response response) throws IOException { if (response.priorResponse() != null) { return null; } final String invalidAccessToken = parseHeaderAccessToken(response); final String token; if (invalidAccessToken.isEmpty()) { token = authenticator.getAccessToken(); } else { token = authenticator.getNewAccessToken(invalidAccessToken); } final String authorization = Headers.AUTH_BEARER + token; return response.request() .newBuilder() .removeHeader(Headers.AUTHORIZATION) .addHeader(Headers.AUTHORIZATION, authorization) .build(); } @Inject  RequestRetryAuthenticator(AccountAuthenticator authenticator); }
RequestRetryAuthenticator implements Authenticator { @Nullable @Override public Request authenticate(@NonNull Route route, @NonNull Response response) throws IOException { if (response.priorResponse() != null) { return null; } final String invalidAccessToken = parseHeaderAccessToken(response); final String token; if (invalidAccessToken.isEmpty()) { token = authenticator.getAccessToken(); } else { token = authenticator.getNewAccessToken(invalidAccessToken); } final String authorization = Headers.AUTH_BEARER + token; return response.request() .newBuilder() .removeHeader(Headers.AUTHORIZATION) .addHeader(Headers.AUTHORIZATION, authorization) .build(); } @Inject  RequestRetryAuthenticator(AccountAuthenticator authenticator); @Nullable @Override Request authenticate(@NonNull Route route, @NonNull Response response); }
RequestRetryAuthenticator implements Authenticator { @Nullable @Override public Request authenticate(@NonNull Route route, @NonNull Response response) throws IOException { if (response.priorResponse() != null) { return null; } final String invalidAccessToken = parseHeaderAccessToken(response); final String token; if (invalidAccessToken.isEmpty()) { token = authenticator.getAccessToken(); } else { token = authenticator.getNewAccessToken(invalidAccessToken); } final String authorization = Headers.AUTH_BEARER + token; return response.request() .newBuilder() .removeHeader(Headers.AUTHORIZATION) .addHeader(Headers.AUTHORIZATION, authorization) .build(); } @Inject  RequestRetryAuthenticator(AccountAuthenticator authenticator); @Nullable @Override Request authenticate(@NonNull Route route, @NonNull Response response);  }