14
14
15
15
package com .google .api .client .http .apache ;
16
16
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 ;
21
17
import static org .mockito .Matchers .any ;
22
18
import static org .mockito .Mockito .mock ;
23
19
import static org .mockito .Mockito .when ;
24
20
25
- import com .google .api .client .http .LowLevelHttpResponse ;
26
21
import com .google .api .client .util .ByteArrayStreamingContent ;
27
22
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 ;
36
24
import org .apache .http .HttpResponse ;
37
25
import org .apache .http .HttpVersion ;
38
26
import org .apache .http .client .HttpClient ;
39
27
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 ;
45
35
46
36
/**
47
37
* Tests {@link ApacheHttpTransport}.
48
38
*
49
39
* @author Yaniv Inbar
50
40
*/
51
- public class ApacheHttpTransportTest {
41
+ public class ApacheHttpTransportTest extends TestCase {
52
42
53
- @ Test
54
43
public void testApacheHttpTransport () {
55
44
ApacheHttpTransport transport = new ApacheHttpTransport ();
56
- checkHttpTransport (transport );
45
+ DefaultHttpClient httpClient = (DefaultHttpClient ) transport .getHttpClient ();
46
+ checkDefaultHttpClient (httpClient );
47
+ checkHttpClient (httpClient );
57
48
}
58
49
59
- @ Test
60
50
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 () );
63
53
}
64
54
65
- @ Test
66
55
public void testNewDefaultHttpClient () {
67
- HttpClient client = ApacheHttpTransport .newDefaultHttpClient ();
68
- checkHttpClient (client );
56
+ checkDefaultHttpClient (ApacheHttpTransport .newDefaultHttpClient ());
69
57
}
70
58
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
84
59
public void testRequestsWithContent () throws Exception {
85
60
HttpClient mockClient = mock (HttpClient .class );
86
61
HttpResponse mockResponse = mock (HttpResponse .class );
@@ -98,8 +73,6 @@ public void testRequestsWithContent() throws Exception {
98
73
subtestUnsupportedRequestsWithContent (
99
74
transport .buildRequest ("HEAD" , "http://www.test.url" ), "HEAD" );
100
75
101
- // Test PATCH.
102
- execute (transport .buildRequest ("PATCH" , "http://www.test.url" ));
103
76
// Test PUT.
104
77
execute (transport .buildRequest ("PUT" , "http://www.test.url" ));
105
78
// Test POST.
@@ -128,51 +101,19 @@ private void execute(ApacheHttpRequest request) throws Exception {
128
101
request .execute ();
129
102
}
130
103
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 ());
150
112
}
151
113
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 ));
177
118
}
178
119
}
0 commit comments