Search in sources :

Example 1 with Header

use of org.apache.jmeter.protocol.http.control.Header in project jmeter by apache.

the class ProxyControl method createAuthorization.

/**
     * Detect Header manager in subConfigs,
     * Find(if any) Authorization header
     * Construct Authentication object
     * Removes Authorization if present 
     *
     * @param subConfigs {@link TestElement}[]
     * @param sampler {@link HTTPSamplerBase}
     * @return {@link Authorization}
     */
private Authorization createAuthorization(final TestElement[] testElements, HTTPSamplerBase sampler) {
    Header authHeader;
    Authorization authorization = null;
    // Iterate over subconfig elements searching for HeaderManager
    for (TestElement te : testElements) {
        if (te instanceof HeaderManager) {
            // headers should only contain the correct classes
            @SuppressWarnings("unchecked") List<TestElementProperty> headers = (ArrayList<TestElementProperty>) ((HeaderManager) te).getHeaders().getObjectValue();
            for (Iterator<?> iterator = headers.iterator(); iterator.hasNext(); ) {
                TestElementProperty tep = (TestElementProperty) iterator.next();
                if (tep.getName().equals(HTTPConstants.HEADER_AUTHORIZATION)) {
                    //Construct Authorization object from HEADER_AUTHORIZATION
                    authHeader = (Header) tep.getObjectValue();
                    //$NON-NLS-1$
                    String[] authHeaderContent = authHeader.getValue().split(" ");
                    String authType;
                    String authCredentialsBase64;
                    if (authHeaderContent.length >= 2) {
                        authType = authHeaderContent[0];
                        authCredentialsBase64 = authHeaderContent[1];
                        authorization = new Authorization();
                        try {
                            authorization.setURL(sampler.getUrl().toExternalForm());
                        } catch (MalformedURLException e) {
                            log.error("Error filling url on authorization, message:" + e.getMessage(), e);
                            //$NON-NLS-1$
                            authorization.setURL("${AUTH_BASE_URL}");
                        }
                        // if HEADER_AUTHORIZATION contains "Basic"
                        // then set Mechanism.BASIC_DIGEST, otherwise Mechanism.KERBEROS
                        authorization.setMechanism(authType.equals(BASIC_AUTH) || authType.equals(DIGEST_AUTH) ? AuthManager.Mechanism.BASIC_DIGEST : AuthManager.Mechanism.KERBEROS);
                        if (BASIC_AUTH.equals(authType)) {
                            String authCred = new String(Base64.decodeBase64(authCredentialsBase64));
                            //$NON-NLS-1$
                            String[] loginPassword = authCred.split(":");
                            authorization.setUser(loginPassword[0]);
                            authorization.setPass(loginPassword[1]);
                        } else {
                            // Digest or Kerberos
                            //$NON-NLS-1$
                            authorization.setUser("${AUTH_LOGIN}");
                            //$NON-NLS-1$
                            authorization.setPass("${AUTH_PASSWORD}");
                        }
                    }
                    // remove HEADER_AUTHORIZATION from HeaderManager 
                    // because it's useless after creating Authorization object
                    iterator.remove();
                }
            }
        }
    }
    return authorization;
}
Also used : Authorization(org.apache.jmeter.protocol.http.control.Authorization) MalformedURLException(java.net.MalformedURLException) Header(org.apache.jmeter.protocol.http.control.Header) TestElementProperty(org.apache.jmeter.testelement.property.TestElementProperty) ArrayList(java.util.ArrayList) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Example 2 with Header

use of org.apache.jmeter.protocol.http.control.Header in project jmeter by apache.

the class HttpRequestHdr method parseLine.

/*
     * Split line into name/value pairs and store in headers if relevant
     * If name = "content-length", then return value as int, else return 0
     */
