Skip to content

Commit 2f12c85

Browse files
authored
Revert change to return type of ApacheHttpTransport.newDefaultHttpClient() (#615)
* move apache classes back into google-http-client * revert unrelated metadata * revert unrelated metadata * ignore differences caused by move * rollback return type change * Revert "rollback return type change" This reverts commit bb32ceb. * Reapply return type change This reverts commit 358d94d. * revert return type change
1 parent 2e2b70b commit 2f12c85

File tree

4 files changed

+351
-145
lines changed

4 files changed

+351
-145
lines changed

clirr-ignored-differences.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,4 @@
1111
<differenceType>8001</differenceType>
1212
<className>com/google/api/client/http/apache/**</className>
1313
</difference>
14-
<difference>
15-
<differenceType>7006</differenceType>
16-
<className>com/google/api/client/http/apache/**</className>
17-
<method>org.apache.http.impl.client.DefaultHttpClient newDefaultHttpClient()</method>
18-
<to>org.apache.http.client.HttpClient</to>
19-
</difference>
2014
</differences>

google-http-client-apache/src/test/java/com/google/api/client/http/apache/ApacheHttpTransportTest.java

Lines changed: 27 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,48 @@
1414

1515
package com.google.api.client.http.apache;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
2117
import static org.mockito.Matchers.any;
2218
import static org.mockito.Mockito.mock;
2319
import static org.mockito.Mockito.when;
2420

25-
import com.google.api.client.http.LowLevelHttpResponse;
2621
import com.google.api.client.util.ByteArrayStreamingContent;
2722
import com.google.api.client.util.StringUtils;
28-
import java.io.IOException;
29-
import java.util.concurrent.atomic.AtomicBoolean;
30-
import java.util.concurrent.atomic.AtomicInteger;
31-
import org.apache.http.Header;
32-
import org.apache.http.HttpClientConnection;
33-
import org.apache.http.HttpException;
34-
import org.apache.http.HttpRequest;
35-
import org.apache.http.HttpRequestInterceptor;
23+
import junit.framework.TestCase;
3624
import org.apache.http.HttpResponse;
3725
import org.apache.http.HttpVersion;
3826
import org.apache.http.client.HttpClient;
3927
import org.apache.http.client.methods.HttpUriRequest;
40-
import org.apache.http.impl.client.HttpClients;
41-
import org.apache.http.message.BasicHttpResponse;
42-
import org.apache.http.protocol.HttpContext;
43-
import org.apache.http.protocol.HttpRequestExecutor;
44-
import org.junit.Test;
28+
import org.apache.http.client.params.ClientPNames;
29+
import org.apache.http.impl.client.DefaultHttpClient;
30+
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
31+
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
32+
import org.apache.http.params.CoreConnectionPNames;
33+
import org.apache.http.params.HttpParams;
34+
import org.apache.http.params.HttpProtocolParams;
4535

4636
/**
4737
* Tests {@link ApacheHttpTransport}.
4838
*
4939
* @author Yaniv Inbar
5040
*/
51-
public class ApacheHttpTransportTest {
41+
public class ApacheHttpTransportTest extends TestCase {
5242

53-
@Test
5443
public void testApacheHttpTransport() {
5544
ApacheHttpTransport transport = new ApacheHttpTransport();
56-
checkHttpTransport(transport);
45+
DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
46+
checkDefaultHttpClient(httpClient);
47+
checkHttpClient(httpClient);
5748
}
5849

59-
@Test
6050
public void testApacheHttpTransportWithParam() {
61-
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build());
62-
checkHttpTransport(transport);
51+
ApacheHttpTransport transport = new ApacheHttpTransport(new DefaultHttpClient());
52+
checkHttpClient(transport.getHttpClient());
6353
}
6454

65-
@Test
6655
public void testNewDefaultHttpClient() {
67-
HttpClient client = ApacheHttpTransport.newDefaultHttpClient();
68-
checkHttpClient(client);
56+
checkDefaultHttpClient(ApacheHttpTransport.newDefaultHttpClient());
6957
}
7058

71-
private void checkHttpTransport(ApacheHttpTransport transport) {
72-
assertNotNull(transport);
73-
HttpClient client = transport.getHttpClient();
74-
checkHttpClient(client);
75-
}
76-
77-
private void checkHttpClient(HttpClient client) {
78-
assertNotNull(client);
79-
// TODO(chingor): Is it possible to test this effectively? The newer HttpClient implementations
80-
// are read-only and we're testing that we built the client with the right configuration
81-
}
82-
83-
@Test
8459
public void testRequestsWithContent() throws Exception {
8560
HttpClient mockClient = mock(HttpClient.class);
8661
HttpResponse mockResponse = mock(HttpResponse.class);
@@ -98,8 +73,6 @@ public void testRequestsWithContent() throws Exception {
9873
subtestUnsupportedRequestsWithContent(
9974
transport.buildRequest("HEAD", "http://www.test.url"), "HEAD");
10075

101-
// Test PATCH.
102-
execute(transport.buildRequest("PATCH", "http://www.test.url"));
10376
// Test PUT.
10477
execute(transport.buildRequest("PUT", "http://www.test.url"));
10578
// Test POST.
@@ -128,51 +101,19 @@ private void execute(ApacheHttpRequest request) throws Exception {
128101
request.execute();
129102
}
130103

131-
@Test
132-
public void testRequestShouldNotFollowRedirects() throws IOException {
133-
final AtomicInteger requestsAttempted = new AtomicInteger(0);
134-
HttpRequestExecutor requestExecutor = new HttpRequestExecutor() {
135-
@Override
136-
public HttpResponse execute(HttpRequest request, HttpClientConnection conn,
137-
HttpContext context) throws IOException, HttpException {
138-
HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
139-
resp.addHeader("location", "https://google.com/path");
140-
requestsAttempted.incrementAndGet();
141-
return resp;
142-
}
143-
};
144-
HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build();
145-
ApacheHttpTransport transport = new ApacheHttpTransport(client);
146-
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
147-
LowLevelHttpResponse response = request.execute();
148-
assertEquals(1, requestsAttempted.get());
149-
assertEquals(302, response.getStatusCode());
104+
private void checkDefaultHttpClient(DefaultHttpClient client) {
105+
HttpParams params = client.getParams();
106+
assertTrue(client.getConnectionManager() instanceof ThreadSafeClientConnManager);
107+
assertEquals(8192, params.getIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, -1));
108+
DefaultHttpRequestRetryHandler retryHandler =
109+
(DefaultHttpRequestRetryHandler) client.getHttpRequestRetryHandler();
110+
assertEquals(0, retryHandler.getRetryCount());
111+
assertFalse(retryHandler.isRequestSentRetryEnabled());
150112
}
151113

152-
@Test
153-
public void testRequestCanSetHeaders() {
154-
final AtomicBoolean interceptorCalled = new AtomicBoolean(false);
155-
HttpClient client = HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() {
156-
@Override
157-
public void process(HttpRequest request, HttpContext context)
158-
throws HttpException, IOException {
159-
Header header = request.getFirstHeader("foo");
160-
assertNotNull("Should have found header", header);
161-
assertEquals("bar", header.getValue());
162-
interceptorCalled.set(true);
163-
throw new IOException("cancelling request");
164-
}
165-
}).build();
166-
167-
ApacheHttpTransport transport = new ApacheHttpTransport(client);
168-
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
169-
request.addHeader("foo", "bar");
170-
try {
171-
LowLevelHttpResponse response = request.execute();
172-
fail("should not actually make the request");
173-
} catch (IOException exception) {
174-
assertEquals("cancelling request", exception.getMessage());
175-
}
176-
assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
114+
private void checkHttpClient(HttpClient client) {
115+
HttpParams params = client.getParams();
116+
assertFalse(params.getBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true));
117+
assertEquals(HttpVersion.HTTP_1_1, HttpProtocolParams.getVersion(params));
177118
}
178119
}

0 commit comments

Comments
 (0)