Skip to content
Permalink
Browse files

[java-client][okhttp-gson] fixes for interceptors in ApiClient.java (#…

…3502)

ApiClient for Okhttp3 must not copy the interceptors when setting the HttpClient.
Enforce invariant that the HttpClient must never be null.
  • Loading branch information...
lostiniceland authored and jmini committed Aug 1, 2019
1 parent bcc3a9e commit 0a7527b03e5ea9d56ea39862217294fd7db81f97
@@ -10,6 +10,7 @@

import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;


public class ApiClientTest {
@@ -329,24 +330,18 @@ public void testSanitizeFilename() {
assertEquals("sun.gif", apiClient.sanitizeFilename(".\\sun.gif"));
}


@Test
public void testInterceptorCleanupWithNewClient() {
public void testNewHttpClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());

OkHttpClient newClient = new OkHttpClient();
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
apiClient.setHttpClient(oldClient.newBuilder().build());
assertThat(apiClient.getHttpClient(), is(not(oldClient)));
}

@Test
public void testInterceptorCleanupWithSameClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());
apiClient.setHttpClient(oldClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
/**
* Tests the invariant that the HttpClient for the ApiClient must never be null
*/
@Test(expected = NullPointerException.class)
public void testNullHttpClient() {
apiClient.setHttpClient(null);
}
}
@@ -187,24 +187,14 @@ public class ApiClient {
}

/**
* Set HTTP client
* Set HTTP client, which must never be null.
*
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
* @throws NullPointerException when newHttpClient is null
*/
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
this.httpClient = Objects.requireNonNull(newHttpClient, "HttpClient must not be null!");
return this;
}

@@ -170,24 +170,14 @@ public OkHttpClient getHttpClient() {
}

/**
* Set HTTP client
* Set HTTP client, which must never be null.
*
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
* @throws NullPointerException when newHttpClient is null
*/
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
this.httpClient = Objects.requireNonNull(newHttpClient, "HttpClient must not be null!");
return this;
}

@@ -170,24 +170,14 @@ public OkHttpClient getHttpClient() {
}

/**
* Set HTTP client
* Set HTTP client, which must never be null.
*
* @param newHttpClient An instance of OkHttpClient
* @return Api Client
* @throws NullPointerException when newHttpClient is null
*/
public ApiClient setHttpClient(OkHttpClient newHttpClient) {
if(!httpClient.equals(newHttpClient)) {
OkHttpClient.Builder builder = newHttpClient.newBuilder();
Iterator<Interceptor> networkInterceptorIterator = httpClient.networkInterceptors().iterator();
while(networkInterceptorIterator.hasNext()) {
builder.addNetworkInterceptor(networkInterceptorIterator.next());
}
Iterator<Interceptor> interceptorIterator = httpClient.interceptors().iterator();
while(interceptorIterator.hasNext()) {
builder.addInterceptor(interceptorIterator.next());
}
this.httpClient = builder.build();
}
this.httpClient = Objects.requireNonNull(newHttpClient, "HttpClient must not be null!");
return this;
}

@@ -10,6 +10,7 @@

import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;


public class ApiClientTest {
@@ -329,24 +330,18 @@ public void testSanitizeFilename() {
assertEquals("sun.gif", apiClient.sanitizeFilename(".\\sun.gif"));
}


@Test
public void testInterceptorCleanupWithNewClient() {
public void testNewHttpClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());

OkHttpClient newClient = new OkHttpClient();
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
apiClient.setHttpClient(newClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
apiClient.setHttpClient(oldClient.newBuilder().build());
assertThat(apiClient.getHttpClient(), is(not(oldClient)));
}

@Test
public void testInterceptorCleanupWithSameClient() {
OkHttpClient oldClient = apiClient.getHttpClient();
assertEquals(1, oldClient.networkInterceptors().size());
apiClient.setHttpClient(oldClient);
assertEquals(1, apiClient.getHttpClient().networkInterceptors().size());
/**
* Tests the invariant that the HttpClient for the ApiClient must never be null
*/
@Test(expected = NullPointerException.class)
public void testNullHttpClient() {
apiClient.setHttpClient(null);
}
}

0 comments on commit 0a7527b

Please sign in to comment.
You can’t perform that action at this time.