Search in sources :

Example 61 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestJettyOSGiBootContextAsService method testContextHandlerAsOSGiService.

/**
     */
@Test
public void testContextHandlerAsOSGiService() throws Exception {
    // now test the context
    HttpClient client = new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/index.html");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        assertTrue(content.indexOf("<h1>Test OSGi Context</h1>") != -1);
    } finally {
        client.stop();
    }
    ServiceReference[] refs = bundleContext.getServiceReferences(ContextHandler.class.getName(), null);
    assertNotNull(refs);
    assertEquals(1, refs.length);
    ContextHandler ch = (ContextHandler) bundleContext.getService(refs[0]);
    assertEquals("/acme", ch.getContextPath());
    // Stop the bundle with the ContextHandler in it and check the jetty
    // Context is destroyed for it.
    // TODO: think of a better way to communicate this to the test, other
    // than checking stderr output
    Bundle testWebBundle = TestOSGiUtil.getBundle(bundleContext, "org.eclipse.jetty.osgi.testcontext");
    assertNotNull("Could not find the org.eclipse.jetty.test-jetty-osgi-context.jar bundle", testWebBundle);
    assertTrue("The bundle org.eclipse.jetty.testcontext is not correctly resolved", testWebBundle.getState() == Bundle.ACTIVE);
    testWebBundle.stop();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Bundle(org.osgi.framework.Bundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) HttpClient(org.eclipse.jetty.client.HttpClient) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 62 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestJettyOSGiBootWithJsp method testJspDump.

@Test
public void testJspDump() throws Exception {
    // TestOSGiUtil.debugBundles(bundleContext);
    HttpClient client = new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/jsp/jstl.jsp");
        assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        assertTrue(content.contains("JSTL Example"));
    } finally {
        client.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 63 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class TestOSGiUtil method testHttpServiceGreetings.

protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
    assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
    assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
    // in the OSGi world this would be bad code and we should use a bundle
    // tracker.
    // here we purposely want to make sure that the httpService is actually
    // ready.
    ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
    Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
    HttpService http = (HttpService) bundleContext.getService(sr);
    http.registerServlet("/greetings", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("Hello");
        }
    }, null, null);
    // now test the servlet
    HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
    try {
        client.start();
        ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
        Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        String content = new String(response.getContent());
        Assert.assertEquals("Hello", content);
    } finally {
        client.stop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpService(org.osgi.service.http.HttpService) HttpServlet(javax.servlet.http.HttpServlet) HttpClient(org.eclipse.jetty.client.HttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference)

Example 64 with HttpClient

use of org.eclipse.jetty.client.HttpClient 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();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 65 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class HttpChannelAssociationTest method testAssociationFailedAbortsRequest.

@Test
public void testAssociationFailedAbortsRequest() throws Exception {
    startServer(new EmptyServerHandler());
    client = new HttpClient(newHttpClientTransport(transport, exchange -> false), sslContextFactory);
    QueuedThreadPool clientThreads = new QueuedThreadPool();
    clientThreads.setName("client");
    client.setExecutor(clientThreads);
    client.start();
    CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(newURI()).send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)197 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)102 Test (org.junit.Test)94 Request (org.eclipse.jetty.client.api.Request)53 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 Test (org.testng.annotations.Test)34 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)19 CloudStore (com.yahoo.athenz.zts.store.CloudStore)17 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)16 HttpCertSigner (com.yahoo.athenz.zts.cert.impl.HttpCertSigner)14 HttpCertSignerFactory (com.yahoo.athenz.zts.cert.impl.HttpCertSignerFactory)14 URI (java.net.URI)11 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)11 ExecutionException (java.util.concurrent.ExecutionException)8 HttpProxy (org.eclipse.jetty.client.HttpProxy)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7