Skip to content

Commit 8cf1e4a

Browse files
pochopspvlsi
andcommitted
feat: allow to enable/disable individual arguments in HTTP Sampler
This property is set by the user for each HTTP parameter by clicking on the "Enable" column put on the very left of each parameter row. Fixes #5466 Co-authored-by: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
1 parent 975e51c commit 8cf1e4a

14 files changed

Lines changed: 342 additions & 73 deletions

File tree

src/core/src/main/java/org/apache/jmeter/config/Arguments.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.apache.commons.collections4.iterators.FilterIterator;
2627
import org.apache.jmeter.testelement.property.CollectionProperty;
2728
import org.apache.jmeter.testelement.property.JMeterProperty;
2829
import org.apache.jmeter.testelement.property.PropertyIterator;
2930
import org.apache.jmeter.testelement.property.TestElementProperty;
3031
import org.apache.jmeter.testelement.schema.PropertiesAccessor;
32+
import org.apiguardian.api.API;
3133

3234
/**
3335
* A set of Argument objects.
@@ -100,7 +102,7 @@ public Map<String, String> getArgumentsAsMap() {
100102
// that this element's values prevail over defaults provided by
101103
// configuration
102104
// elements:
103-
if (!argMap.containsKey(arg.getName())) {
105+
if (!argMap.containsKey(arg.getName()) && arg.isEnabled()) {
104106
argMap.put(arg.getName(), arg.getValue());
105107
}
106108
}
@@ -173,6 +175,18 @@ public PropertyIterator iterator() {
173175
return getArguments().iterator();
174176
}
175177

178+
/**
179+
* Returns the list of enabled arguments.
180+
* @return the list of enabled arguments
181+
*/
182+
@API(since = "5.6", status = API.Status.EXPERIMENTAL)
183+
public Iterable<JMeterProperty> getEnabledArguments() {
184+
return () -> new FilterIterator<>(iterator(), property -> {
185+
Object value = property.getObjectValue();
186+
return value instanceof Argument argument && argument.isEnabled();
187+
});
188+
}
189+
176190
/**
177191
* Create a string representation of the arguments.
178192
*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.jmeter.treebuilder
19+
20+
import org.apache.jmeter.control.LoopController
21+
import org.apache.jmeter.threads.ThreadGroup
22+
23+
fun TreeBuilder.oneRequest(body: ThreadGroup.() -> Unit) {
24+
ThreadGroup::class {
25+
numThreads = 1
26+
rampUp = 0
27+
setSamplerController(
28+
LoopController().apply {
29+
loops = 1
30+
}
31+
)
32+
body()
33+
}
34+
}

src/functions/src/test/kotlin/org/apache/jmeter/timers/ConstantThroughputTimerKtTest.kt

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,15 @@
1717

1818
package org.apache.jmeter.timers
1919

20-
import org.apache.jmeter.control.LoopController
2120
import org.apache.jmeter.junit.JMeterTestCase
2221
import org.apache.jmeter.sampler.DebugSampler
2322
import org.apache.jmeter.test.assertions.executePlanAndCollectEvents
24-
import org.apache.jmeter.threads.ThreadGroup
25-
import org.apache.jmeter.treebuilder.TreeBuilder
23+
import org.apache.jmeter.treebuilder.oneRequest
2624
import org.junit.jupiter.api.Assertions.assertEquals
2725
import org.junit.jupiter.api.Test
2826
import kotlin.time.Duration.Companion.seconds
2927

3028
class ConstantThroughputTimerKtTest : JMeterTestCase() {
31-
fun TreeBuilder.oneRequest(body: ThreadGroup.() -> Unit) {
32-
ThreadGroup::class {
33-
numThreads = 1
34-
rampUp = 0
35-
setSamplerController(
36-
LoopController().apply {
37-
loops = 1
38-
}
39-
)
40-
body()
41-
}
42-
}
43-
4429
@Test
4530
fun `throughput as variable`() {
4631
val events = executePlanAndCollectEvents(5.seconds) {

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/config/gui/UrlConfigGui.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private static String computePostBody(Arguments arguments) {
261261
*/
262262
private static String computePostBody(Arguments arguments, boolean crlfToLF) {
263263
StringBuilder postBody = new StringBuilder();
264-
for (JMeterProperty argument : arguments) {
264+
for (JMeterProperty argument : arguments.getEnabledArguments()) {
265265
HTTPArgument arg = (HTTPArgument) argument.getObjectValue();
266266
String value = arg.getValue();
267267
if (crlfToLF) {

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/gui/HTTPArgumentsPanel.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@
3939
/**
4040
* A GUI panel allowing the user to enter HTTP Parameters.
4141
* These have names and values, as well as check-boxes to determine whether or not to
42-
* include the "=" sign in the output and whether or not to encode the output.
42+
* include the "=" sign in the output and whether or not to encode the output and
43+
* whether or not to enable them.
4344
*/
4445
public class HTTPArgumentsPanel extends ArgumentsPanel {
4546

4647
private static final long serialVersionUID = 240L;
4748

49+
private static final String ENABLE = "enable"; //$NON-NLS-1$
50+
4851
private static final String ENCODE_OR_NOT = "encode?"; //$NON-NLS-1$
4952

5053
private static final String INCLUDE_EQUALS = "include_equals"; //$NON-NLS-1$
@@ -60,21 +63,23 @@ public class HTTPArgumentsPanel extends ArgumentsPanel {
6063
@Override
6164
protected void initializeTableModel() {
6265
tableModel = new ObjectTableModel(new String[] {
63-
ArgumentsPanel.COLUMN_RESOURCE_NAMES_0, ArgumentsPanel.COLUMN_RESOURCE_NAMES_1, ENCODE_OR_NOT, CONTENT_TYPE, INCLUDE_EQUALS },
66+
ENABLE, ArgumentsPanel.COLUMN_RESOURCE_NAMES_0, ArgumentsPanel.COLUMN_RESOURCE_NAMES_1, ENCODE_OR_NOT, CONTENT_TYPE, INCLUDE_EQUALS },
6467
HTTPArgument.class,
6568
new Functor[] {
69+
new Functor("isEnabled"), //$NON-NLS-1$
6670
new Functor("getName"), //$NON-NLS-1$
6771
new Functor("getValue"), //$NON-NLS-1$
6872
new Functor("isAlwaysEncoded"), //$NON-NLS-1$
6973
new Functor("getContentType"), //$NON-NLS-1$
7074
new Functor("isUseEquals") }, //$NON-NLS-1$
7175
new Functor[] {
76+
new Functor("setEnabled"), //$NON-NLS-1$
7277
new Functor("setName"), //$NON-NLS-1$
7378
new Functor("setValue"), //$NON-NLS-1$
7479
new Functor("setAlwaysEncoded"), //$NON-NLS-1$
7580
new Functor("setContentType"),
7681
new Functor("setUseEquals")}, //$NON-NLS-1$
77-
new Class[] {String.class, String.class, Boolean.class, String.class, Boolean.class });
82+
new Class[] {Boolean.class, String.class, String.class, Boolean.class, String.class, Boolean.class });
7883
}
7984

8085
public static boolean testFunctors(){
@@ -85,6 +90,7 @@ public static boolean testFunctors(){
8590

8691
@Override
8792
protected void sizeColumns(JTable table) {
93+
GuiUtils.fixSize(table.getColumn(ENABLE), table);
8894
GuiUtils.fixSize(table.getColumn(INCLUDE_EQUALS), table);
8995
GuiUtils.fixSize(table.getColumn(ENCODE_OR_NOT), table);
9096
}

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/AjpSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private String setConnectionHeaders(URL url, String host, String method)
279279
setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
280280
StringBuilder sb = new StringBuilder();
281281
boolean first = true;
282-
for (JMeterProperty arg : getArguments()) {
282+
for (JMeterProperty arg : getArguments().getEnabledArguments()) {
283283
if (first) {
284284
first = false;
285285
} else {

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPHC4Impl.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
import org.apache.jmeter.services.FileServer;
159159
import org.apache.jmeter.testelement.property.CollectionProperty;
160160
import org.apache.jmeter.testelement.property.JMeterProperty;
161-
import org.apache.jmeter.testelement.property.PropertyIterator;
162161
import org.apache.jmeter.threads.JMeterContextService;
163162
import org.apache.jmeter.threads.JMeterVariables;
164163
import org.apache.jmeter.util.JMeterUtils;
@@ -1562,7 +1561,7 @@ protected String setupHttpEntityEnclosingRequestData(HttpEntityEnclosingRequestB
15621561
}
15631562
// Create the parts
15641563
// Add any parameters
1565-
for (JMeterProperty jMeterProperty : getArguments()) {
1564+
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
15661565
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
15671566
String parameterName = arg.getName();
15681567
if (arg.isSkippable(parameterName)) {
@@ -1644,7 +1643,7 @@ else if(ADD_CONTENT_TYPE_TO_POST_IF_MISSING) {
16441643

16451644
// Just append all the parameter values, and use that as the post body
16461645
StringBuilder postBody = new StringBuilder();
1647-
for (JMeterProperty jMeterProperty : getArguments()) {
1646+
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
16481647
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
16491648
postBody.append(arg.getEncodedValue(contentEncoding));
16501649
}
@@ -1787,10 +1786,9 @@ else if(getSendParameterValuesAsPostBody()) {
17871786
private UrlEncodedFormEntity createUrlEncodedFormEntity(final String urlContentEncoding) throws UnsupportedEncodingException {
17881787
// It is a normal request, with parameter names and values
17891788
// Add the parameters
1790-
PropertyIterator args = getArguments().iterator();
17911789
List<NameValuePair> nvps = new ArrayList<>();
1792-
while (args.hasNext()) {
1793-
HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
1790+
for (JMeterProperty jMeterProperty: getArguments().getEnabledArguments()) {
1791+
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
17941792
// The HTTPClient always urlencodes both name and value,
17951793
// so if the argument is already encoded, we have to decode
17961794
// it before adding it to the post request

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import org.apache.jmeter.testelement.ThreadListener;
8080
import org.apache.jmeter.testelement.property.CollectionProperty;
8181
import org.apache.jmeter.testelement.property.JMeterProperty;
82-
import org.apache.jmeter.testelement.property.PropertyIterator;
8382
import org.apache.jmeter.testelement.schema.PropertiesAccessor;
8483
import org.apache.jmeter.testelement.schema.PropertyDescriptor;
8584
import org.apache.jmeter.threads.JMeterContext;
@@ -410,7 +409,7 @@ public boolean getSendParameterValuesAsPostBody() {
410409
return true;
411410
} else {
412411
boolean hasArguments = false;
413-
for (JMeterProperty jMeterProperty : getArguments()) {
412+
for (JMeterProperty jMeterProperty : getArguments().getEnabledArguments()) {
414413
hasArguments = true;
415414
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
416415
if (arg.getName() != null && !arg.getName().isEmpty()) {
@@ -1156,9 +1155,10 @@ public String getQueryString() {
11561155
*/
11571156
public String getQueryString(final String contentEncoding) {
11581157

1159-
CollectionProperty arguments = getArguments().getArguments();
1158+
Arguments args = getArguments();
1159+
Iterator<JMeterProperty> iter = args.getEnabledArguments().iterator();
11601160
// Optimisation : avoid building useless objects if empty arguments
1161-
if(arguments.isEmpty()) {
1161+
if (!iter.hasNext()) {
11621162
return "";
11631163
}
11641164
String lContentEncoding = contentEncoding;
@@ -1168,8 +1168,7 @@ public String getQueryString(final String contentEncoding) {
11681168
lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
11691169
}
11701170

1171-
StringBuilder buf = new StringBuilder(arguments.size() * 15);
1172-
PropertyIterator iter = arguments.iterator();
1171+
StringBuilder buf = new StringBuilder(args.getArgumentCount() * 15);
11731172
boolean first = true;
11741173
while (iter.hasNext()) {
11751174
HTTPArgument item = null;

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PostWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws
191191
ByteArrayOutputStream bos = new ByteArrayOutputStream();
192192
OutputStreamWriter osw = new OutputStreamWriter(bos, contentEncoding);
193193
// Add any parameters
194-
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
194+
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
195195
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
196196
String parameterName = arg.getName();
197197
if (arg.isSkippable(parameterName)) {
@@ -300,7 +300,7 @@ public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws
300300

301301
// Just append all the parameter values, and use that as the post body
302302
StringBuilder postBodyBuffer = new StringBuilder();
303-
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
303+
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
304304
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
305305
postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
306306
}

src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/PutWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ else if(sampler.getSendParameterValuesAsPostBody()) {
8585

8686
// Just append all the parameter values, and use that as the put body
8787
StringBuilder putBodyBuffer = new StringBuilder();
88-
for (JMeterProperty jMeterProperty : sampler.getArguments()) {
88+
for (JMeterProperty jMeterProperty : sampler.getArguments().getEnabledArguments()) {
8989
HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
9090
putBodyBuffer.append(arg.getEncodedValue(contentEncoding));
9191
}

0 commit comments

Comments
 (0)