Skip to content

Commit

Permalink
refactor: receive flag on the loggingEnabled method. add getter
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Nov 25, 2016
1 parent 9439675 commit aa98761
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ private AuthenticationAPIClient(Auth0 auth0, RequestFactory factory, OkHttpClien
* Log every Request and Response made by this client.
* You shouldn't enable logging in release builds as it may leak sensitive information.
*/
public void enableLogging() {
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
public void setLoggingEnabled(boolean enabled) {
logInterceptor.setLevel(enabled ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
}

/**
* Getter for the current client logger enabled state.
*/
public boolean isLoggingEnabled() {
return logInterceptor.getLevel() == HttpLoggingInterceptor.Level.BODY;
}

public String getClientId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ private Uri buildAuthorizeUri() {
private PKCE createPKCE(String redirectUri) {
if (pkce == null) {
final AuthenticationAPIClient client = new AuthenticationAPIClient(account);
if (loggingEnabled) {
client.enableLogging();
}
client.setLoggingEnabled(loggingEnabled);
return new PKCE(client, redirectUri);
} else {
return pkce;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,37 @@ public void shouldEnableHttpLogging() throws Exception {

ArgumentCaptor<Interceptor> interceptorCaptor = ArgumentCaptor.forClass(Interceptor.class);
AuthenticationAPIClient client = new AuthenticationAPIClient(account, factory, okClient);
client.enableLogging();
client.setLoggingEnabled(true);

verify(okClient).interceptors();
verify(list).add(interceptorCaptor.capture());

assertThat(interceptorCaptor.getValue(), is(notNullValue()));
assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(), is(HttpLoggingInterceptor.Level.BODY));
assertThat(client.isLoggingEnabled(), is(true));
}

@SuppressWarnings("unchecked")
@Test
public void shouldDisableHttpLogging() throws Exception {
Auth0 account = mock(Auth0.class);
RequestFactory factory = mock(RequestFactory.class);
OkHttpClient okClient = mock(OkHttpClient.class);
List list = mock(List.class);
when(okClient.interceptors()).thenReturn(list);

ArgumentCaptor<Interceptor> interceptorCaptor = ArgumentCaptor.forClass(Interceptor.class);
AuthenticationAPIClient client = new AuthenticationAPIClient(account, factory, okClient);
client.setLoggingEnabled(false);

verify(okClient).interceptors();
verify(list).add(interceptorCaptor.capture());

assertThat(interceptorCaptor.getValue(), is(notNullValue()));
assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(), is(HttpLoggingInterceptor.Level.NONE));
assertThat(client.isLoggingEnabled(), is(false));
}

@SuppressWarnings("unchecked")
Expand All @@ -185,6 +208,7 @@ public void shouldHaveHttpLoggingDisabledByDefault() throws Exception {
assertThat(interceptorCaptor.getValue(), is(notNullValue()));
assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(), is(HttpLoggingInterceptor.Level.NONE));
assertThat(client.isLoggingEnabled(), is(false));
}

@Test
Expand Down

0 comments on commit aa98761

Please sign in to comment.