Search in sources :

Example 6 with BasicTestingServlet

use of org.apache.felix.httplite.osgi.test.BasicTestingServlet in project felix by apache.

the class TestParameters method testParameterContents.

/**
 * Test the parameter contents.
 *
 * @throws IOException
 * @throws ServletException
 * @throws NamespaceException
 */
public void testParameterContents() throws IOException, ServletException, NamespaceException {
    HttpService httpService = getHTTPService(registry.getBundleContext());
    BasicTestingServlet testServlet = new BasicTestingServlet();
    httpService.registerServlet("/test", testServlet, null, null);
    HttpURLConnection client = getConnection(DEFAULT_BASE_URL + "/test", "GET");
    int parameterCount = 16;
    for (int i = 0; i < parameterCount; ++i) {
        client.addRequestProperty("k" + i, "v" + i);
    }
    client.connect();
    assertTrue(client.getResponseCode() == 200);
    Map rp = new HashMap();
    Enumeration e = testServlet.getHeaderNames();
    while (e.hasMoreElements()) {
        String key = e.nextElement().toString();
        rp.put(key, testServlet.getHeader(key));
    }
    for (int i = 0; i < parameterCount; ++i) {
        assertTrue(rp.get("k" + i).equals("v" + i));
    }
}
Also used : BasicTestingServlet(org.apache.felix.httplite.osgi.test.BasicTestingServlet) HttpURLConnection(java.net.HttpURLConnection) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) HttpService(org.osgi.service.http.HttpService) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with BasicTestingServlet

use of org.apache.felix.httplite.osgi.test.BasicTestingServlet in project felix by apache.

the class TestServletContainer method testExecutePOST.

/**
 * Test can execute POST method.
 *
 * @throws ServletException
 * @throws NamespaceException
 * @throws IOException
 */
public void testExecutePOST() throws ServletException, NamespaceException, IOException {
    HttpService httpService = getHTTPService(registry.getBundleContext());
    BasicTestingServlet testServlet = new BasicTestingServlet();
    httpService.registerServlet("/test", testServlet, null, null);
    HttpURLConnection client = getConnection(DEFAULT_BASE_URL + "/test", "POST");
    client.connect();
    assertTrue(client.getResponseCode() == 200);
    assertFalse(testServlet.isGetCalled());
    assertFalse(testServlet.isDeleteCalled());
    assertTrue(testServlet.isPostCalled());
    assertFalse(testServlet.isPutCalled());
}
Also used : BasicTestingServlet(org.apache.felix.httplite.osgi.test.BasicTestingServlet) HttpURLConnection(java.net.HttpURLConnection) HttpService(org.osgi.service.http.HttpService)

Example 8 with BasicTestingServlet

use of org.apache.felix.httplite.osgi.test.BasicTestingServlet in project felix by apache.

the class TestServletContainer method testExecuteGET.

/**
 * Test calling GET enters TestServlet doGet() method.
 *
 * @throws ServletException
 * @throws NamespaceException
 * @throws IOException
 */
public void testExecuteGET() throws ServletException, NamespaceException, IOException {
    HttpService httpService = getHTTPService(registry.getBundleContext());
    BasicTestingServlet testServlet = new BasicTestingServlet();
    httpService.registerServlet("/test", testServlet, null, null);
    HttpURLConnection client = getConnection(DEFAULT_BASE_URL + "/test", "GET");
    client.connect();
    assertTrue(client.getResponseCode() == 200);
    assertTrue(testServlet.isGetCalled());
    assertFalse(testServlet.isDeleteCalled());
    assertFalse(testServlet.isPostCalled());
    assertFalse(testServlet.isPutCalled());
}
Also used : BasicTestingServlet(org.apache.felix.httplite.osgi.test.BasicTestingServlet) HttpURLConnection(java.net.HttpURLConnection) HttpService(org.osgi.service.http.HttpService)

Example 9 with BasicTestingServlet

use of org.apache.felix.httplite.osgi.test.BasicTestingServlet in project felix by apache.

the class TestServletContainer method testExecuteDELETE.

/**
 * Test can execute DELETE method.
 *
 * @throws ServletException
 * @throws NamespaceException
 * @throws IOException
 */
public void testExecuteDELETE() throws ServletException, NamespaceException, IOException {
    HttpService httpService = getHTTPService(registry.getBundleContext());
    BasicTestingServlet testServlet = new BasicTestingServlet();
    httpService.registerServlet("/test", testServlet, null, null);
    HttpURLConnection client = getConnection(DEFAULT_BASE_URL + "/test", "DELETE");
    client.connect();
    assertTrue(client.getResponseCode() == 200);
    assertFalse(testServlet.isGetCalled());
    assertTrue(testServlet.isDeleteCalled());
    assertFalse(testServlet.isPostCalled());
    assertFalse(testServlet.isPutCalled());
}
Also used : BasicTestingServlet(org.apache.felix.httplite.osgi.test.BasicTestingServlet) HttpURLConnection(java.net.HttpURLConnection) HttpService(org.osgi.service.http.HttpService)

Example 10 with BasicTestingServlet

use of org.apache.felix.httplite.osgi.test.BasicTestingServlet in project felix by apache.

the class TestQueryString method testQueryString.

// TODO: test unicode keys and values
// TODO: test invalid query string in request
/**
 * @throws ServletException
 * @throws NamespaceException
 * @throws IOException
 */
public void testQueryString() throws ServletException, NamespaceException, IOException {
    HttpService httpService = getHTTPService(registry.getBundleContext());
    BasicTestingServlet testServlet = new BasicTestingServlet();
    httpService.registerServlet("/test", testServlet, null, null);
    StringBuffer qs = new StringBuffer("?");
    int parameterCount = 16;
    for (int i = 0; i < parameterCount; ++i) {
        qs.append("k" + i);
        qs.append("=");
        qs.append("v" + i);
        if (i != (parameterCount - 1)) {
            qs.append('&');
        }
    }
    HttpURLConnection client = getConnection(DEFAULT_BASE_URL + "/test" + qs.toString(), "GET");
    client.connect();
    assertTrue(client.getResponseCode() == 200);
    Map qsm = testServlet.getQueryStringMap();
    assertTrue(qsm.size() == parameterCount);
    for (int i = 0; i < parameterCount; ++i) {
        assertTrue(qsm.containsKey("k" + i));
        assertTrue(qsm.get("k" + i).equals("v" + i));
    }
}
Also used : BasicTestingServlet(org.apache.felix.httplite.osgi.test.BasicTestingServlet) HttpURLConnection(java.net.HttpURLConnection) HttpService(org.osgi.service.http.HttpService) Map(java.util.Map)

Aggregations

BasicTestingServlet (org.apache.felix.httplite.osgi.test.BasicTestingServlet)15 HttpService (org.osgi.service.http.HttpService)15 HttpURLConnection (java.net.HttpURLConnection)11 Enumeration (java.util.Enumeration)2 Map (java.util.Map)2 NamespaceException (org.osgi.service.http.NamespaceException)2 HashMap (java.util.HashMap)1