Search in sources :

Example 1 with Request

use of org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request in project jmeter by apache.

the class ParseCurlCommandAction method parseCommands.

public List<Request> parseCommands(boolean isReadFromFile, List<String> commandsList) {
    List<Request> requests = new ArrayList<>();
    BasicCurlParser basicCurlParser = new BasicCurlParser();
    for (int i = 0; i < commandsList.size(); i++) {
        try {
            BasicCurlParser.Request q = basicCurlParser.parse(commandsList.get(i));
            requests.add(q);
            LOGGER.info("Parsed CURL command {} into {}", commandsList.get(i), q);
        } catch (IllegalArgumentException ie) {
            if (isReadFromFile) {
                int line = i + 1;
                LOGGER.error("Error creating test plan from line {} of file, command:{}, error:{}", line, commandsList.get(i), ie.getMessage(), ie);
                throw new IllegalArgumentException("Error creating tast plan from file in line " + line + ", see log file");
            } else {
                LOGGER.error("Error creating test plan from cURL command:{}, error:{}", commandsList.get(i), ie.getMessage(), ie);
                throw ie;
            }
        }
    }
    return requests;
}
Also used : BasicCurlParser(org.apache.jmeter.protocol.http.curl.BasicCurlParser) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) ArrayList(java.util.ArrayList) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request)

Example 2 with Request

use of org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request in project jmeter by apache.

the class ParseCurlCommandAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    statusText.setText("");
    statusText.setForeground(Color.GREEN);
    boolean isReadFromFile = false;
    if (e.getActionCommand().equals(CREATE_REQUEST)) {
        List<String> commandsList = null;
        try {
            if (!filePanel.getFilename().trim().isEmpty() && cURLCommandTA.getText().trim().isEmpty()) {
                commandsList = readFromFile(filePanel.getFilename().trim());
                isReadFromFile = true;
            } else if (filePanel.getFilename().trim().isEmpty() && !cURLCommandTA.getText().trim().isEmpty()) {
                commandsList = readFromTextPanel(cURLCommandTA.getText().trim());
            } else {
                throw new IllegalArgumentException("Error creating tast plan ,Please select one between reading file and directly fill in the panel");
            }
            List<Request> requests = parseCommands(isReadFromFile, commandsList);
            for (int i = 0; i < requests.size(); i++) {
                BasicCurlParser.Request request = requests.get(i);
                try {
                    String commentText = createCommentText(request);
                    GuiPackage guiPackage = GuiPackage.getInstance();
                    guiPackage.updateCurrentNode();
                    JMeterTreeNode treeNode = findFirstNodeOfType(AbstractThreadGroup.class);
                    if (treeNode == null) {
                        LOGGER.info("No AbstractThreadGroup found, potentially empty plan, creating a new plan");
                        createTestPlan(e, request, commentText);
                    } else {
                        JMeterTreeNode currentNode = guiPackage.getCurrentNode();
                        Object userObject = currentNode.getUserObject();
                        if (userObject instanceof Controller && !(userObject instanceof ReplaceableController)) {
                            LOGGER.info("Newly created element will be placed under current selected node {}", currentNode.getName());
                            addToTestPlan(currentNode, request, commentText);
                        } else {
                            LOGGER.info("Newly created element will be placed under first AbstractThreadGroup node {}", treeNode.getName());
                            addToTestPlan(treeNode, request, commentText);
                        }
                    }
                    statusText.setText(JMeterUtils.getResString("curl_create_success"));
                } catch (Exception ex) {
                    LOGGER.error("Error creating test plan from cURL command:{}, error:{}", commandsList.get(i), ex.getMessage(), ex);
                    statusText.setText(MessageFormat.format(JMeterUtils.getResString("curl_create_failure"), ex.getMessage()));
                    statusText.setForeground(UIManager.getColor(JMeterUIDefaults.LABEL_ERROR_FOREGROUND));
                    break;
                }
            }
        } catch (Exception ex) {
            LOGGER.error("Error creating test plan from cURL command list:{}", commandsList, ex);
            statusText.setText(MessageFormat.format(JMeterUtils.getResString("curl_create_failure"), ex.getMessage()));
            statusText.setForeground(UIManager.getColor(JMeterUIDefaults.LABEL_ERROR_FOREGROUND));
        }
    }
}
Also used : BasicCurlParser(org.apache.jmeter.protocol.http.curl.BasicCurlParser) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) ReplaceableController(org.apache.jmeter.control.ReplaceableController) Controller(org.apache.jmeter.control.Controller) LoopController(org.apache.jmeter.control.LoopController) SAXException(org.xml.sax.SAXException) TikaException(org.apache.tika.exception.TikaException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) GuiPackage(org.apache.jmeter.gui.GuiPackage) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) ReplaceableController(org.apache.jmeter.control.ReplaceableController)

