gpt4 book ai didi

java - JMeter - 在 HTTPSampler 中以编程方式添加自定义 HTTP header

转载 作者:行者123 更新时间:2023-11-30 10:34:33 25 4
gpt4 key购买 nike

我在设置 JMeter 测试计划时以编程方式将自定义 header 添加到 HTTPSampler。

请引用以下代码段:

HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Foo", "Joe"));
sampler.setHeaderManager(headerManager);`

这里的问题是这个自定义 header 没有被发送到服务器。

这里有什么我遗漏的吗?

最佳答案

这样不行,你需要修改你的代码来添加 HeaderManager HashTree到 HTTPSamplerProxy 哈希树。之后你需要添加这个 HTTPSamplerProxy到线程组,比如:

HashTree httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);

testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpRequestTree);

完整代码,以防万一:

// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// Create Header Manager
HeaderManager manager = new HeaderManager();
manager.add(new Header("Foo", "Joe"));
manager.setName(JMeterUtils.getResString("header_manager_title")); // $NON-NLS-1$
manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

// HTTP Sampler - open example.com
HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
httpRequest.setDomain("example.com");
httpRequest.setPort(80);
httpRequest.setPath("/");
httpRequest.setMethod("GET");
httpRequest.setName("Open example.com");
httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpRequest.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

// HTTP Request Sampler and Header Manager
HashTree httpRequestTree = new HashTree();
httpRequestTree.add(httpRequest, manager);

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpRequestTree);

查看 Five Ways To Launch a JMeter Test without Using the JMeter GUI有关以编程方式构建 JMeter 测试计划的更多信息。

关于java - JMeter - 在 HTTPSampler 中以编程方式添加自定义 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41605836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com