2016-12-02 2 views
1

Ich versuche, HTTP-Post-Anfrage mit Json mit Jmeter API zu senden. Ich füge HeaderManager mit Content-Type-Header, aber es scheint nicht, wie ich erwarten zu arbeiten, weil ich die Anfrage mit Content-Type: application/x-www-form-urlencodedHinzufügen von Header programmgesteuert mit Jmeter Java-API

HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy(); 
    httpSamplerProxy.setDomain("localhost"); 
    httpSamplerProxy.setPort(8090); 
    httpSamplerProxy.setPath("/"); 
    httpSamplerProxy.setMethod("POST"); 
    httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", ""); 
    HeaderManager headerManager = new HeaderManager(); 
    headerManager.add(new Header("Content-type", "application/json")); 
    httpSamplerProxy.setHeaderManager(headerManager); 

bin immer Was mache ich falsch? Wie füge ich einen Header zu httpSampler hinzu?

+0

Mögliche doppelte von [JMeter ändern HTTP-Header während des Tests] (http://stackoverflow.com/questions/24542747/jmeter-alter-http-headers-during-test) –

Antwort

1

Sie haben mehrere Probleme mit dem Test:

  1. -API wird nicht empfohlen, einen JMeter Test schaffen als API daher ändern könnte Ihr Test brüchig werden kann und nicht Upgrade zur nächsten Version JMeter überleben. Sie sollten Tests mithilfe von JMeter GUI erstellen und sobald Sie fertig sind, haben Sie die Freiheit, sie mit jedem beliebigen Ansatz auszuführen.
  2. Sie sollten JMeter und die zu testende Anwendung nicht auf demselben Host ausführen, um gegenseitige Störungen zu vermeiden. JMeter-Tests können sehr sehr ressourcenintensiv sein.
  3. "Typ" Wort in der Name sollte Großbuchstaben, es zählt.

Wenn Sie die Art und Weise suchen noch programmatisch HTTP Header-Manager hinzuzufügen, wie gemäß einem der comments zum Five Ways To Launch a JMeter Test without Using the JMeter GUI sollten Sie es ein bisschen anders, nämlich tun:

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

// Create Header Manager 
HeaderManager manager = new HeaderManager(); 
manager.add(new Header("Content-Type", "application/json")); 
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 
HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy(); 
httpSamplerProxy.setDomain("localhost"); 
httpSamplerProxy.setPort(8090); 
httpSamplerProxy.setPath("/"); 
httpSamplerProxy.setMethod("POST"); 
httpSamplerProxy.setName("HTTP Request"); 
httpSamplerProxy.addEncodedArgument("Body Data", "{\"1\":\"2\"}", ""); 
httpSamplerProxy.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName()); 
httpSamplerProxy.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 requestHashTree = new HashTree(); 
requestHashTree.add(httpSamplerProxy, manager); 

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