Example 3 with Request

use of org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request in project jmeter by apache.

the class ParseCurlCommandActionTest method testCreateSampler.

@Test
public void testCreateSampler(@TempDir Path tempDir) throws Exception {
    // test proxy in httpsampler
    ParseCurlCommandAction p = new ParseCurlCommandAction();
    BasicCurlParser basicCurlParser = new BasicCurlParser();
    Request request = basicCurlParser.parse("curl 'http://jmeter.apache.org:8443/' -x 'https://aa:bb@example.com:8042'");
    Method method = getMethodFor("createSampler", Request.class, String.class);
    HTTPSamplerProxy httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    assertEquals("https", httpSampler.getProxyScheme(), "proxy scheme should be set in httpsampler");
    assertEquals("example.com", httpSampler.getProxyHost(), "proxy host should be set in httpsampler");
    assertEquals(8042, httpSampler.getProxyPortInt(), "The command line should be parsed in turn");
    assertEquals("/", httpSampler.getPath(), "path should be set in httpsampler");
    assertEquals("jmeter.apache.org", httpSampler.getDomain(), "domain should be set in httpsampler");
    assertEquals(8443, httpSampler.getPort(), "port should be set in httpsampler");
    assertEquals("GET", httpSampler.getMethod(), "method should be set in httpsampler");
    // test post data in httpsampler
    request = basicCurlParser.parse("curl 'http://jmeter.apache.org/' --data 'name=test'");
    request.setInterfaceName("interface_name");
    httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    assertEquals("POST", httpSampler.getMethod());
    assertEquals("name=test", httpSampler.getArguments().getArgument(0).toString());
    // test form data in httpsampler(upload data)
    request = basicCurlParser.parse("curl 'http://jmeter.apache.org/' -F 'test=name;type=text/foo' -F 'test1=name1'");
    httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    Arguments samplerArguments = httpSampler.getArguments();
    assertEquals("POST", httpSampler.getMethod(), "method should be set in httpsampler");
    assertEquals("test", samplerArguments.getArgument(0).getName(), "form name should be set in httpsampler");
    assertEquals("name", samplerArguments.getArgument(0).getValue(), "form value should be set in httpsampler");
    assertEquals("test1", samplerArguments.getArgument(1).getName(), "form name should be set in httpsampler");
    assertEquals("name1", samplerArguments.getArgument(1).getValue(), "form value should be set in httpsampler");
    // test form data in httpsampler(upload file)
    String filePath = tempDir.resolve("test.txt").toAbsolutePath().toString();
    request = basicCurlParser.parse("curl 'http://jmeter.apache.org/' -F 'c=@" + filePath + ";type=text/foo' -F 'c1=@" + filePath + "'");
    httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    assertEquals("c", httpSampler.getHTTPFiles()[0].getParamName(), "form name should be set in httpsampler");
    assertEquals(filePath, httpSampler.getHTTPFiles()[0].getPath(), "form name should be set in httpsampler");
    assertEquals("c1", httpSampler.getHTTPFiles()[1].getParamName(), "form name should be set in httpsampler");
    // test form data in httpsampler
    request = basicCurlParser.parse("curl 'http://jmeter.apache.org/' --form-string 'c=@test.txt;type=text/foo'");
    httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    assertEquals("c", httpSampler.getArguments().getArgument(0).getName());
    assertEquals("@test.txt;type=text/foo", httpSampler.getArguments().getArgument(0).getValue());
    // test form data in httpsampler
    request = basicCurlParser.parse("curl -X PUT \"https://www.example.com:123/12345?param1=value1&param2=value2\" -H  \"accept: */*\" -H  \"X-XSRF-TOKEN: 1234\"");
    httpSampler = (HTTPSamplerProxy) method.invoke(p, request, "");
    assertEquals(new URL("https://www.example.com:123/12345?param1=value1&param2=value2"), httpSampler.getUrl());
    assertEquals(123, httpSampler.getPort());
    assertEquals("www.example.com", httpSampler.getDomain());
    assertEquals("/12345?param1=value1&param2=value2", httpSampler.getPath());
    assertEquals("PUT", httpSampler.getMethod());
    assertEquals(new Header("accept", "*/*"), httpSampler.getHeaderManager().getHeader(0));
    assertEquals(new Header("X-XSRF-TOKEN", "1234"), httpSampler.getHeaderManager().getHeader(1));
}
Also used : BasicCurlParser(org.apache.jmeter.protocol.http.curl.BasicCurlParser) HTTPSamplerProxy(org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy) Header(org.apache.jmeter.protocol.http.control.Header) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) Arguments(org.apache.jmeter.config.Arguments) ParseCurlCommandAction(org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction) Method(java.lang.reflect.Method) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 4 with Request

