Search in sources :

Example 1 with SimpleResponse

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

the class SimpleRestService method hookResponse.

protected void hookResponse(SimpleHttpServerConnection conn, String body) throws IOException {
    SimpleResponse res = new SimpleResponse();
    res.setStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK);
    res.setBodyString(body);
    conn.setKeepAlive(false);
    conn.writeResponse(res);
}
Also used : SimpleResponse(org.apache.commons.httpclient.server.SimpleResponse)

Example 2 with SimpleResponse

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

the class TestResponseHeaders method testHeaders.

// ----------------------------------------------------------- Test Methods
public void testHeaders() 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("Connection", "close"));
            response.addHeader(new Header("Content-Length", Integer.toString(body.length())));
            response.addHeader(new Header("Content-Type", "text/xml; charset=utf-8"));
            response.addHeader(new Header("Date", "Wed, 28 Mar 2001 05:05:04 GMT"));
            response.addHeader(new Header("Server", "UserLand Frontier/7.0-WinNT"));
            response.setBodyString(body);
            return true;
        }
    });
    HttpMethod method = new GetMethod();
    client.executeMethod(method);
    assertEquals("close", method.getResponseHeader("Connection").getValue());
    assertEquals(body.length(), Integer.parseInt(method.getResponseHeader("Content-Length").getValue()));
    assertEquals("text/xml; charset=utf-8", method.getResponseHeader("Content-Type").getValue());
    assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method.getResponseHeader("Date").getValue());
    assertEquals("UserLand Frontier/7.0-WinNT", method.getResponseHeader("Server").getValue());
}
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 3 with SimpleResponse

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

the class TestResponseHeaders method testInvalidContentLength1.

public void testInvalidContentLength1() 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("Content-Length", "5"));
            response.addHeader(new Header("Content-Length", "stuff"));
            response.setBodyString("12345");
            return true;
        }
    });
    GetMethod method = new GetMethod("/");
    client.executeMethod(method);
    assertEquals(5, method.getResponseContentLength());
}
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 4 with SimpleResponse

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

the class TestResponseHeaders method testNullHeaders.

public void testNullHeaders() 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.setBodyString("XXX\r\nYYY\r\nZZZ");
            return true;
        }
    });
    HttpMethod method = new GetMethod("/");
    client.executeMethod(method);
    assertEquals(null, method.getResponseHeader(null));
    assertEquals(null, method.getResponseHeader("bogus"));
}
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 5 with SimpleResponse

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

the class TestResponseHeaders method testInvalidContentLength2.

public void testInvalidContentLength2() 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("Content-Length", "stuff"));
            response.addHeader(new Header("Content-Length", "5"));
            response.setBodyString("12345");
            return true;
        }
    });
    GetMethod method = new GetMethod("/");
    client.executeMethod(method);
    assertEquals(5, method.getResponseContentLength());
}
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)

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