Search in sources :

Example 6 with Header

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

the class TestHeaderManager method testReplaceNoMatch.

@Test
public void testReplaceNoMatch() throws Exception {
    HeaderManager headerManager = new HeaderManager();
    headerManager.add(new Header("Referer", "https://jmeter.apache.org/changes.html"));
    headerManager.add(new Header("JSESSIONID", "AZAZDZDAFEFZEVZEZEVZEVZEVZZ"));
    int numberOfReplacements = headerManager.replace("JMeter.apache.org", "${host}", true);
    assertEquals(0, numberOfReplacements);
    assertEquals("Referer", headerManager.getHeader(0).getName());
    assertEquals("JSESSIONID", headerManager.getHeader(1).getName());
    assertEquals("https://jmeter.apache.org/changes.html", headerManager.getHeader(0).getValue());
    assertEquals("AZAZDZDAFEFZEVZEZEVZEVZEVZZ", headerManager.getHeader(1).getValue());
    headerManager = new HeaderManager();
    headerManager.add(new Header("Referer", "https://jmeter.apache.org/changes.html"));
    headerManager.add(new Header("JSESSIONID", "AZAZDZDAFEFZEVZEZEVZEVZEVZZ"));
    numberOfReplacements = headerManager.replace("JMeterx.apache.org", "${host}", false);
    assertEquals(0, numberOfReplacements);
    assertEquals("Referer", headerManager.getHeader(0).getName());
    assertEquals("JSESSIONID", headerManager.getHeader(1).getName());
    assertEquals("https://jmeter.apache.org/changes.html", headerManager.getHeader(0).getValue());
    assertEquals("AZAZDZDAFEFZEVZEZEVZEVZEVZZ", headerManager.getHeader(1).getValue());
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Test(org.junit.Test)

Example 7 with Header

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

the class TestHeaderManager method testReplace.

@Test
public void testReplace() throws Exception {
    HeaderManager headerManager = new HeaderManager();
    headerManager.add(new Header("Referer", "https://jmeter.apache.org/changes.html"));
    headerManager.add(new Header("JSESSIONID", "AZAZDZDAFEFZEVZEZEVZEVZEVZZ"));
    int numberOfReplacements = headerManager.replace("jmeter.apache.org", "${host}", true);
    assertEquals(1, numberOfReplacements);
    assertEquals("Referer", headerManager.getHeader(0).getName());
    assertEquals("JSESSIONID", headerManager.getHeader(1).getName());
    assertEquals("https://${host}/changes.html", headerManager.getHeader(0).getValue());
    assertEquals("AZAZDZDAFEFZEVZEZEVZEVZEVZZ", headerManager.getHeader(1).getValue());
    headerManager = new HeaderManager();
    headerManager.add(new Header("Referer", "https://JMeter.apache.org/changes.html"));
    headerManager.add(new Header("JSESSIONID", "AZAZDZDAFEFZEVZEZEVZEVZEVZZ"));
    numberOfReplacements = headerManager.replace("jmeter.apache.org", "${host}", false);
    assertEquals(1, numberOfReplacements);
    assertEquals("Referer", headerManager.getHeader(0).getName());
    assertEquals("JSESSIONID", headerManager.getHeader(1).getName());
    assertEquals("https://${host}/changes.html", headerManager.getHeader(0).getValue());
    assertEquals("AZAZDZDAFEFZEVZEZEVZEVZEVZZ", headerManager.getHeader(1).getValue());
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Test(org.junit.Test)

Example 8 with Header

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

the class HTTPJavaImpl method setConnectionHeaders.

/**
     * Extracts all the required headers for that particular URL request and
     * sets them in the <code>HttpURLConnection</code> passed in
     *
     * @param conn
     *            <code>HttpUrlConnection</code> which represents the URL
     *            request
     * @param u
     *            <code>URL</code> of the URL request
     * @param headerManager
     *            the <code>HeaderManager</code> containing all the cookies
     *            for this <code>UrlConfig</code>
     * @param cacheManager the CacheManager (may be null)
     */
private void setConnectionHeaders(HttpURLConnection conn, URL u, HeaderManager headerManager, CacheManager cacheManager) {
    // Add all the headers from the HeaderManager
    if (headerManager != null) {
        CollectionProperty headers = headerManager.getHeaders();
        if (headers != null) {
            for (JMeterProperty jMeterProperty : headers) {
                Header header = (Header) jMeterProperty.getObjectValue();
                String n = header.getName();
                String v = header.getValue();
                conn.addRequestProperty(n, v);
            }
        }
    }
    if (cacheManager != null) {
        cacheManager.setHeaders(conn, u);
    }
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Header(org.apache.jmeter.protocol.http.control.Header)

Example 9 with Header

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

the class AjpSampler method setConnectionHeaders.

private String setConnectionHeaders(URL url, String host, String method) throws IOException {
    HeaderManager headers = getHeaderManager();
    AuthManager auth = getAuthManager();
    StringBuilder hbuf = new StringBuilder();
    // Allow Headers to override Host setting
    //$NON-NLS-1$
    hbuf.append("Host").append(COLON_SPACE).append(host).append(NEWLINE);
    //Host
    setInt(0xA00b);
    setString(host);
    if (headers != null) {
        for (JMeterProperty jMeterProperty : headers.getHeaders()) {
            Header header = (Header) jMeterProperty.getObjectValue();
            String n = header.getName();
            String v = header.getValue();
            hbuf.append(n).append(COLON_SPACE).append(v).append(NEWLINE);
            int hc = translateHeader(n);
            if (hc > 0) {
                setInt(hc + AJP_HEADER_BASE);
            } else {
                setString(n);
            }
            setString(v);
        }
    }
    if (method.equals(HTTPConstants.POST)) {
        int cl = -1;
        HTTPFileArg[] hfa = getHTTPFiles();
        if (hfa.length > 0) {
            HTTPFileArg fa = hfa[0];
            String fn = fa.getPath();
            File input = new File(fn);
            cl = (int) input.length();
            if (body != null) {
                JOrphanUtils.closeQuietly(body);
                body = null;
            }
            body = new BufferedInputStream(new FileInputStream(input));
            setString(HTTPConstants.HEADER_CONTENT_DISPOSITION);
            setString("form-data; name=\"" + encode(fa.getParamName()) + "\"; filename=\"" + encode(fn) + //$NON-NLS-1$ //$NON-NLS-2$
            "\"");
            String mt = fa.getMimeType();
            hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(mt).append(NEWLINE);
            // content-type
            setInt(0xA007);
            setString(mt);
        } else {
            hbuf.append(HTTPConstants.HEADER_CONTENT_TYPE).append(COLON_SPACE).append(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED).append(NEWLINE);
            // content-type
            setInt(0xA007);
            setString(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
            StringBuilder sb = new StringBuilder();
            boolean first = true;
            for (JMeterProperty arg : getArguments()) {
                if (first) {
                    first = false;
                } else {
                    sb.append('&');
                }
                sb.append(arg.getStringValue());
            }
            stringBody = sb.toString();
            // TODO - charset?
            byte[] sbody = stringBody.getBytes();
            cl = sbody.length;
            body = new ByteArrayInputStream(sbody);
        }
        hbuf.append(HTTPConstants.HEADER_CONTENT_LENGTH).append(COLON_SPACE).append(String.valueOf(cl)).append(NEWLINE);
        // Content-length
        setInt(0xA008);
        setString(String.valueOf(cl));
    }
    if (auth != null) {
        String authHeader = auth.getAuthHeaderForURL(url);
        if (authHeader != null) {
            // Authorization
            setInt(0xA005);
            setString(authHeader);
            hbuf.append(HTTPConstants.HEADER_AUTHORIZATION).append(COLON_SPACE).append(authHeader).append(NEWLINE);
        }
    }
    return hbuf.toString();
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) FileInputStream(java.io.FileInputStream) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) Header(org.apache.jmeter.protocol.http.control.Header) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 10 with Header

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

the class HeaderPanel method addFromClipboard.

/**
     * Add values from the clipboard.
     * The clipboard is first split into lines, and the lines are then split on ':' or '\t'
     * to produce the header name and value.
     * Lines without a ':' are tested with '\t' and ignored if not found.
     */
protected void addFromClipboard() {
    GuiUtils.stopTableEditing(this.headerTable);
    int rowCount = headerTable.getRowCount();
    try {
        String clipboardContent = GuiUtils.getPastedText();
        if (clipboardContent == null) {
            return;
        }
        // $NON-NLS-1$
        String[] clipboardLines = clipboardContent.split("\n");
        for (String clipboardLine : clipboardLines) {
            // $NON-NLS-1$
            int index = clipboardLine.indexOf(":");
            if (index < 0) {
                // when pasting from another header panel the values are separated with '\t'
                index = clipboardLine.indexOf("\t");
            }
            if (index > 0) {
                Header header = new Header(clipboardLine.substring(0, index), clipboardLine.substring(index + 1));
                headerManager.add(header);
            }
        }
        tableModel.fireTableDataChanged();
        if (headerTable.getRowCount() > rowCount) {
            // Highlight (select) the appropriate rows.
            int rowToSelect = tableModel.getRowCount() - 1;
            headerTable.setRowSelectionInterval(rowCount, rowToSelect);
        }
        checkButtonsStatus();
    } catch (IOException ioe) {
        JOptionPane.showMessageDialog(this, "Could not add read headers from clipboard:\n" + ioe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    } catch (UnsupportedFlavorException ufe) {
        JOptionPane.showMessageDialog(this, "Could not add retrieved " + DataFlavor.stringFlavor.getHumanPresentableName() + " from clipboard" + ufe.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : Header(org.apache.jmeter.protocol.http.control.Header) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

Header (org.apache.jmeter.protocol.http.control.Header)10 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)7 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)1 AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)1 Authorization (org.apache.jmeter.protocol.http.control.Authorization)1 HeaderPanel (org.apache.jmeter.protocol.http.gui.HeaderPanel)1 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)1 TestElement (org.apache.jmeter.testelement.TestElement)1