Search in sources :

Example 6 with ValueReplacer

use of org.apache.jmeter.engine.util.ValueReplacer in project jmeter by apache.

the class TestHTTPSamplersAgainstHttpMirrorServer method testPostRequest_BodyFromParameterValues.

private void testPostRequest_BodyFromParameterValues(int samplerType, String samplerDefaultEncoding) throws Exception {
    // ensure only values are used
    final String titleField = "";
    String titleValue = "mytitle";
    // ensure only values are used
    final String descriptionField = "";
    String descriptionValue = "mydescription";
    // Test sending data with default encoding
    HTTPSamplerBase sampler = createHttpSampler(samplerType);
    String contentEncoding = "";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    HTTPSampleResult res = executeSampler(sampler);
    String expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as ISO-8859-1
    sampler = createHttpSampler(samplerType);
    contentEncoding = ISO_8859_1;
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitleœ₡ĕÅ";
    descriptionValue = "mydescriptionœ₡ĕÅ";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with values that will change when urlencoded
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle/=";
    descriptionValue = "mydescription   /\\";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with values that will change when urlencoded, and where
    // we tell the sampler to urlencode the parameter value
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle/=";
    descriptionValue = "mydescription   /\\";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
    res = executeSampler(sampler);
    expectedPostBody = URLEncoder.encode(titleValue + descriptionValue, contentEncoding);
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with values that have been urlencoded
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle%2F%3D";
    descriptionValue = "mydescription+++%2F%5C";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with values that have been urlencoded, and
    // where we tell the sampler to urlencode the parameter values
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle%2F%3D";
    descriptionValue = "mydescription+++%2F%5C";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
    descriptionValue = "mydescription";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, with + as part of the value,
    // where the value is set in sampler as not urluencoded, but the 
    // isalwaysencoded flag of the argument is set to false.
    // This mimics the HTTPConstants.addNonEncodedArgument, which the
    // Proxy server calls in some cases
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle++";
    descriptionValue = "mydescription+";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    res = executeSampler(sampler);
    expectedPostBody = titleValue + descriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
    // Test sending data as UTF-8, where user defined variables are used
    // to set the value for form data
    JMeterUtils.setLocale(Locale.ENGLISH);
    TestPlan testPlan = new TestPlan();
    JMeterVariables vars = new JMeterVariables();
    vars.put("title_prefix", "a testÅ");
    vars.put("description_suffix", "the_end");
    JMeterContextService.getContext().setVariables(vars);
    JMeterContextService.getContext().setSamplingStarted(true);
    ValueReplacer replacer = new ValueReplacer();
    replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "${title_prefix}mytitleœ₡ĕÅ";
    descriptionValue = "mydescriptionœ₡ĕÅ${description_suffix}";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
    ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
    // Replace the variables in the sampler
    replacer.replaceValues(sampler);
    res = executeSampler(sampler);
    String expectedTitleValue = "a testÅmytitleœ₡ĕÅ";
    String expectedDescriptionValue = "mydescriptionœ₡ĕÅthe_end";
    expectedPostBody = expectedTitleValue + expectedDescriptionValue;
    checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) TestPlan(org.apache.jmeter.testelement.TestPlan) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer)

Example 7 with ValueReplacer

use of org.apache.jmeter.engine.util.ValueReplacer in project jmeter by apache.

the class ProxyControlGui method startProxy.

