use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testExternalServer.
@Ignore
@Test
public void testExternalServer() throws Exception {
HTTP2Client http2Client = new HTTP2Client();
SslContextFactory sslContextFactory = new SslContextFactory();
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), sslContextFactory);
Executor executor = new QueuedThreadPool();
httpClient.setExecutor(executor);
httpClient.start();
// ContentResponse response = httpClient.GET("https://http2.akamai.com/");
ContentResponse response = httpClient.GET("https://webtide.com/");
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
httpClient.stop();
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testRequestHasHTTP2Version.
@Test
public void testRequestHasHTTP2Version() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
HttpVersion version = HttpVersion.fromString(request.getProtocol());
response.setStatus(version == HttpVersion.HTTP_2 ? HttpStatus.OK_200 : HttpStatus.INTERNAL_SERVER_ERROR_500);
}
});
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).onRequestBegin(request -> {
if (request.getVersion() != HttpVersion.HTTP_2)
request.abort(new Exception("Not a HTTP/2 request"));
}).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTransportOverHTTP2Test method testRequestViaForwardHttpProxy.
@Test
public void testRequestViaForwardHttpProxy() throws Exception {
String path = "/path";
String query = "a=b";
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(path, request.getRequestURI());
Assert.assertEquals(query, request.getQueryString());
}
});
int proxyPort = connector.getLocalPort();
client.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", proxyPort));
// Any port will do, just not the same as the proxy.
int serverPort = proxyPort + 1;
ContentResponse response = client.newRequest("localhost", serverPort).path(path + "?" + query).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class PushedResourcesTest method testPushedResourceCancelled.
@Test
public void testPushedResourceCancelled() throws Exception {
String pushPath = "/secondary";
CountDownLatch latch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {
@Override
public void succeeded(Stream pushStream) {
// Just send the normal response and wait for the reset.
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
}
}, new Stream.Listener.Adapter() {
@Override
public void onReset(Stream stream, ResetFrame frame) {
latch.countDown();
}
});
return null;
}
});
HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testAfterContentTransformerDoNoTransform.
private void testAfterContentTransformerDoNoTransform(final boolean readSource, final boolean useDisk) throws Exception {
final String key0 = "id";
long value0 = 1;
final String key1 = "channel";
String value1 = "foo";
final String json = "{ \"" + key0 + "\":" + value0 + ", \"" + key1 + "\":\"" + value1 + "\" }";
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
return new AfterContentTransformer() {
{
if (useDisk)
setMaxInputBufferSize(0);
}
@Override
public boolean transform(Source source, Sink sink) throws IOException {
if (readSource) {
InputStream input = source.getInputStream();
JSON.parse(new InputStreamReader(input, "UTF-8"));
}
// No transformation.
return false;
}
};
}
});
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
@SuppressWarnings("unchecked") Map<String, Object> obj = (Map<String, Object>) JSON.parse(response.getContentAsString());
Assert.assertNotNull(obj);
Assert.assertEquals(2, obj.size());
Assert.assertEquals(value0, obj.get(key0));
Assert.assertEquals(value1, obj.get(key1));
}
Aggregations