Search in sources :

Example 1 with HttpResponse

use of ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse in project jaxrs-api by eclipse-ee4j.

the class AdaptiveHttpRequest method execute.

/**
 * <code>execute</code> will dispatch the current request to the target
 * server.
 *
 * @return HttpResponse the server's response.
 * @throws IOException
 *           if an I/O error occurs during dispatch.
 */
public HttpResponse execute() throws IOException, HttpException {
    String method;
    int defaultPort;
    ProtocolSocketFactory factory;
    if (_method.getFollowRedirects()) {
        client = new HttpClient();
        if (_isSecure) {
            method = "https";
            defaultPort = DEFAULT_SSL_PORT;
            factory = new SSLProtocolSocketFactory();
        } else {
            method = "http";
            defaultPort = DEFAULT_HTTP_PORT;
            factory = new DefaultProtocolSocketFactory();
        }
        Protocol protocol = new Protocol(method, factory, defaultPort);
        HttpConnection conn = new HttpConnection(_host, _port, protocol);
        if (conn.isOpen()) {
            throw new IllegalStateException("Connection incorrectly opened");
        }
        conn.open();
        TestUtil.logMsg("[HttpRequest] Dispatching request: '" + _requestLine + "' to target server at '" + _host + ":" + _port + "'");
        addSupportHeaders();
        _headers = _method.getRequestHeaders();
        TestUtil.logTrace("########## The real value set: " + _method.getFollowRedirects());
        client.getHostConfiguration().setHost(_host, _port, protocol);
        client.executeMethod(_method);
        return new HttpResponse(_host, _port, _isSecure, _method, getState());
    } else {
        if (_isSecure) {
            method = "https";
            defaultPort = DEFAULT_SSL_PORT;
            factory = new SSLProtocolSocketFactory();
        } else {
            method = "http";
            defaultPort = DEFAULT_HTTP_PORT;
            factory = new DefaultProtocolSocketFactory();
        }
        Protocol protocol = new Protocol(method, factory, defaultPort);
        HttpConnection conn = new HttpConnection(_host, _port, protocol);
        if (conn.isOpen()) {
            throw new IllegalStateException("Connection incorrectly opened");
        }
        conn.open();
        TestUtil.logMsg("[HttpRequest] Dispatching request: '" + _requestLine + "' to target server at '" + _host + ":" + _port + "'");
        addSupportHeaders();
        _headers = _method.getRequestHeaders();
        TestUtil.logTrace("########## The real value set: " + _method.getFollowRedirects());
        _method.execute(getState(), conn);
        return new HttpResponse(_host, _port, _isSecure, _method, getState());
    }
}
Also used : SSLProtocolSocketFactory(org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory) ProtocolSocketFactory(org.apache.commons.httpclient.protocol.ProtocolSocketFactory) DefaultProtocolSocketFactory(org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory) DefaultProtocolSocketFactory(org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory) SSLProtocolSocketFactory(org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory) HttpConnection(org.apache.commons.httpclient.HttpConnection) HttpClient(org.apache.commons.httpclient.HttpClient) HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 2 with HttpResponse

use of ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method selectVariantResponseVaryTest.

/*
   * @testName: selectVariantResponseVaryTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:119; JAXRS:SPEC:40;
   * 
   * @test_Strategy: Check if the response contains VARY
   */
@Test
public void selectVariantResponseVaryTest() throws Fault {
    setProperty(Property.REQUEST, buildRequest(GET, "SelectVariantTestResponse"));
    setProperty(Property.REQUEST_HEADERS, "Accept: application/json");
    setProperty(Property.REQUEST_HEADERS, "Accept-Encoding: *");
    setProperty(Property.REQUEST_HEADERS, "Accept-Language: *");
    setProperty(Property.STATUS_CODE, getStatusCode(Status.OK));
    invoke();
    HttpResponse response = _testCase.getResponse();
    Header[] headers = response.getResponseHeaders("Vary");
    assertTrue(headers.length != 0, "Expected at least 1 Vary response header");
    boolean accept = false, lang = false, encoding = false;
    for (Header header : headers) for (String vary : header.getValue().split(",")) {
        lang |= vary.contains("Accept-Language");
        encoding |= vary.contains("Accept-Encoding");
        accept |= (vary.contains("Accept") && !vary.contains("Accept-"));
    }
    assertTrue(lang, "Vary should contain Accept-Language");
    assertTrue(encoding, "Vary should contain Accept-Encoding");
    assertTrue(accept, "Vary should contain Accept");
}
Also used : Header(org.apache.commons.httpclient.Header) HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with HttpResponse

use of ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse in project jaxrs-api by eclipse-ee4j.

the class JAXRSProvidersClientIT method getReturnedNumber.

// ///////////////////////////////////////////////////////////////////////
protected int getReturnedNumber() throws Fault {
    HttpResponse response = _testCase.getResponse();
    String body;
    try {
        body = response.getResponseBodyAsString();
    } catch (IOException e) {
        throw new Fault(e);
    }
    return Integer.parseInt(body);
}
Also used : HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) IOException(java.io.IOException)

Example 4 with HttpResponse

use of ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse in project jaxrs-api by eclipse-ee4j.

the class JAXRSCommonClient method getResponseBody.

/**
 * @return http response body as string
 * @throws Fault
 *           when an error occur
 */
protected String getResponseBody() throws Fault {
    try {
        HttpResponse response;
        response = _testCase.getResponse();
        boolean isNull = response.getResponseBodyAsRawStream() == null;
        return isNull ? null : response.getResponseBodyAsString();
    } catch (IOException e) {
        throw new Fault(e);
    }
}
Also used : HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse)

Example 5 with HttpResponse

use of ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method headGetTest.

/*
   * @testName: headGetTest
   * 
   * @assertion_ids: JAXRS:SPEC:17.2;
   * 
   * @test_Strategy: Call a method annotated with a request method designator
   * for GET and discard any returned entity
   */
@Test
public void headGetTest() throws Fault, IOException {
    setProperty(REQUEST, buildRequest("HEAD", "get"));
    setProperty(Property.EXPECTED_HEADERS, "CTS-HEAD: get");
    invoke();
    HttpResponse request = _testCase.getResponse();
    assertTrue(request.getResponseBodyAsRawString() == null, "Unexpected entity in request body");
}
Also used : HttpResponse(ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (ee.jakarta.tck.ws.rs.common.webclient.http.HttpResponse)6 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 Header (org.apache.commons.httpclient.Header)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpConnection (org.apache.commons.httpclient.HttpConnection)1 DefaultProtocolSocketFactory (org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory)1 Protocol (org.apache.commons.httpclient.protocol.Protocol)1 ProtocolSocketFactory (org.apache.commons.httpclient.protocol.ProtocolSocketFactory)1 SSLProtocolSocketFactory (org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory)1