use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class DigestPostTest method testServerWithHttpClientStreamContent.
@Test
public void testServerWithHttpClientStreamContent() throws Exception {
String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
HttpClient client = new HttpClient();
try {
AuthenticationStore authStore = client.getAuthenticationStore();
authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
client.start();
String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
Request request = client.newRequest(srvUrl);
request.method(HttpMethod.POST);
request.content(new StringContentProvider(sent));
_received = null;
request = request.timeout(5, TimeUnit.SECONDS);
ContentResponse response = request.send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(sent, _received);
} finally {
client.stop();
}
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class AsyncIOServletTest method testCompleteBeforeOnAllDataRead.
@Test
public void testCompleteBeforeOnAllDataRead() throws Exception {
String success = "SUCCESS";
AtomicBoolean allDataRead = new AtomicBoolean(false);
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() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
while (input.isReady()) {
int b = input.read();
if (b < 0) {
output.write(success.getBytes(StandardCharsets.UTF_8));
async.complete();
return;
}
}
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
output.write("FAILURE".getBytes(StandardCharsets.UTF_8));
allDataRead.set(true);
throw new IllegalStateException();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider("XYZ")).timeout(5, TimeUnit.SECONDS).send();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(response.getContentAsString(), Matchers.equalTo(success));
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange.
@Test
public void test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange() throws Exception {
byte[] data = new byte[128 * 1024];
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ServletInputStream input = request.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
response.setContentLength(data.length);
response.getOutputStream().write(data);
try {
// Delay the server from sending the TCP FIN.
Thread.sleep(1000);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
String host = "localhost";
int port = connector.getLocalPort();
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(new StringContentProvider("0")).onRequestSuccess(request -> {
HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
}).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
Assert.assertArrayEquals(data, response.getContent());
Assert.assertEquals(0, connectionPool.getConnectionCount());
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class HttpClientTest method testCopyRequest.
@Test
public void testCopyRequest() throws Exception {
startClient();
assertCopyRequest(client.newRequest("http://example.com/some/url").method(HttpMethod.HEAD).version(HttpVersion.HTTP_2).content(new StringContentProvider("some string")).timeout(321, TimeUnit.SECONDS).idleTimeout(2221, TimeUnit.SECONDS).followRedirects(true).header(HttpHeader.CONTENT_TYPE, "application/json").header("X-Some-Custom-Header", "some-value"));
assertCopyRequest(client.newRequest("https://example.com").method(HttpMethod.POST).version(HttpVersion.HTTP_1_0).content(new StringContentProvider("some other string")).timeout(123231, TimeUnit.SECONDS).idleTimeout(232342, TimeUnit.SECONDS).followRedirects(false).header(HttpHeader.ACCEPT, "application/json").header("X-Some-Other-Custom-Header", "some-other-value"));
assertCopyRequest(client.newRequest("https://example.com").header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.ACCEPT, "application/xml").header("x-same-name", "value1").header("x-same-name", "value2"));
assertCopyRequest(client.newRequest("https://example.com").header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.CONTENT_TYPE, "application/json"));
assertCopyRequest(client.newRequest("https://example.com").header("Accept", "application/json").header("Content-Type", "application/json"));
assertCopyRequest(client.newRequest("https://example.com").header("X-Custom-Header-1", "value1").header("X-Custom-Header-2", "value2"));
assertCopyRequest(client.newRequest("https://example.com").header("X-Custom-Header-1", "value").header("X-Custom-Header-2", "value"));
}
use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncReadThrows.
private void testAsyncReadThrows(Throwable throwable) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
AsyncContext asyncContext = request.startAsync(request, response);
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
if (throwable instanceof RuntimeException)
throw (RuntimeException) throwable;
if (throwable instanceof Error)
throw (Error) throwable;
throw new IOException(throwable);
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
}
@Override
public void onError(Throwable t) {
assertScope();
Assert.assertThat("onError type", t, instanceOf(throwable.getClass()));
Assert.assertThat("onError message", t.getMessage(), is(throwable.getMessage()));
latch.countDown();
response.setStatus(500);
asyncContext.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
}
Aggregations