use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class AsyncIOServletTest method testOnErrorThrows.
@Test
public void testOnErrorThrows() throws Exception {
AtomicInteger errors = new AtomicInteger();
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
if (request.getDispatcherType() == DispatcherType.ERROR) {
response.flushBuffer();
return;
}
request.startAsync(request, response);
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
throw new NullPointerException("explicitly_thrown_by_test_1");
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
}
@Override
public void onError(Throwable t) {
assertScope();
errors.incrementAndGet();
throw new NullPointerException("explicitly_thrown_by_test_2") {
{
this.initCause(t);
}
};
}
});
}
});
try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
ContentResponse response = client.newRequest(newURI()).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
Assert.assertEquals(1, errors.get());
}
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class AsyncIOServletTest method testIsReadyAtEOF.
@Test
public void testIsReadyAtEOF() throws Exception {
String text = "TEST\n";
byte[] data = text.getBytes(StandardCharsets.UTF_8);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
input.setReadListener(new ReadListener() {
transient int _i = 0;
transient boolean _minusOne = false;
transient boolean _finished = false;
@Override
public void onDataAvailable() throws IOException {
assertScope();
while (input.isReady() && !input.isFinished()) {
int b = input.read();
if (b == -1)
_minusOne = true;
else if (data[_i++] != b)
throw new IllegalStateException();
}
if (input.isFinished())
_finished = true;
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
output.write(String.format("i=%d eof=%b finished=%b", _i, _minusOne, _finished).getBytes(StandardCharsets.UTF_8));
async.complete();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
async.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider(text)).timeout(5, TimeUnit.SECONDS).send();
String responseContent = response.getContentAsString();
assertThat(responseContent, containsString("i=" + data.length + " eof=true finished=true"));
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class ForwardProxyTLSServerTest method testTwoExchanges.
@Test
public void testTwoExchanges() throws Exception {
startTLSServer(new ServerHandler());
startProxy();
HttpClient httpClient = new HttpClient(newSslContextFactory());
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.start();
try {
String body = "BODY";
ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(body, "UTF-8")).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
String content = response1.getContentAsString();
Assert.assertEquals(body, content);
content = "body=" + body;
ContentResponse response2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(content.length())).content(new StringContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
content = response2.getContentAsString();
Assert.assertEquals(body, content);
} finally {
httpClient.stop();
}
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class ForwardProxyTLSServerTest method testTwoConcurrentExchanges.
@Test
public void testTwoConcurrentExchanges() throws Exception {
startTLSServer(new ServerHandler());
startProxy();
final HttpClient httpClient = new HttpClient(newSslContextFactory());
httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
httpClient.start();
try {
final AtomicReference<Connection> connection = new AtomicReference<>();
final CountDownLatch connectionLatch = new CountDownLatch(1);
String content1 = "BODY";
ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(content1, "UTF-8")).onRequestCommit(request -> {
Destination destination = httpClient.getDestination(HttpScheme.HTTPS.asString(), "localhost", serverConnector.getLocalPort());
destination.newConnection(new Promise.Adapter<Connection>() {
@Override
public void succeeded(Connection result) {
connection.set(result);
connectionLatch.countDown();
}
});
}).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
String content = response1.getContentAsString();
Assert.assertEquals(content1, content);
Assert.assertTrue(connectionLatch.await(5, TimeUnit.SECONDS));
String body2 = "body=" + content1;
org.eclipse.jetty.client.api.Request request2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(body2.length())).content(new StringContentProvider(body2));
// Make sure the second connection can send the exchange via the tunnel
FutureResponseListener listener2 = new FutureResponseListener(request2);
connection.get().send(request2, listener2);
ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
String content2 = response2.getContentAsString();
Assert.assertEquals(content1, content2);
} finally {
httpClient.stop();
}
}
use of org.eclipse.jetty.client.util.StringContentProvider in project camel by apache.
the class JettyContentExchange9 method setRequestContent.
public void setRequestContent(String data, String charset) throws UnsupportedEncodingException {
StringContentProvider cp = charset != null ? new StringContentProvider(data, charset) : new StringContentProvider(data);
this.request.content(cp, this.requestContentType);
}
Aggregations