Search in sources :

Example 6 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.

the class HTTPSamplerBase method replace.

/**
     * Replace by replaceBy in path and body (arguments) properties
     */
@Override
public int replace(String regex, String replaceBy, boolean caseSensitive) throws Exception {
    int totalReplaced = 0;
    for (JMeterProperty jMeterProperty : getArguments()) {
        HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
        String value = arg.getValue();
        if (!StringUtils.isEmpty(value)) {
            Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive);
            // check if there is anything to replace
            int nbReplaced = ((Integer) result[1]).intValue();
            if (nbReplaced > 0) {
                String replacedText = (String) result[0];
                arg.setValue(replacedText);
                totalReplaced += nbReplaced;
            }
        }
    }
    String value = getPath();
    if (!StringUtils.isEmpty(value)) {
        Object[] result = JOrphanUtils.replaceAllWithRegex(value, regex, replaceBy, caseSensitive);
        // check if there is anything to replace
        int nbReplaced = ((Integer) result[1]).intValue();
        if (nbReplaced > 0) {
            String replacedText = (String) result[0];
            setPath(replacedText);
            totalReplaced += nbReplaced;
        }
    }
    if (!StringUtils.isEmpty(getDomain())) {
        Object[] result = JOrphanUtils.replaceAllWithRegex(getDomain(), regex, replaceBy, caseSensitive);
        // check if there is anything to replace
        int nbReplaced = ((Integer) result[1]).intValue();
        if (nbReplaced > 0) {
            String replacedText = (String) result[0];
            setDomain(replacedText);
            totalReplaced += nbReplaced;
        }
    }
    return totalReplaced;
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument)

Example 7 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.

the class PostWriter method setHeaders.

