Search in sources :

Example 16 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class TestHTTPSamplers method testSetAndGetMimetype.

@Test
public void testSetAndGetMimetype() {
    HTTPSamplerBase sampler = new HTTPNullSampler();
    sampler.setHTTPFiles(new HTTPFileArg[] { new HTTPFileArg("", "", "mime") });
    HTTPFileArg file = sampler.getHTTPFiles()[0];
    assertEquals("mime", file.getMimeType());
    sampler.setHTTPFiles(new HTTPFileArg[] { new HTTPFileArg("", "", "mime2") });
    file = sampler.getHTTPFiles()[0];
    assertEquals("mime2", file.getMimeType());
}
Also used : HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) Test(org.junit.Test)

Example 17 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class TestHTTPSamplers method testSetAndGetFileField.

@Test
public void testSetAndGetFileField() {
    HTTPSamplerBase sampler = new HTTPNullSampler();
    sampler.setHTTPFiles(new HTTPFileArg[] { new HTTPFileArg("", "param", "") });
    HTTPFileArg file = sampler.getHTTPFiles()[0];
    assertEquals("param", file.getParamName());
    sampler.setHTTPFiles(new HTTPFileArg[] { new HTTPFileArg("", "param2", "") });
    file = sampler.getHTTPFiles()[0];
    assertEquals("param2", file.getParamName());
}
Also used : HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) Test(org.junit.Test)

Example 18 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class HTTPSamplerBase method mergeFileProperties.

/**
     * JMeter 2.3.1 and earlier only had fields for one file on the GUI:
     * <ul>
     *   <li>FILE_NAME</li>
     *   <li>FILE_FIELD</li>
     *   <li>MIMETYPE</li>
     * </ul>
     * These were stored in their own individual properties.
     * <p>
     * Version 2.3.3 introduced a list of files, each with their own path, name and mimetype.
     * <p>
     * In order to maintain backwards compatibility of test plans, the 3 original properties
     * were retained; additional file entries are stored in an HTTPFileArgs class.
     * The HTTPFileArgs class was only present if there is more than 1 file; this means that
     * such test plans are backward compatible.
     * <p>
     * Versions after 2.3.4 dispense with the original set of 3 properties.
     * Test plans that use them are converted to use a single HTTPFileArgs list.
     *
     * @see HTTPSamplerBaseConverter
     */
void mergeFileProperties() {
    JMeterProperty fileName = getProperty(FILE_NAME);
    JMeterProperty paramName = getProperty(FILE_FIELD);
    JMeterProperty mimeType = getProperty(MIMETYPE);
    HTTPFileArg oldStyleFile = new HTTPFileArg(fileName, paramName, mimeType);
    HTTPFileArgs fileArgs = getHTTPFileArgs();
    HTTPFileArgs allFileArgs = new HTTPFileArgs();
    if (oldStyleFile.isNotEmpty()) {
        // OK, we have an old-style file definition
        // save it
        allFileArgs.addHTTPFileArg(oldStyleFile);
        // Now deal with any additional file arguments
        if (fileArgs != null) {
            HTTPFileArg[] infiles = fileArgs.asArray();
            for (HTTPFileArg infile : infiles) {
                allFileArgs.addHTTPFileArg(infile);
            }
        }
    } else {
        if (fileArgs != null) {
            // for new test plans that don't have FILE/PARAM/MIME properties
            allFileArgs = fileArgs;
        }
    }
    // Updated the property lists
    setHTTPFileArgs(allFileArgs);
    removeProperty(FILE_FIELD);
    removeProperty(FILE_NAME);
    removeProperty(MIMETYPE);
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HTTPFileArgs(org.apache.jmeter.protocol.http.util.HTTPFileArgs) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg)

Example 19 with HTTPFileArg

use of org.apache.jmeter.protocol.http.util.HTTPFileArg in project jmeter by apache.

the class HTTPSamplerBase method setHTTPFiles.

/**
     * Saves the list of files.
     * The first file is saved in the Filename/field/mimetype properties.
     * Any additional files are saved in the FILE_ARGS array.
     *
     * @param files list of files to save
     */
public void setHTTPFiles(HTTPFileArg[] files) {
    HTTPFileArgs fileArgs = new HTTPFileArgs();
    // Weed out the empty files
    if (files.length > 0) {
        for (HTTPFileArg file : files) {
            if (file.isNotEmpty()) {
                fileArgs.addHTTPFileArg(file);
            }
        }
    }
    setHTTPFileArgs(fileArgs);
}
Also used : HTTPFileArgs(org.apache.jmeter.protocol.http.util.HTTPFileArgs) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg)

Example 20 with HTTPFileArg

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

Aggregations

HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)24 Test (org.junit.Test)9 File (java.io.File)6 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)6 Arguments (org.apache.jmeter.config.Arguments)5 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HTTPFileArgs (org.apache.jmeter.protocol.http.util.HTTPFileArgs)4 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)3 URLConnection (java.net.URLConnection)2 HttpEntity (org.apache.http.HttpEntity)2 FileEntity (org.apache.http.entity.FileEntity)2 StringEntity (org.apache.http.entity.StringEntity)2 Argument (org.apache.jmeter.config.Argument)2 AuthManager (org.apache.jmeter.protocol.http.control.AuthManager)2 HeaderManager (org.apache.jmeter.protocol.http.control.HeaderManager)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1