Search in sources :

Example 1 with HeaderManager

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

use of org.apache.jmeter.protocol.http.control.HeaderManager 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 3 with HeaderManager

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

the class HTTPSamplerBase method setHeaderManager.

public void setHeaderManager(final HeaderManager value) {
    HeaderManager mgr = getHeaderManager();
    HeaderManager lValue = value;
    if (mgr != null) {
        lValue = mgr.merge(value);
        if (log.isDebugEnabled()) {
            log.debug("Existing HeaderManager '" + mgr.getName() + "' merged with '" + lValue.getName() + "'");
            for (int i = 0; i < lValue.getHeaders().size(); i++) {
                log.debug("    " + lValue.getHeader(i).getName() + "=" + lValue.getHeader(i).getValue());
            }
        }
    }
    setProperty(new TestElementProperty(HEADER_MANAGER, lValue));
}
Also used : TestElementProperty(org.apache.jmeter.testelement.property.TestElementProperty) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager)

Example 4 with HeaderManager

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

the class TestHttpRequestHdr method testParse1.

@Test
public void testParse1() throws Exception {
    // no space after :
    HttpRequestHdr req = new HttpRequestHdr();
    ByteArrayInputStream bis = null;
    bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:value \r\n".getBytes("ISO-8859-1"));
    req.parse(bis);
    bis.close();
    HeaderManager mgr = req.getHeaderManager();
    Header header;
    mgr.getHeaders();
    header = mgr.getHeader(0);
    assertEquals("name", header.getName());
    assertEquals("value", header.getValue());
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Test(org.junit.Test)

Example 5 with HeaderManager

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

the class TestHttpRequestHdr method testParse2.

@Test
public void testParse2() throws Exception {
    // spaces after :
    HttpRequestHdr req = new HttpRequestHdr();
    ByteArrayInputStream bis = null;
    bis = new ByteArrayInputStream("GET xxx HTTP/1.0\r\nname:           value \r\n".getBytes("ISO-8859-1"));
    req.parse(bis);
    bis.close();
    HeaderManager mgr = req.getHeaderManager();
    Header header;
    mgr.getHeaders();
    header = mgr.getHeader(0);
    assertEquals("name", header.getName());
    assertEquals("value", header.getValue());
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Test(org.junit.Test)

Aggregations

HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)10 Header (org.apache.jmeter.protocol.http.control.Header)7 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ArrayList (java.util.ArrayList)2 AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)2 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)2 TestElement (org.apache.jmeter.testelement.TestElement)2 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyStoreException (java.security.KeyStoreException)1 HashMap (java.util.HashMap)1