use of org.apache.jmeter.protocol.http.curl.BasicCurlParser 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;
}
use of org.apache.jmeter.protocol.http.curl.BasicCurlParser 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¶m2=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¶m2=value2"), httpSampler.getUrl());
assertEquals(123, httpSampler.getPort());
assertEquals("www.example.com", httpSampler.getDomain());
assertEquals("/12345?param1=value1¶m2=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));
}
use of org.apache.jmeter.protocol.http.curl.BasicCurlParser 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());
}
use of org.apache.jmeter.protocol.http.curl.BasicCurlParser 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");
}
use of org.apache.jmeter.protocol.http.curl.BasicCurlParser 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));
}
Aggregations