Search in sources :

Example 1 with ParseCurlCommandAction

use of org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction in project jmeter by apache.

the class ParseCurlCommandActionTest method testReadCurlCommandsFromFile.

@Test
public void testReadCurlCommandsFromFile(@TempDir Path tempDir) throws Exception {
    String cmdLine = "curl 'http://jmeter.apache.org/' --max-redirs 'b'" + System.lineSeparator() + "curl 'http://jmeter.apache.org/' --include --keepalive-time '20'";
    String tempPath = writeToTempFile(tempDir, cmdLine);
    ParseCurlCommandAction p = new ParseCurlCommandAction();
    List<String> commands = p.readFromFile(tempPath);
    assertTrue(commands.contains("curl 'http://jmeter.apache.org/' --max-redirs 'b'"), "Curl commands should be saved in list");
    assertTrue(commands.contains("curl 'http://jmeter.apache.org/' --include --keepalive-time '20'"), "Curl commands should be saved in list");
    assertEquals(2, commands.size());
}
Also used : ParseCurlCommandAction(org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction) Test(org.junit.jupiter.api.Test)

Example 2 with ParseCurlCommandAction

use of org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction 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 3 with ParseCurlCommandAction

use of org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction 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 4 with ParseCurlCommandAction

use of org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction 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)

Example 5 with ParseCurlCommandAction

use of org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction in project jmeter by apache.

the class ParseCurlCommandActionTest method testCommentTextStartsWith.

private void testCommentTextStartsWith(String cmdLine, String expectedComment) {
    ParseCurlCommandAction p = new ParseCurlCommandAction();
    BasicCurlParser basicCurlParser = new BasicCurlParser();
    Request request = basicCurlParser.parse(cmdLine);
    String comment = p.createCommentText(request);
    assertTrue(comment.trim().startsWith(expectedComment));
}
Also used : 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)

Aggregations

ParseCurlCommandAction (org.apache.jmeter.protocol.http.gui.action.ParseCurlCommandAction)24 Test (org.junit.jupiter.api.Test)21 BasicCurlParser (org.apache.jmeter.protocol.http.curl.BasicCurlParser)19 Request (org.apache.jmeter.protocol.http.curl.BasicCurlParser.Request)19 Method (java.lang.reflect.Method)17 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 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 JCheckBox (javax.swing.JCheckBox)1 Arguments (org.apache.jmeter.config.Arguments)1 Header (org.apache.jmeter.protocol.http.control.Header)1