0

I have the following code in JSR223 Sampler and I get SSL certificate error. Is there any way to do disable?

Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;


List<String> sendRequest(String url, String method, String body) {


    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(body, "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();

   String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
            request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";


    HttpClientBuilder.create().build().withCloseable {httpClient ->

        httpClient.execute(request).withCloseable {response ->

            String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                    response.getAllHeaders() + "\n" +
                    (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

            System.out.println(req + "\n"  + res );

            return Arrays.asList(req, res);
        }
    }
}

List test1 = sendRequest("https://testserver.com","POST", "");
log.info(Arrays.toString(test1));
user1829449
  • 59
  • 11

1 Answers1

1

Just use normal HTTP Request sampler instead, as per documentation:

The JMeter HTTP samplers are configured to accept all certificates, whether trusted or not, regardless of validity periods, etc. This is to allow the maximum flexibility in testing servers


However if you're doing something very special and need to do the same in Groovy - here is example solution:

import org.apache.http.HttpHeaders
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.SSLConnectionSocketFactory
import org.apache.http.conn.ssl.TrustStrategy
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.ssl.SSLContextBuilder
import org.apache.http.util.EntityUtils

import java.security.cert.CertificateException
import java.security.cert.X509Certificate

List<String> sendRequest(String url, String method, String body) {


    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(body, "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();

    String req = "REQUEST:" + "\n" + request.getRequestLine() + "\n" + "Headers: " +
            request.getAllHeaders() + "\n" + EntityUtils.toString(entity) + "\n";

    def builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    });
    def trustAllFactory = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());

    HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->

        httpClient.execute(request).withCloseable { response ->

            String res = "RESPONSE:" + "\n" + response.getStatusLine() + "\n" + "Headers: " +
                    response.getAllHeaders() + "\n" +
                    (response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "") + "\n";

            System.out.println(req + "\n" + res);

            return Arrays.asList(req, res);
        }
    }
}

List test1 = sendRequest("https://testserver.com", "POST", "");
println(Arrays.toString(test1));

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • This worked like a charm. Thank you so much. I needed to use JSR223Sampler to send Multipart form-data, changing the image on the fly. I couldn't acheive this via HTTPSamplerProxy – user1829449 Jun 09 '20 at 20:44