Search in sources :

Example 6 with SimpleResponse

use of org.apache.commons.httpclient.server.SimpleResponse in project ecf by eclipse.

the class TestResponseHeaders method testDuplicateConnection.

public void testDuplicateConnection() throws Exception {
    this.server.setHttpService(new HttpService() {

        public boolean process(SimpleRequest request, SimpleResponse response) throws IOException {
            response.setStatusLine(request.getRequestLine().getHttpVersion(), 200);
            response.addHeader(new Header("Connection", "close"));
            response.addHeader(new Header("Connection", "close"));
            return true;
        }
    });
    GetMethod method = new GetMethod("/");
    client.executeMethod(method);
    method.getResponseBodyAsString();
    assertFalse(connectionManager.getConection().isOpen());
    this.server.setHttpService(new HttpService() {

        public boolean process(SimpleRequest request, SimpleResponse response) throws IOException {
            response.setStatusLine(HttpVersion.HTTP_1_0, 200);
            response.addHeader(new Header("Connection", "keep-alive"));
            response.addHeader(new Header("Connection", "keep-alive"));
            response.setBodyString("aa");
            return true;
        }
    });
    method = new GetMethod("/");
    client.executeMethod(method);
    method.getResponseBodyAsString();
    assertTrue(connectionManager.getConection().isOpen());
}
Also used : HttpService(org.apache.commons.httpclient.server.HttpService) SimpleResponse(org.apache.commons.httpclient.server.SimpleResponse) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Example 7 with SimpleResponse

use of org.apache.commons.httpclient.server.SimpleResponse in project ecf by eclipse.

the class TestResponseHeaders method testDuplicateContentLength.

/**
 * Tests that having a duplicate content length causes no problems.
 */
public void testDuplicateContentLength() throws Exception {
    final String body = "XXX\r\nYYY\r\nZZZ";
    this.server.setHttpService(new HttpService() {

        public boolean process(SimpleRequest request, SimpleResponse response) throws IOException {
            response.setStatusLine(request.getRequestLine().getHttpVersion(), 200);
            response.addHeader(new Header("Content-Length", Integer.toString(body.length())));
            response.addHeader(new Header("Content-Length", Integer.toString(body.length())));
            response.setBodyString(body);
            return true;
        }
    });
    HttpMethod method = new GetMethod();
    client.executeMethod(method);
    assertNotNull("Response body is null.", method.getResponseBodyAsStream());
}
Also used : HttpService(org.apache.commons.httpclient.server.HttpService) SimpleResponse(org.apache.commons.httpclient.server.SimpleResponse) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Example 8 with SimpleResponse

use of org.apache.commons.httpclient.server.SimpleResponse in project ecf by eclipse.

the class TestConnectionPersistence method testRequestConnClose.

public void testRequestConnClose() throws Exception {
    this.server.setRequestHandler(new HttpRequestHandler() {

        public boolean processRequest(final SimpleHttpServerConnection conn, final SimpleRequest request) throws IOException {
            // Make sure the request if fully consumed
            request.getBodyBytes();
            SimpleResponse response = new SimpleResponse();
            response.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
            response.setBodyString("stuff back");
            conn.setKeepAlive(true);
            conn.writeResponse(response);
            return true;
        }
    });
    AccessibleHttpConnectionManager connman = new AccessibleHttpConnectionManager();
    this.client.getParams().setVersion(HttpVersion.HTTP_1_0);
    this.client.setHttpConnectionManager(connman);
    PostMethod httppost = new PostMethod("/test/");
    httppost.setRequestHeader("Connection", "close");
    httppost.setRequestEntity(new StringRequestEntity("stuff"));
    try {
        this.client.executeMethod(httppost);
    } finally {
        httppost.releaseConnection();
    }
    assertFalse(connman.getConection().isOpen());
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) PostMethod(org.apache.commons.httpclient.methods.PostMethod) SimpleResponse(org.apache.commons.httpclient.server.SimpleResponse) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException)

Aggregations

SimpleResponse (org.apache.commons.httpclient.server.SimpleResponse)8 IOException (java.io.IOException)7 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 HttpService (org.apache.commons.httpclient.server.HttpService)6 PostMethod (org.apache.commons.httpclient.methods.PostMethod)1 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)1 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)1 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)1