public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws IOException {
    // Get the encoding to use for the request
    String contentEncoding = sampler.getContentEncoding();
    if (contentEncoding == null || contentEncoding.length() == 0) {
        contentEncoding = ENCODING;
    }
    long contentLength = 0L;
    HTTPFileArg[] files = sampler.getHTTPFiles();
    // application/x-www-form-urlencoded post request
    if (sampler.getUseMultipartForPost()) {
        // Set the content type
        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, // $NON-NLS-1$
        HTTPConstants.MULTIPART_FORM_DATA + "; boundary=" + getBoundary());
        // Write the form section
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // First the multipart start divider
        bos.write(getMultipartDivider());
        // Add any parameters
        for (JMeterProperty jMeterProperty : sampler.getArguments()) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            String parameterName = arg.getName();
            if (arg.isSkippable(parameterName)) {
                continue;
            }
            // End the previous multipart
            bos.write(CRLF);
            // Write multipart for parameter
            writeFormMultipart(bos, parameterName, arg.getValue(), contentEncoding, sampler.getDoBrowserCompatibleMultipart());
        }
        // If there are any files, we need to end the previous multipart
        if (files.length > 0) {
            // End the previous multipart
            bos.write(CRLF);
        }
        bos.flush();
        // Keep the content, will be sent later
        formDataPostBody = bos.toByteArray();
        bos.close();
        contentLength = formDataPostBody.length;
        // the actual file content
        for (int i = 0; i < files.length; i++) {
            HTTPFileArg file = files[i];
            // Write multipart for file
            bos = new ByteArrayOutputStream();
            writeStartFileMultipart(bos, file.getPath(), file.getParamName(), file.getMimeType());
            bos.flush();
            // TODO is this correct?
            String header = bos.toString(contentEncoding);
            // If this is not the first file we can't write its header now
            // for simplicity we always save it, even if there is only one file
            file.setHeader(header);
            bos.close();
            contentLength += header.length();
            // Add also the length of the file content
            File uploadFile = new File(file.getPath());
            contentLength += uploadFile.length();
            // And the end of the file multipart
            contentLength += getFileMultipartEndDivider().length;
            if (i + 1 < files.length) {
                contentLength += CRLF.length;
            }
        }
        // Add the end of multipart
        contentLength += getMultipartEndDivider().length;
        // Set the content length
        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_LENGTH, Long.toString(contentLength));
        // Make the connection ready for sending post data
        connection.setDoOutput(true);
        connection.setDoInput(true);
    } else {
        // Check if the header manager had a content type header
        // This allows the user to specify his own content-type for a POST request
        String contentTypeHeader = connection.getRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE);
        boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0;
        // If there are no arguments, we can send a file as the body of the request
        if (sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
            // we're sure that there is one file because of
            // getSendFileAsPostBody method's return value.
            HTTPFileArg file = files[0];
            if (!hasContentTypeHeader) {
                // Allow the mimetype of the file to control the content type
                if (file.getMimeType() != null && file.getMimeType().length() > 0) {
                    connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                } else {
                    connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
            }
            // Create the content length we are going to write
            File inputFile = new File(file.getPath());
            contentLength = inputFile.length();
        } else {
            // We create the post body content now, so we know the size
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // If none of the arguments have a name specified, we
            // just send all the values as the post body
            String postBody = null;
            if (!sampler.getSendParameterValuesAsPostBody()) {
                // Set the content type
                if (!hasContentTypeHeader) {
                    connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
                // It is a normal post request, with parameter names and values
                postBody = sampler.getQueryString(contentEncoding);
            } else {
                // TODO: needs a multiple file upload scenerio
                if (!hasContentTypeHeader) {
                    HTTPFileArg file = files.length > 0 ? files[0] : null;
                    if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
                        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                    } else {
                        // TODO: is this the correct default?
                        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                    }
                }
                // Just append all the parameter values, and use that as the post body
                StringBuilder postBodyBuffer = new StringBuilder();
                for (JMeterProperty jMeterProperty : sampler.getArguments()) {
                    HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
                    postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
                }
                postBody = postBodyBuffer.toString();
            }
            bos.write(postBody.getBytes(contentEncoding));
            bos.flush();
            bos.close();
            // Keep the content, will be sent later
            formDataUrlEncoded = bos.toByteArray();
            contentLength = bos.toByteArray().length;
        }
        // Set the content length
        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_LENGTH, Long.toString(contentLength));
        // Make the connection ready for sending post data
        connection.setDoOutput(true);
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) File(java.io.File)

Example 8 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.

the class PutWriter method setHeaders.

@Override
public void setHeaders(URLConnection connection, HTTPSamplerBase sampler) throws IOException {
    // Get the encoding to use for the request
    String contentEncoding = sampler.getContentEncoding();
    if (contentEncoding == null || contentEncoding.length() == 0) {
        contentEncoding = ENCODING;
    }
    long contentLength = 0L;
    boolean hasPutBody = false;
    // Check if the header manager had a content type header
    // This allows the user to specify his own content-type for a PUT request
    String contentTypeHeader = connection.getRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE);
    boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0;
    HTTPFileArg[] files = sampler.getHTTPFiles();
    // If there are no arguments, we can send a file as the body of the request
    if (sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
        // If getSendFileAsPostBody returned true, it's sure that file is not null
        HTTPFileArg file = files[0];
        hasPutBody = true;
        if (!hasContentTypeHeader) {
            // Allow the mimetype of the file to control the content type
            if (file.getMimeType().length() > 0) {
                connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
            }
        }
        // Create the content length we are going to write
        File inputFile = new File(file.getPath());
        contentLength = inputFile.length();
    } else if (sampler.getSendParameterValuesAsPostBody()) {
        hasPutBody = true;
        // but just sending the content of nameless parameters
        if (!hasContentTypeHeader && files.length == 1 && files[0].getMimeType().length() > 0) {
            connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_TYPE, files[0].getMimeType());
        }
        // We create the post body content now, so we know the size
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        // Just append all the parameter values, and use that as the put body
        StringBuilder putBodyBuffer = new StringBuilder();
        for (JMeterProperty jMeterProperty : sampler.getArguments()) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            putBodyBuffer.append(arg.getEncodedValue(contentEncoding));
        }
        bos.write(putBodyBuffer.toString().getBytes(contentEncoding));
        bos.flush();
        bos.close();
        // Keep the content, will be sent later
        formDataUrlEncoded = bos.toByteArray();
        contentLength = bos.toByteArray().length;
    }
    if (hasPutBody) {
        // Set the content length
        connection.setRequestProperty(HTTPConstants.HEADER_CONTENT_LENGTH, Long.toString(contentLength));
        // Make the connection ready for sending post data
        connection.setDoOutput(true);
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) File(java.io.File)

