apache / apache/jmeter

Executed through hashtree, the headermanager does not take effect

Open
#5,670 1 comment 0 reactions 0 assignees View on GitHub
os: macOS P2
Dominant language
Java
Stars
9.5k
Forks
2.3k
Avg merge
1d 22h
Merged PRs (30d)
5

Description

**AmosChen** ([Bug 66070](https://bz.apache.org/bugzilla//show_bug.cgi?id=66070&redirect=false)):
code:

import lombok.extern.slf4j.Slf4j;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.threads.AbstractThreadGroup;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.junit.jupiter.api.Test;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.UUID;

/**
* @author AmosChen
* @date 2022/5/17 22:27
*/
@Slf4j
public class HttpBuilderTest {

static {
final String jmeterHome = "/Users/amos/Documents/apache-jmeter-5.4.3";
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.setLocale(Locale.CHINA);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
}

@Test
void build() throws IOException {

final String jmeterHome = "/Users/amos/Documents/apache-jmeter-5.4.3";
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.setLocale(Locale.CHINA);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");

// request
final HTTPSamplerProxy samplerProxy = new HTTPSamplerProxy();
final String address = "https://postman-echo.com/post";
final UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(address).build();
samplerProxy.setProtocol(uriComponents.getScheme());
samplerProxy.setPath(uriComponents.getPath());
samplerProxy.setFollowRedirects(true);
samplerProxy.setAutoRedirects(true);
samplerProxy.setMethod("POST");
samplerProxy.setContentEncoding("utf-8");
samplerProxy.setUseKeepAlive(false);
samplerProxy.setDoMultipart(false);
samplerProxy.setDomain(uriComponents.getHost());
// Java or HttpClient4(默认的)
// samplerProxy.setImplementation("Java");
samplerProxy.setConnectTimeout(String.valueOf(2000));
samplerProxy.setResponseTimeout(String.valueOf(2000));
// set body
samplerProxy.setPostBodyRaw(true);
final Arguments arguments = new Arguments();
arguments.setProperty(TestElement.GUI_CLASS, "HTTPArgumentsPanel");
final HTTPArgument httpArgument = new HTTPArgument();
httpArgument.setValue("{\"a\":\"123\"}");
httpArgument.setMetaData("=");
httpArgument.setAlwaysEncoded(false);
arguments.addArgument(httpArgument);
samplerProxy.setArguments(arguments);
samplerProxy.setName("HTTP Requests");
samplerProxy.setComment("HTTP");
samplerProxy.setProperty(TestElement.GUI_CLASS, "HttpTestSampleGui");
samplerProxy.setProperty(TestElement.TEST_CLASS, "HTTPSamplerProxy");
samplerProxy.setEnabled(true);

// 头信息
final HeaderManager headerManager = new HeaderManager();
// one header config
final Header header = new Header();
final String contentType = "application/json; charset=utf-8";
header.setProperty(new StringProperty("Header.name", "Content-Type"));
header.setProperty(new StringProperty("Header.value", contentType));
header.setEnabled(true);
// manager config
headerManager.setEnabled(true);
headerManager.add(header);
headerManager.setProperty(TestElement.GUI_CLASS, "HeaderPanel");
headerManager.setProperty(TestElement.TEST_CLASS, "HeaderManager");

final HashTree planTree = new HashTree();
final HashTree groupTree = new HashTree();
final HashTree httpTree = new HashTree();
final HashTree httpConfigTree = new HashTree();
httpConfigTree.add(headerManager);
httpTree.add(samplerProxy, httpConfigTree);
// 线程配置
final ThreadGroup group = new ThreadGroup();
group.setName("ThreadName");
group.setComment("Comments");
group.setNumThreads(1);
group.setRampUp(1);
group.setScheduler(false);
final LoopController controller = new LoopController();
controller.setProperty(TestElement.GUI_CLASS, "LoopControlPanel");
controller.setContinueForever(false);
controller.setLoops(1);
group.setSamplerController(controller);
group.setIsSameUserOnNextIteration(true);
group.setProperty(AbstractThreadGroup.ON_SAMPLE_ERROR, AbstractThreadGroup.ON_SAMPLE_ERROR_CONTINUE);
group.setProperty(TestElement.GUI_CLASS, "ThreadGroupGui");
group.setProperty(TestElement.TEST_CLASS, "ThreadGroup");
group.setProperty(ThreadGroup.DELAY, "");
group.setProperty(ThreadGroup.DURATION, "");
group.setEnabled(true);

groupTree.add(group, httpTree);

final TestPlan plan = new TestPlan("Plan Name");
plan.setFunctionalMode(false);
plan.setSerialized(false);
plan.setBasedir("");
plan.setTearDownOnShutdown(true);
plan.setComment("");
plan.setProperty(TestElement.GUI_CLASS, "TestPlanGui");
plan.setProperty(TestElement.TEST_CLASS, "TestPlan");
plan.setEnabled(true);

planTree.add(plan, groupTree);

log.info("====================================== tree running");
StandardJMeterEngine engine = new StandardJMeterEngine();
engine.configure(planTree);
engine.run();
log.info("====================================== jxm to tree running");
String name = UUID.randomUUID() + ".jmx";
final File file = new File("/Users/amos/Downloads", name);
final FileOutputStream outputStream = new FileOutputStream(file);
SaveService.saveTree(planTree, outputStream);

HashTree newHashTree = SaveService.loadTree(file);
StandardJMeterEngine newEngine = new StandardJMeterEngine();
newEngine.configure(newHashTree);
newEngine.run();
}
}

step:
1. set log level to debug
2. run this code.

Run hashTree log:
11:41:53.221 [ThreadName 1-1] DEBUG org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl - Headers in request before:[Connection: close]

Run hashTree after Convert to JMX log:
11:41:54.648 [ThreadName 1-1] DEBUG org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl - Headers in request before:[Connection: close, Content-Type: application/json; charset=utf-8]

Severity: major
OS: Mac OS X 10.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.