Disabling HttpUnit script loading

Those who work with HttpUnit (for testing or development needs) might have faced issues with scripts (and its loading). This situation is more obvious if we are working on a third party web site which will be loading numerous script files from different domains (or sub-domains), sometimes thru a secured channel. The one possible exception is:

java.lang.RuntimeException: Error loading included script: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Even if we are setting the following,

HttpUnitOptions.setScriptingEnabled(false);
HttpUnitOptions.setExceptionsThrownOnScriptError(false);

it will not avoid the above situation as the problem is with the file itself (either an invalid URL or an invalid certificate).

One possible solution is to modify the HttpUnit code so that it can avoid loading the script itself. The right place will be com.meterware.httpunit.ParsedHTML.getScript(). Change the method body to:

private String getScript( Node scriptNode )
{
   String scriptLocation = NodeUtils.getNodeAttribute
      (scriptNode, "src", null);
   if (null != scriptLocation)
      System.out.println("Blocking script from: " +
         scriptLocation);

   return (null);
}

This is how the actual code look like:

private String getScript(Node scriptNode)
{
   String scriptLocation = NodeUtils.getNodeAttribute
      (scriptNode, "src", null);

   if (scriptLocation == null)
   {
      return NodeUtils.asText(scriptNode.getChildNodes());
   }
   else
   {
      try
      {
         return getIncludedScript(scriptLocation);
      }
      catch (IOException e)
      {
         throw new RuntimeException("Error loading included script: "
             + e);
      }
   }
   return (null);
}

We were working on a crawler where we used HttpUnit as a supporting API, and JavaScript was never an important factor for our process. And because of this tweak we were able to improve the performance by 4 times approx.

Note:
Be very careful while modifying the source code. And do it at your own risk.

Advertisements