Skip to content

Commit

Permalink
Bug 65004 - HTTP(S) Test Script recorder computes wrong HTTP Request
Browse files Browse the repository at this point in the history
breaking the application
  • Loading branch information
pmouawad committed Dec 17, 2020
1 parent e58c557 commit acca046
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Expand Up @@ -55,6 +55,9 @@
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Default implementation that handles classical HTTP textual + Multipart requests
*/
Expand All @@ -69,6 +72,7 @@ public class DefaultSamplerCreator extends AbstractSamplerCreator {
private static final int SAMPLER_NAME_NAMING_MODE_SUFFIX = 2; // $NON-NLS-1$
private static final int SAMPLER_NAME_NAMING_MODE_FORMATTER = 3; // $NON_NLS-1$

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
*
*/
Expand Down Expand Up @@ -248,7 +252,8 @@ protected void computeFromPostBody(HTTPSamplerBase sampler,
// used when postData is pure xml (eg. an xml-rpc call) or for PUT
} else if (postData.trim().startsWith("<?")
|| HTTPConstants.PUT.equals(sampler.getMethod())
|| isPotentialXml(postData)) {
|| isPotentialXml(postData)
|| isPotentialJson(postData)) {
sampler.addNonEncodedArgument("", postData, "");
} else if (contentType == null ||
(contentType.startsWith(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED) &&
Expand Down Expand Up @@ -276,12 +281,29 @@ protected void computeFromPostBody(HTTPSamplerBase sampler,
}
}

/**
* Tries parsing to see if content is JSON
* @param postData String
* @return boolean
*/
public static boolean isPotentialJson(final String postData) throws IOException {
boolean valid = true;
try{
OBJECT_MAPPER.readTree(postData);
} catch(JsonProcessingException e){
valid = false;
}
log.debug("Is Post data {} JSON ? {}", postData, valid);
return valid;
}

/**
* Tries parsing to see if content is xml
* @param postData String
* @return boolean
*/
private static boolean isPotentialXml(String postData) {
boolean isXml;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Expand All @@ -292,10 +314,12 @@ private static boolean isPotentialXml(String postData) {
xmlReader.setContentHandler(detectionHandler);
xmlReader.setErrorHandler(detectionHandler);
xmlReader.parse(new InputSource(new StringReader(postData)));
return !detectionHandler.isErrorDetected();
isXml = !detectionHandler.isErrorDetected();
} catch (ParserConfigurationException | SAXException | IOException e) {
return false;
isXml = false;
}
log.debug("Is Post data {} XML ? {}", postData, isXml);
return isXml;
}

private static final class ErrorDetectionHandler extends DefaultHandler {
Expand Down
1 change: 1 addition & 0 deletions xdocs/changes.xml
Expand Up @@ -137,6 +137,7 @@ Summary
<ul>
<li><bug>64955</bug>Keystore password not reset on reload</li>
<li><bug>65002</bug>HTTP(S) Test Script recorder creates an invalid Basic authentication URL. Contributed by Ubik Load Pack (https://ubikloadpack.com)</li>
<li><bug>65004</bug>HTTP(S) Test Script recorder computes wrong HTTP Request breaking the application. Contributed by Ubik Load Pack (https://ubikloadpack.com)</li>
</ul>

<h3>Other Samplers</h3>
Expand Down

0 comments on commit acca046

Please sign in to comment.