Search in sources :

Example 1 with SOAPTeststepProperties

use of io.irontest.models.teststep.SOAPTeststepProperties in project irontest by zheng-wang.

the class SOAPTeststepRunner method run.

protected BasicTeststepRun run(Teststep teststep) throws Exception {
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    Endpoint endpoint = teststep.getEndpoint();
    // set request HTTP headers
    HttpPost httpPost = new HttpPost(endpoint.getUrl());
    SOAPTeststepProperties otherProperties = (SOAPTeststepProperties) teststep.getOtherProperties();
    if (otherProperties != null) {
        for (HTTPHeader httpHeader : otherProperties.getHttpHeaders()) {
            httpPost.setHeader(httpHeader.getName(), httpHeader.getValue());
        }
    }
    // set HTTP basic auth
    if (!"".equals(StringUtils.trimToEmpty(endpoint.getUsername()))) {
        String auth = endpoint.getUsername() + ":" + getDecryptedEndpointPassword();
        String encodedAuth = Base64.encodeBase64String(auth.getBytes());
        String authHeader = "Basic " + encodedAuth;
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
    }
    // set request HTTP body
    httpPost.setEntity(new StringEntity((String) teststep.getRequest(), "UTF-8"));
    final SOAPAPIResponse apiResponse = new SOAPAPIResponse();
    ResponseHandler<Void> responseHandler = new ResponseHandler<Void>() {

        public Void handleResponse(final HttpResponse httpResponse) throws IOException {
            LOGGER.info(httpResponse.toString());
            apiResponse.getHttpHeaders().add(new HTTPHeader("*Status-Line*", httpResponse.getStatusLine().toString()));
            Header[] headers = httpResponse.getAllHeaders();
            for (Header header : headers) {
                apiResponse.getHttpHeaders().add(new HTTPHeader(header.getName(), header.getValue()));
            }
            HttpEntity entity = httpResponse.getEntity();
            apiResponse.setHttpBody(entity != null ? EntityUtils.toString(entity) : null);
            return null;
        }
    };
    // build HTTP Client instance
    // trust all SSL certificates
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {

        public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            return true;
        }
    }).build();
    HttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();
    // invoke the web service
    httpClient.execute(httpPost, responseHandler);
    basicTeststepRun.setResponse(apiResponse);
    return basicTeststepRun;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) SOAPTeststepProperties(io.irontest.models.teststep.SOAPTeststepProperties) ResponseHandler(org.apache.http.client.ResponseHandler) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) SSLContext(javax.net.ssl.SSLContext) StringEntity(org.apache.http.entity.StringEntity) Endpoint(io.irontest.models.endpoint.Endpoint) Header(org.apache.http.Header) HTTPHeader(io.irontest.models.teststep.HTTPHeader) HttpClient(org.apache.http.client.HttpClient) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HTTPHeader(io.irontest.models.teststep.HTTPHeader)

Aggregations

Endpoint (io.irontest.models.endpoint.Endpoint)1 HTTPHeader (io.irontest.models.teststep.HTTPHeader)1 SOAPTeststepProperties (io.irontest.models.teststep.SOAPTeststepProperties)1 SSLContext (javax.net.ssl.SSLContext)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 ResponseHandler (org.apache.http.client.ResponseHandler)1 HttpPost (org.apache.http.client.methods.HttpPost)1 TrustStrategy (org.apache.http.conn.ssl.TrustStrategy)1 StringEntity (org.apache.http.entity.StringEntity)1 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)1