1414
1515package 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 ;
2117import static org .mockito .Matchers .any ;
2218import static org .mockito .Mockito .mock ;
2319import static org .mockito .Mockito .when ;
2420
25- import com .google .api .client .http .LowLevelHttpResponse ;
2621import com .google .api .client .util .ByteArrayStreamingContent ;
2722import 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 ;
3624import org .apache .http .HttpResponse ;
3725import org .apache .http .HttpVersion ;
3826import org .apache .http .client .HttpClient ;
3927import 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