forked from googleapis/google-http-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApacheHttpTransportTest.java
213 lines (193 loc) · 7.97 KB
/
ApacheHttpTransportTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.http.apache.v2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.util.ByteArrayStreamingContent;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.http.Header;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.junit.Test;
/**
* Tests {@link ApacheHttpTransport}.
*
* @author Yaniv Inbar
*/
public class ApacheHttpTransportTest {
@Test
public void testApacheHttpTransport() {
ApacheHttpTransport transport = new ApacheHttpTransport();
checkHttpTransport(transport);
}
@Test
public void testApacheHttpTransportWithParam() {
ApacheHttpTransport transport = new ApacheHttpTransport(HttpClients.custom().build());
checkHttpTransport(transport);
}
@Test
public void testNewDefaultHttpClient() {
HttpClient client = ApacheHttpTransport.newDefaultHttpClient();
checkHttpClient(client);
}
private void checkHttpTransport(ApacheHttpTransport transport) {
assertNotNull(transport);
HttpClient client = transport.getHttpClient();
checkHttpClient(client);
}
private void checkHttpClient(HttpClient client) {
assertNotNull(client);
// TODO(chingor): Is it possible to test this effectively? The newer HttpClient implementations
// are read-only and we're testing that we built the client with the right configuration
}
@Test
public void testRequestsWithContent() throws IOException {
HttpClient mockClient = mock(HttpClient.class);
HttpResponse mockResponse = mock(HttpResponse.class);
when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(mockResponse);
ApacheHttpTransport transport = new ApacheHttpTransport(mockClient);
// Test GET.
subtestUnsupportedRequestsWithContent(
transport.buildRequest("GET", "http://www.test.url"), "GET");
// Test DELETE.
subtestUnsupportedRequestsWithContent(
transport.buildRequest("DELETE", "http://www.test.url"), "DELETE");
// Test HEAD.
subtestUnsupportedRequestsWithContent(
transport.buildRequest("HEAD", "http://www.test.url"), "HEAD");
// Test PATCH.
execute(transport.buildRequest("PATCH", "http://www.test.url"));
// Test PUT.
execute(transport.buildRequest("PUT", "http://www.test.url"));
// Test POST.
execute(transport.buildRequest("POST", "http://www.test.url"));
// Test PATCH.
execute(transport.buildRequest("PATCH", "http://www.test.url"));
}
private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, String method)
throws IOException {
try {
execute(request);
fail("expected " + IllegalArgumentException.class);
} catch (IllegalArgumentException e) {
// expected
assertEquals(e.getMessage(),
"Apache HTTP client does not support " + method + " requests with content.");
}
}
private void execute(ApacheHttpRequest request) throws IOException {
byte[] bytes = "abc".getBytes(StandardCharsets.UTF_8);
request.setStreamingContent(new ByteArrayStreamingContent(bytes));
request.setContentType("text/html");
request.setContentLength(bytes.length);
request.execute();
}
@Test
public void testRequestShouldNotFollowRedirects() throws IOException {
final AtomicInteger requestsAttempted = new AtomicInteger(0);
HttpRequestExecutor requestExecutor = new HttpRequestExecutor() {
@Override
public HttpResponse execute(HttpRequest request, HttpClientConnection connection,
HttpContext context) throws IOException, HttpException {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
response.addHeader("location", "https://google.com/path");
requestsAttempted.incrementAndGet();
return response;
}
};
HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build();
ApacheHttpTransport transport = new ApacheHttpTransport(client);
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
LowLevelHttpResponse response = request.execute();
assertEquals(1, requestsAttempted.get());
assertEquals(302, response.getStatusCode());
}
@Test
public void testRequestCanSetHeaders() {
final AtomicBoolean interceptorCalled = new AtomicBoolean(false);
HttpClient client = HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
Header header = request.getFirstHeader("foo");
assertNotNull("Should have found header", header);
assertEquals("bar", header.getValue());
interceptorCalled.set(true);
throw new IOException("cancelling request");
}
}).build();
ApacheHttpTransport transport = new ApacheHttpTransport(client);
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
request.addHeader("foo", "bar");
try {
LowLevelHttpResponse response = request.execute();
fail("should not actually make the request");
} catch (IOException exception) {
assertEquals("cancelling request", exception.getMessage());
}
assertTrue("Expected to have called our test interceptor", interceptorCalled.get());
}
@Test
public void testNormalizedUrl() throws IOException {
int port = 8000 + new Random().nextInt(1000);
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext(
"/",
new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
byte[] response = httpExchange.getRequestURI().toString().getBytes();
httpExchange.sendResponseHeaders(200, response.length);
OutputStream os = httpExchange.getResponseBody();
os.write(response);
os.close();
}
});
server.start();
ApacheHttpTransport transport = new ApacheHttpTransport();
com.google.api.client.http.HttpResponse response =
transport
.createRequestFactory()
.buildGetRequest(new GenericUrl("http://localhost:" + port + "/foo//bar"))
.execute();
assertEquals(200, response.getStatusCode());
assertEquals("/foo//bar", response.parseAsString());
}
}