use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method test_PUT_WithParameters.
@Test
public void test_PUT_WithParameters() throws Exception {
final String paramName = "a";
final String paramValue = "€";
final String encodedParamValue = URLEncoder.encode(paramValue, "UTF-8");
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
String value = request.getParameter(paramName);
if (paramValue.equals(value)) {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getOutputStream().print(value);
}
}
});
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + encodedParamValue);
ContentResponse response = client.newRequest(uri).method(HttpMethod.PUT).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8));
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method testUserAgentCanBeRemoved.
@Test
public void testUserAgentCanBeRemoved() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
ArrayList<String> userAgents = Collections.list(request.getHeaders("User-Agent"));
if ("/ua".equals(target))
Assert.assertEquals(1, userAgents.size());
else
Assert.assertEquals(0, userAgents.size());
}
});
// User agent not specified, use default.
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/ua").timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
// User agent explicitly removed.
response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).agent(null).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
// User agent explicitly removed.
response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).header(HttpHeader.USER_AGENT, null).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method test_Request_IdleTimeout.
@Slow
@Test
public void test_Request_IdleTimeout() throws Exception {
final long idleTimeout = 1000;
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
baseRequest.setHandled(true);
TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
} catch (InterruptedException x) {
throw new ServletException(x);
}
}
});
final String host = "localhost";
final int port = connector.getLocalPort();
try {
client.newRequest(host, port).scheme(scheme).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
Assert.fail();
} catch (ExecutionException expected) {
Assert.assertTrue(expected.getCause() instanceof TimeoutException);
}
// Make another request without specifying the idle timeout, should not fail
ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method test_POST_WithParameters_WithContent.
@Test
public void test_POST_WithParameters_WithContent() throws Exception {
final byte[] content = { 0, 1, 2, 3 };
final String paramName = "a";
final String paramValue = "€";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
consume(request.getInputStream(), true);
String value = request.getParameter(paramName);
if (paramValue.equals(value)) {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/plain");
response.getOutputStream().write(content);
}
}
});
ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
Assert.assertArrayEquals(content, response.getContent());
}
use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.
the class HttpClientTest method testCustomHostHeader.
@Test
public void testCustomHostHeader() throws Exception {
final String host = "localhost";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(host, request.getServerName());
}
});
ContentResponse response = client.newRequest("http://127.0.0.1:" + connector.getLocalPort() + "/path").scheme(scheme).header(HttpHeader.HOST, host).send();
Assert.assertEquals(200, response.getStatus());
}
Aggregations