Example 9 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.

the class HTTPHC4Impl method setConnectionHeaders.

/**
     * Extracts all the required non-cookie headers for that particular URL request and
     * sets them in the <code>HttpMethod</code> passed in
     *
     * @param request
     *            <code>HttpRequest</code> which represents the request
     * @param url
     *            <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)
     */
protected void setConnectionHeaders(HttpRequestBase request, URL url, HeaderManager headerManager, CacheManager cacheManager) {
    if (headerManager != null) {
        CollectionProperty headers = headerManager.getHeaders();
        if (headers != null) {
            for (JMeterProperty jMeterProperty : headers) {
                org.apache.jmeter.protocol.http.control.Header header = (org.apache.jmeter.protocol.http.control.Header) jMeterProperty.getObjectValue();
                String n = header.getName();
                // TODO - what other headers are not allowed?
                if (!HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(n)) {
                    String v = header.getValue();
                    if (HTTPConstants.HEADER_HOST.equalsIgnoreCase(n)) {
                        int port = getPortFromHostHeader(v, url.getPort());
                        // remove any port specification // $NON-NLS-1$ $NON-NLS-2$
                        v = v.replaceFirst(":\\d+$", "");
                        if (port != -1) {
                            if (port == url.getDefaultPort()) {
                                // no need to specify the port if it is the default
                                port = -1;
                            }
                        }
                        request.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(v, port));
                    } else {
                        request.addHeader(n, v);
                    }
                }
            }
        }
    }
    if (cacheManager != null) {
        cacheManager.setHeaders(url, request);
    }
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) HttpHost(org.apache.http.HttpHost)

Example 10 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty in project jmeter by apache.

the class ThroughputController method getPercentThroughputAsFloat.

protected float getPercentThroughputAsFloat() {
    JMeterProperty prop = getProperty(PERCENTTHROUGHPUT);
    float retVal = 100;
    if (prop instanceof FloatProperty) {
        retVal = ((FloatProperty) prop).getFloatValue();
    } else {
        String valueString = prop.getStringValue();
        try {
            retVal = Float.parseFloat(valueString);
        } catch (NumberFormatException e) {
            log.warn("Error parsing '{}'", valueString, e);
        }
    }
    return retVal;
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) FloatProperty(org.apache.jmeter.testelement.property.FloatProperty)

Aggregations

JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)87 StringProperty (org.apache.jmeter.testelement.property.StringProperty)23 Test (org.junit.Test)23 PropertyIterator (org.apache.jmeter.testelement.property.PropertyIterator)13 CollectionProperty (org.apache.jmeter.testelement.property.CollectionProperty)12 Argument (org.apache.jmeter.config.Argument)11 ArrayList (java.util.ArrayList)9 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)8 File (java.io.File)7 Arguments (org.apache.jmeter.config.Arguments)7 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)6 TestElement (org.apache.jmeter.testelement.TestElement)6 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)5 URL (java.net.URL)4 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)3 Sampler (org.apache.jmeter.samplers.Sampler)3 TestPlan (org.apache.jmeter.testelement.TestPlan)3 NullProperty (org.apache.jmeter.testelement.property.NullProperty)3