private void startProxy() {
    ValueReplacer replacer = GuiPackage.getInstance().getReplacer();
    modifyTestElement(model);
    TreeNodeWrapper treeNodeWrapper = (TreeNodeWrapper) targetNodesModel.getSelectedItem();
    if (JMeterUtils.getResString("use_recording_controller").equals(treeNodeWrapper.getLabel())) {
        JMeterTreeNode targetNode = model.findTargetControllerNode();
        if (targetNode == null || !(targetNode.getTestElement() instanceof RecordingController)) {
            JOptionPane.showMessageDialog(this, // $NON-NLS-1$
            JMeterUtils.getResString("proxy_cl_wrong_target_cl"), // $NON-NLS-1$
            JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
            return;
        }
    }
    // Proxy can take some while to start up; show a waiting cursor
    Cursor cursor = getCursor();
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    // TODO somehow show progress
    try {
        replacer.replaceValues(model);
        model.startProxy();
        start.setEnabled(false);
        stop.setEnabled(true);
        restart.setEnabled(false);
        if (ProxyControl.isDynamicMode()) {
            String[] details = model.getCertificateDetails();
            StringBuilder sb = new StringBuilder();
            sb.append("<html>");
            // $NON-NLS-1$
            sb.append(JMeterUtils.getResString("proxy_daemon_msg_rootca_cert")).append("&nbsp;<b>").append(KeyToolUtils.ROOT_CACERT_CRT_PFX).append("</b>&nbsp;").append(JMeterUtils.getResString("proxy_daemon_msg_created_in_bin"));
            // $NON-NLS-1$
            sb.append("<br>").append(JMeterUtils.getResString("proxy_daemon_msg_install_as_in_doc"));
            sb.append("<br><b>").append(MessageFormat.format(JMeterUtils.getResString("proxy_daemon_msg_check_expiration"), // $NON-NLS-1$
            new Object[] { ProxyControl.CERT_VALIDITY })).append("</b><br>");
            sb.append("<br>").append(JMeterUtils.getResString("proxy_daemon_msg_check_details")).append(// $NON-NLS-1$
            "<ul>");
            for (String detail : details) {
                sb.append("<li>").append(detail).append("</li>");
            }
            sb.append("</ul>").append("</html>");
            JOptionPane.showMessageDialog(this, sb.toString(), // $NON-NLS-1$
            JMeterUtils.getResString("proxy_daemon_msg_rootca_cert") + SPACE + KeyToolUtils.ROOT_CACERT_CRT_PFX + SPACE + // $NON-NLS-1$
            JMeterUtils.getResString("proxy_daemon_msg_created_in_bin"), JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (InvalidVariableException e) {
        JOptionPane.showMessageDialog(this, // $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.getResString("invalid_variables") + ": " + e.getMessage(), // $NON-NLS-1$
        JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
    } catch (BindException e) {
        JOptionPane.showMessageDialog(this, // $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.getResString("proxy_daemon_bind_error") + ": " + e.getMessage(), // $NON-NLS-1$
        JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, // $NON-NLS-1$ $NON-NLS-2$
        JMeterUtils.getResString("proxy_daemon_error") + ": " + e.getMessage(), // $NON-NLS-1$
        JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE);
    } finally {
        setCursor(cursor);
    }
}
Also used : InvalidVariableException(org.apache.jmeter.functions.InvalidVariableException) TreeNodeWrapper(org.apache.jmeter.control.gui.TreeNodeWrapper) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) BindException(java.net.BindException) IOException(java.io.IOException) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) Cursor(java.awt.Cursor) RecordingController(org.apache.jmeter.protocol.http.control.RecordingController)

Example 8 with ValueReplacer

use of org.apache.jmeter.engine.util.ValueReplacer in project jmeter by apache.

the class ProxyControl method replaceValues.

/**
     * Scan all test elements passed in for values matching the value of any of
     * the variables in any of the variable-holding elements in the collection.
     *
     * @param sampler
     *            A TestElement to replace values on
     * @param configs
     *            More TestElements to replace values on
     * @param variables
     *            Collection of Arguments to use to do the replacement, ordered
     *            by ascending priority.
     */
private void replaceValues(TestElement sampler, TestElement[] configs, Collection<Arguments> variables) {
    // Build the replacer from all the variables in the collection:
    ValueReplacer replacer = new ValueReplacer();
    for (Arguments variable : variables) {
        final Map<String, String> map = variable.getArgumentsAsMap();
        // Drop any empty values (Bug 45199)
        map.values().removeIf(""::equals);
        replacer.addVariables(map);
    }
    try {
        boolean cachedRegexpMatch = regexMatch;
        replacer.reverseReplace(sampler, cachedRegexpMatch);
        for (TestElement config : configs) {
            if (config != null) {
                replacer.reverseReplace(config, cachedRegexpMatch);
            }
        }
    } catch (InvalidVariableException e) {
        log.warn("Invalid variables included for replacement into recorded " + "sample", e);
    }
}
Also used : InvalidVariableException(org.apache.jmeter.functions.InvalidVariableException) Arguments(org.apache.jmeter.config.Arguments) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement)

Example 9 with ValueReplacer

use of org.apache.jmeter.engine.util.ValueReplacer in project jmeter by apache.

the class TestWhileController method testVariable1.

/*
         * Generic Controller
         * - before
         * - While Controller ${VAR}
         * - - one
         * - - two
         * - - Simple Controller
         * - - - three
         * - - - four
         * - after
         */
@Test
public void testVariable1() throws Exception {
    GenericController controller = new GenericController();
    WhileController while_cont = new WhileController();
    setLastSampleStatus(false);
    while_cont.setCondition("${VAR}");
    jmvars.put("VAR", "");
    ValueReplacer vr = new ValueReplacer();
    vr.replaceValues(while_cont);
    setRunning(while_cont);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(while_cont);
    while_cont.addTestElement(new TestSampler("one"));
    while_cont.addTestElement(new TestSampler("two"));
    GenericController simple = new GenericController();
    while_cont.addTestElement(simple);
    simple.addTestElement(new TestSampler("three"));
    simple.addTestElement(new TestSampler("four"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        assertEquals("Loop: " + i, "one", nextName(controller));
        assertEquals("Loop: " + i, "two", nextName(controller));
        assertEquals("Loop: " + i, "three", nextName(controller));
        assertEquals("Loop: " + i, "four", nextName(controller));
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
    // Should not enter the loop
    jmvars.put("VAR", "LAST");
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
    jmvars.put("VAR", "");
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        if (i == 1) {
            assertEquals("Loop: " + i, "one", nextName(controller));
            assertEquals("Loop: " + i, "two", nextName(controller));
            assertEquals("Loop: " + i, "three", nextName(controller));
            // Should not enter the loop next time
            jmvars.put("VAR", "LAST");
            assertEquals("Loop: " + i, "four", nextName(controller));
        }
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
}
Also used : ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Aggregations

ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)9 TestPlan (org.apache.jmeter.testelement.TestPlan)5 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)4 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)3 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)2 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)2 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)2 TestElement (org.apache.jmeter.testelement.TestElement)2 Test (org.junit.Test)2 Cursor (java.awt.Cursor)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 URL (java.net.URL)1 Arguments (org.apache.jmeter.config.Arguments)1 TreeNodeWrapper (org.apache.jmeter.control.gui.TreeNodeWrapper)1 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)1 RecordingController (org.apache.jmeter.protocol.http.control.RecordingController)1 Timer (org.apache.jmeter.timers.Timer)1