private int parseLine(String nextLine) {
    int colon = nextLine.indexOf(':');
    if (colon <= 0) {
        // Nothing to do
        return 0;
    }
    String name = nextLine.substring(0, colon).trim();
    String value = nextLine.substring(colon + 1).trim();
    headers.put(name.toLowerCase(java.util.Locale.ENGLISH), new Header(name, value));
    if (name.equalsIgnoreCase(CONTENT_LENGTH)) {
        return Integer.parseInt(value);
    }
    return 0;
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header)

Example 3 with Header

use of org.apache.jmeter.protocol.http.control.Header in project jmeter by apache.

the class HttpRequestHdr method createHeaderManager.

private HeaderManager createHeaderManager() {
    HeaderManager manager = new HeaderManager();
    for (Map.Entry<String, Header> entry : headers.entrySet()) {
        final String key = entry.getKey();
        if (!key.equals(PROXY_CONNECTION) && !key.equals(CONTENT_LENGTH) && !key.equalsIgnoreCase(HTTPConstants.HEADER_CONNECTION)) {
            manager.add(entry.getValue());
        }
    }
    // $NON-NLS-1$
    manager.setName(JMeterUtils.getResString("header_manager_title"));
    manager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
    manager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
    return manager;
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) HeaderPanel(org.apache.jmeter.protocol.http.gui.HeaderPanel) HashMap(java.util.HashMap) Map(java.util.Map) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Example 4 with Header

use of org.apache.jmeter.protocol.http.control.Header in project jmeter by apache.

the class DefaultSamplerCreator method detectAndModifySamplerOnGraphQLRequest.

private void detectAndModifySamplerOnGraphQLRequest(final HTTPSamplerBase sampler, final HttpRequestHdr request) {
    final String method = request.getMethod();
    final Header header = request.getHeaderManager().getFirstHeaderNamed("Content-Type");
    final boolean graphQLContentType = header != null && GraphQLRequestParamUtils.isGraphQLContentType(header.getValue());
    GraphQLRequestParams params = null;
    if (HTTPConstants.POST.equals(method) && graphQLContentType) {
        try {
            byte[] postData = request.getRawPostData();
            if (postData != null && postData.length > 0) {
                params = GraphQLRequestParamUtils.toGraphQLRequestParams(request.getRawPostData(), sampler.getContentEncoding());
            }
        } catch (Exception e) {
            log.debug("Ignoring request, '{}' as it's not a valid GraphQL post data.", request);
        }
    } else if (HTTPConstants.GET.equals(method)) {
        try {
            params = GraphQLRequestParamUtils.toGraphQLRequestParams(sampler.getArguments(), sampler.getContentEncoding());
        } catch (Exception e) {
            log.debug("Ignoring request, '{}' as it does not valid GraphQL arguments.", request);
        }
    }
    if (params != null) {
        sampler.setProperty(TestElement.GUI_CLASS, GraphQLHTTPSamplerGui.class.getName());
        sampler.setProperty(GraphQLUrlConfigGui.OPERATION_NAME, params.getOperationName());
        sampler.setProperty(GraphQLUrlConfigGui.QUERY, params.getQuery());
        sampler.setProperty(GraphQLUrlConfigGui.VARIABLES, params.getVariables());
    }
}
Also used : GraphQLRequestParams(org.apache.jmeter.protocol.http.config.GraphQLRequestParams) GraphQLHTTPSamplerGui(org.apache.jmeter.protocol.http.control.gui.GraphQLHTTPSamplerGui) Header(org.apache.jmeter.protocol.http.control.Header) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 5 with Header

use of org.apache.jmeter.protocol.http.control.Header 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)

Aggregations

Header (org.apache.jmeter.protocol.http.control.Header)15 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)10 Test (org.junit.jupiter.api.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 Authorization (org.apache.jmeter.protocol.http.control.Authorization)2 HeaderPanel (org.apache.jmeter.protocol.http.gui.HeaderPanel)2 TestElement (org.apache.jmeter.testelement.TestElement)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 MappingBuilder (com.github.tomakehurst.wiremock.client.MappingBuilder)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1