Skip to content

Commit

Permalink
Fixes reported issue with headers inheritance when using jsr223Sample…
Browse files Browse the repository at this point in the history
…r with a script class
  • Loading branch information
rabelenda-abstracta committed Nov 9, 2023
1 parent 8cfe783 commit 968d96f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.lang.reflect.Method;
import java.util.List;
import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.config.gui.SimpleConfigGui;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.engine.event.LoopIterationListener;
import org.apache.jmeter.engine.util.ConfigMergabilityIndicator;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.AbstractTestElement;
Expand Down Expand Up @@ -84,7 +87,7 @@ protected TestElement buildTestElement() {

public abstract static class Jsr223DslLambdaTestElement<V extends Jsr223ScriptVars> extends
AbstractTestElement implements TestBean, ThreadListener, TestIterationListener,
LoopIterationListener {
LoopIterationListener, ConfigMergabilityIndicator {

private static final String SCRIPT_ID_PROP = "SCRIPT_ID";
private Jsr223Script<V> script;
Expand Down Expand Up @@ -145,6 +148,13 @@ public void threadFinished() {
}
}

// This is required to avoid the issue https://github.com/abstracta/jmeter-java-dsl/issues/240
@Override
public boolean applies(ConfigTestElement configElement) {
return SimpleConfigGui.class.getName()
.equals(configElement.getProperty(TestElement.GUI_CLASS).getStringValue());
}

}

protected interface Jsr223Script<V extends Jsr223ScriptVars> extends DslScript<V, Void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package us.abstracta.jmeter.javadsl.java;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static us.abstracta.jmeter.javadsl.JmeterDsl.forLoopController;
import static us.abstracta.jmeter.javadsl.JmeterDsl.httpHeaders;
import static us.abstracta.jmeter.javadsl.JmeterDsl.httpSampler;
import static us.abstracta.jmeter.javadsl.JmeterDsl.jsr223Sampler;
import static us.abstracta.jmeter.javadsl.JmeterDsl.jtlWriter;
import static us.abstracta.jmeter.javadsl.JmeterDsl.testPlan;
import static us.abstracta.jmeter.javadsl.JmeterDsl.testResource;
import static us.abstracta.jmeter.javadsl.JmeterDsl.threadGroup;
import static us.abstracta.jmeter.javadsl.JmeterDsl.transaction;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand All @@ -21,13 +28,14 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import us.abstracta.jmeter.javadsl.JmeterDslTest;
import us.abstracta.jmeter.javadsl.codegeneration.MethodCallBuilderTest;
import us.abstracta.jmeter.javadsl.core.DslTestPlan;
import us.abstracta.jmeter.javadsl.core.listeners.JtlWriter;
import us.abstracta.jmeter.javadsl.java.DslJsr223Sampler.SamplerScript;
import us.abstracta.jmeter.javadsl.java.DslJsr223Sampler.SamplerVars;

public class DslJsr223SamplerTest {
public class DslJsr223SamplerTest extends JmeterDslTest {

private static final String RESULTS_JTL = "results.jtl";

Expand Down Expand Up @@ -131,6 +139,25 @@ private Map<String, Integer> buildExpectedThreadCountsMap(int threadCount, int i
return ret;
}

@Test
public void shouldNotAlterHttpHeadersWhenSamplerAndHttpHeadersAndSampler() throws Exception {
String headerNamePrefix = "HEADER";
String header1Name = headerNamePrefix + "1";
String header2Name = headerNamePrefix + "2";
String headerValue = "VALUE";
testPlan(threadGroup(1, 1,
httpHeaders().header(header1Name, headerValue),
transaction("test",
httpHeaders().header(header2Name, headerValue),
jsr223Sampler(MySampler.class),
httpSampler(wiremockUri))
)
).run();
verify(getRequestedFor(urlEqualTo("/"))
.withHeader(header1Name, equalTo(headerValue))
.withHeader(header2Name, equalTo(headerValue)));
}

@Nested
public class CodeBuilderTest extends MethodCallBuilderTest {

Expand Down

0 comments on commit 968d96f

Please sign in to comment.