use of org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request in project jmeter by apache.

the class ParseCurlCommandActionTest method testConfigureTimeout.

@Test
public void testConfigureTimeout() throws Exception {
    ParseCurlCommandAction p = new ParseCurlCommandAction();
    BasicCurlParser basicCurlParser = new BasicCurlParser();
    Request request = basicCurlParser.parse("curl 'http://jmeter.apache.org/'  -m '20'  --connect-timeout '1'");
    HTTPSamplerProxy httpSampler = (HTTPSamplerProxy) HTTPSamplerFactory.newInstance(HTTPSamplerFactory.DEFAULT_CLASSNAME);
    httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    httpSampler.setProperty(TestElement.NAME, "HTTP Request");
    Method method = getMethodFor("configureTimeout", Request.class, HTTPSamplerProxy.class);
    method.invoke(p, request, httpSampler);
    assertEquals(1000, httpSampler.getConnectTimeout());
    assertEquals(19000, httpSampler.getResponseTimeout());
}
Also used : BasicCurlParser(org.apache.jmeter.protocol.http.curl.BasicCurlParser) HTTPSamplerProxy(org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy) HttpTestSampleGui(org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) ParseCurlCommandAction(org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 5 with Request

use of org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request in project jmeter by apache.

the class ParseCurlCommandActionTest method testCreateCookieManagerHeader.

@Test
public void testCreateCookieManagerHeader() throws Exception {
    ParseCurlCommandAction p = new ParseCurlCommandAction();
    CookieManager cookieManager = new CookieManager();
    BasicCurlParser basicCurlParser = new BasicCurlParser();
    Request request = basicCurlParser.parse("curl 'http://jmeter.apache.org/' -H 'cookie: PHPSESSID=testphpsessid;a=b' --compressed");
    Field f = ParseCurlCommandAction.class.getDeclaredField("uploadCookiesCheckBox");
    f.setAccessible(true);
    JCheckBox uploadCookiesCheckBox = new JCheckBox(JMeterUtils.getResString("curl_add_cookie_header_to_cookiemanager"), true);
    f.set(p, uploadCookiesCheckBox);
    Method method = getMethodFor("createCookieManager", CookieManager.class, Request.class);
    method.invoke(p, cookieManager, request);
    assertEquals("jmeter.apache.org", cookieManager.get(0).getDomain(), "the domain of cookie should be set in cookieManager");
    assertEquals("/", cookieManager.get(0).getPath(), "the path of cookie should be set in cookieManager");
    assertEquals("a", cookieManager.get(0).getName(), "the name of cookie should be set in cookieManager");
    assertEquals("b", cookieManager.get(0).getValue(), "the password of cookie should be set in cookieManager");
    uploadCookiesCheckBox = new JCheckBox(JMeterUtils.getResString("curl_add_cookie_header_to_cookiemanager"), false);
    f.set(p, uploadCookiesCheckBox);
    cookieManager = new CookieManager();
    method.invoke(p, cookieManager, request);
    assertEquals(0, cookieManager.getCookies().size(), "When doesn't choose to upload cookies from header," + "the cookie shouldn't be set in cookieManager");
}
Also used : JCheckBox(javax.swing.JCheckBox) Field(java.lang.reflect.Field) BasicCurlParser(org.apache.jmeter.protocol.http.curl.BasicCurlParser) Request(org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request) ParseCurlCommandAction(org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction) Method(java.lang.reflect.Method) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) Test(org.junit.jupiter.api.Test)

Aggregations

BasicCurlParser (org.apache.jmeter.protocol.http.curl.BasicCurlParser)21 Request (org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request)21 ParseCurlCommandAction (org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction)19 Test (org.junit.jupiter.api.Test)17 Method (java.lang.reflect.Method)16 HTTPSamplerProxy (org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy)6 DNSCacheManager (org.apache.jmeter.protocol.http.control.DNSCacheManager)4 AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)3 HttpTestSampleGui (org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Authorization (org.apache.jmeter.protocol.http.control.Authorization)2 CookieManager (org.apache.jmeter.protocol.http.control.CookieManager)2 TestPlan (org.apache.jmeter.testelement.TestPlan)2 ThreadGroup (org.apache.jmeter.threads.ThreadGroup)2 HashTree (org.apache.jorphan.collections.HashTree)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1