Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add checkbox to enable/disable params in HTTP Sampler #6291

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
16 changes: 15 additions & 1 deletion src/core/src/main/java/org/apache/jmeter/config/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.iterators.FilterIterator;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jmeter.testelement.schema.PropertiesAccessor;
import org.apiguardian.api.API;

/**
* A set of Argument objects.
Expand Down Expand Up @@ -100,7 +102,7 @@ public Map<String, String> getArgumentsAsMap() {
// that this element's values prevail over defaults provided by
// configuration
// elements:
if (!argMap.containsKey(arg.getName())) {
if (!argMap.containsKey(arg.getName()) && arg.isEnabled()) {
argMap.put(arg.getName(), arg.getValue());
}
}
Expand Down Expand Up @@ -173,6 +175,18 @@ public PropertyIterator iterator() {
return getArguments().iterator();
}

/**
* Returns the list of enabled arguments.
* @return the list of enabled arguments
*/
@API(since = "5.6", status = API.Status.EXPERIMENTAL)
public Iterable<JMeterProperty> getEnabledArguments() {
return () -> new FilterIterator<>(iterator(), property -> {
Object value = property.getObjectValue();
return value instanceof Argument && ((Argument) value).isEnabled();
});
}

/**
* Create a string representation of the arguments.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static String computePostBody(Arguments arguments) {
*/
private static String computePostBody(Arguments arguments, boolean crlfToLF) {
StringBuilder postBody = new StringBuilder();
for (JMeterProperty argument : arguments) {
for (JMeterProperty argument : arguments.getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) argument.getObjectValue();
String value = arg.getValue();
if (crlfToLF) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
/**
* A GUI panel allowing the user to enter HTTP Parameters.
* These have names and values, as well as check-boxes to determine whether or not to
* include the "=" sign in the output and whether or not to encode the output.
* include the "=" sign in the output and whether or not to encode the output and
* whether or not to enable them.
*/
public class HTTPArgumentsPanel extends ArgumentsPanel {

private static final long serialVersionUID = 240L;

private static final String ENABLE = "enable"; //$NON-NLS-1$

private static final String ENCODE_OR_NOT = "encode?"; //$NON-NLS-1$

private static final String INCLUDE_EQUALS = "include_equals"; //$NON-NLS-1$
Expand All @@ -60,21 +63,23 @@ public class HTTPArgumentsPanel extends ArgumentsPanel {
@Override
protected void initializeTableModel() {
tableModel = new ObjectTableModel(new String[] {
ArgumentsPanel.COLUMN_RESOURCE_NAMES_0, ArgumentsPanel.COLUMN_RESOURCE_NAMES_1, ENCODE_OR_NOT, CONTENT_TYPE, INCLUDE_EQUALS },
ENABLE, ArgumentsPanel.COLUMN_RESOURCE_NAMES_0, ArgumentsPanel.COLUMN_RESOURCE_NAMES_1, ENCODE_OR_NOT, CONTENT_TYPE, INCLUDE_EQUALS },
HTTPArgument.class,
new Functor[] {
new Functor("isEnabled"), //$NON-NLS-1$
new Functor("getName"), //$NON-NLS-1$
new Functor("getValue"), //$NON-NLS-1$
new Functor("isAlwaysEncoded"), //$NON-NLS-1$
new Functor("getContentType"), //$NON-NLS-1$
new Functor("isUseEquals") }, //$NON-NLS-1$
new Functor[] {
new Functor("setEnabled"), //$NON-NLS-1$
new Functor("setName"), //$NON-NLS-1$
new Functor("setValue"), //$NON-NLS-1$
new Functor("setAlwaysEncoded"), //$NON-NLS-1$
new Functor("setContentType"),
new Functor("setUseEquals")}, //$NON-NLS-1$
new Class[] {String.class, String.class, Boolean.class, String.class, Boolean.class });
new Class[] {Boolean.class, String.class, String.class, Boolean.class, String.class, Boolean.class });
}

public static boolean testFunctors(){
Expand All @@ -85,6 +90,7 @@ public static boolean testFunctors(){

@Override
protected void sizeColumns(JTable table) {
GuiUtils.fixSize(table.getColumn(ENABLE), table);
GuiUtils.fixSize(table.getColumn(INCLUDE_EQUALS), table);
GuiUtils.fixSize(table.getColumn(ENCODE_OR_NOT), table);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private String setConnectionHeaders(URL url, String host, String method)
setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (JMeterProperty arg : getArguments()) {
for (JMeterProperty arg : getArguments().getEnabledArguments()) {
if (first) {
first = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
Expand Down Expand Up @@ -1571,7 +1570,7 @@ protected String setupHttpEntityEnclosingRequestData(HttpEntityEnclosingRequestB
}
// Create the parts
// Add any parameters
for (JMeterProperty jMeterProperty : getArguments()) {
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
String parameterName = arg.getName();
if (arg.isSkippable(parameterName)) {
Expand Down Expand Up @@ -1653,7 +1652,7 @@ else if(ADD_CONTENT_TYPE_TO_POST_IF_MISSING) {

// Just append all the parameter values, and use that as the post body
StringBuilder postBody = new StringBuilder();
for (JMeterProperty jMeterProperty : getArguments()) {
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
postBody.append(arg.getEncodedValue(contentEncoding));
}
Expand Down Expand Up @@ -1796,10 +1795,9 @@ else if(getSendParameterValuesAsPostBody()) {
private UrlEncodedFormEntity createUrlEncodedFormEntity(final String urlContentEncoding) throws UnsupportedEncodingException {
// It is a normal request, with parameter names and values
// Add the parameters
PropertyIterator args = getArguments().iterator();
List<NameValuePair> nvps = new ArrayList<>();
while (args.hasNext()) {
HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
for (JMeterProperty jMeterProperty: getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
// The HTTPClient always urlencodes both name and value,
// so if the argument is already encoded, we have to decode
// it before adding it to the post request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import org.apache.jmeter.testelement.ThreadListener;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.schema.PropertiesAccessor;
import org.apache.jmeter.testelement.schema.PropertyDescriptor;
import org.apache.jmeter.threads.JMeterContext;
Expand Down Expand Up @@ -409,7 +408,7 @@ public boolean getSendParameterValuesAsPostBody() {
return true;
} else {
boolean hasArguments = false;
for (JMeterProperty jMeterProperty : getArguments()) {
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
hasArguments = true;
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
if (arg.getName() != null && !arg.getName().isEmpty()) {
Expand Down Expand Up @@ -1155,9 +1154,10 @@ public String getQueryString() {
*/
public String getQueryString(final String contentEncoding) {

CollectionProperty arguments = getArguments().getArguments();
Arguments args = getArguments();
Iterator<JMeterProperty> iter = args.getEnabledArguments().iterator();
// Optimisation : avoid building useless objects if empty arguments
if(arguments.isEmpty()) {
if (!iter.hasNext()) {
return "";
}
String lContentEncoding = contentEncoding;
Expand All @@ -1167,8 +1167,7 @@ public String getQueryString(final String contentEncoding) {
lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
}

StringBuilder buf = new StringBuilder(arguments.size() * 15);
PropertyIterator iter = arguments.iterator();
StringBuilder buf = new StringBuilder(args.getArgumentCount() * 15);
boolean first = true;
while (iter.hasNext()) {
HTTPArgument item = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(bos, contentEncoding);
// Add any parameters
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
String parameterName = arg.getName();
if (arg.isSkippable(parameterName)) {
Expand Down Expand Up @@ -299,7 +299,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws

// Just append all the parameter values, and use that as the post body
StringBuilder postBodyBuffer = new StringBuilder();
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we should not filter "empty" arguments from here.

Frankly, I do not understand the reason why "empty named" (.isSkippable(...)) arguments are included only in certain cases.

HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ else if(sampler.getSendParameterValuesAsPostBody()) {

// Just append all the parameter values, and use that as the put body
StringBuilder putBodyBuffer = new StringBuilder();
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
putBodyBuffer.append(arg.getEncodedValue(contentEncoding));
}
Expand Down
Loading