use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method testRelativeURIPathWithSpaces.
@Test
public void testRelativeURIPathWithSpaces() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/a+space?relative=true&decode=true").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method test_303_302.
@Test
public void test_303_302() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/localhost/302/localhost/done").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class HttpClientRedirectTest method test_303_302_OnDifferentDestinations.
@Test
public void test_303_302_OnDifferentDestinations() throws Exception {
start(new RedirectHandler());
Response response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/303/127.0.0.1/302/localhost/done").timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncIOServletTest method testWriteFromOnDataAvailable.
@Test
public void testWriteFromOnDataAvailable() throws Exception {
Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
CountDownLatch writeLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
while (input.isReady()) {
byte[] buffer = new byte[512];
int read = input.read(buffer);
if (read < 0) {
asyncContext.complete();
break;
}
if (output.isReady())
output.write(buffer, 0, read);
else
Assert.fail();
}
}
@Override
public void onAllDataRead() throws IOException {
asyncContext.complete();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
response.getOutputStream().setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
writeLatch.countDown();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
}
});
String content = "0123456789ABCDEF";
DeferredContentProvider contentProvider = new DeferredContentProvider();
contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
Response response = result.getResponse();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(getContentAsString(), Matchers.equalTo(content));
assertThat(errors, Matchers.hasSize(0));
clientLatch.countDown();
}
}
});
assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
contentProvider.close();
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.api.Response in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncWriteClosed.
@Test
public void testAsyncWriteClosed() throws Exception {
String text = "Now is the winter of our discontent. How Now Brown Cow. The quick brown fox jumped over the lazy dog.\n";
for (int i = 0; i < 10; i++) text = text + text;
byte[] data = text.getBytes(StandardCharsets.UTF_8);
CountDownLatch errorLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
assertScope();
// Wait for the failure to arrive to
// the server while we are about to write.
sleep(1000);
out.write(data);
}
@Override
public void onError(Throwable t) {
assertScope();
async.complete();
errorLatch.countDown();
}
});
}
});
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
if (response.getStatus() == HttpStatus.OK_200)
response.abort(new IOException("explicitly_closed_by_test"));
}).send(result -> {
if (result.isFailed())
clientLatch.countDown();
});
assertTrue(errorLatch.await(5, TimeUnit.SECONDS));
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Aggregations