Search in sources :

Example 21 with JMeterProperty

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

the class UrlConfigTest method testOverRide.

@Test
public void testOverRide() {
    JMeterProperty jmp = partialConfig.getProperty(HTTPSamplerBase.DOMAIN);
    assertTrue(jmp instanceof NullProperty);
    assertEquals(jmp, new NullProperty(HTTPSamplerBase.DOMAIN));
    partialConfig.addTestElement(defaultConfig);
    assertEquals(partialConfig.getPropertyAsString(HTTPSamplerBase.DOMAIN), "www.xerox.com");
    assertEquals(partialConfig.getPropertyAsString(HTTPSamplerBase.PATH), "main.jsp");
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) NullProperty(org.apache.jmeter.testelement.property.NullProperty) Test(org.junit.Test)

Example 22 with JMeterProperty

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

the class HTTPHC4Impl method sendEntityData.

// TODO merge put and post methods as far as possible.
// e.g. post checks for multipart form/files, and if not, invokes sendData(HttpEntityEnclosingRequestBase)
/**
     * Creates the entity data to be sent.
     * <p>
     * If there is a file entry with a non-empty MIME type we use that to
     * set the request Content-Type header, otherwise we default to whatever
     * header is present from a Header Manager.
     * <p>
     * If the content charset {@link #getContentEncoding()} is null or empty 
     * we use the HC4 default provided by {@link HTTP#DEF_CONTENT_CHARSET} which is
     * ISO-8859-1.
     * 
     * @param entity to be processed, e.g. PUT or PATCH
     * @return the entity content, may be empty
     * @throws  UnsupportedEncodingException for invalid charset name
     * @throws IOException cannot really occur for ByteArrayOutputStream methods
     */
protected String sendEntityData(HttpEntityEnclosingRequestBase entity) throws IOException {
    boolean hasEntityBody = false;
    final HTTPFileArg[] files = getHTTPFiles();
    // Allow the mimetype of the file to control the content type
    // This is not obvious in GUI if you are not uploading any files,
    // but just sending the content of nameless parameters
    final HTTPFileArg file = files.length > 0 ? files[0] : null;
    String contentTypeValue = null;
    if (file != null && file.getMimeType() != null && file.getMimeType().length() > 0) {
        contentTypeValue = file.getMimeType();
        // we provide the MIME type here
        entity.setHeader(HEADER_CONTENT_TYPE, contentTypeValue);
    }
    // Check for local contentEncoding (charset) override; fall back to default for content body
    // we do this here rather so we can use the same charset to retrieve the data
    final String charset = getContentEncoding(HTTP.DEF_CONTENT_CHARSET.name());
    if (!hasArguments() && getSendFileAsPostBody()) {
        hasEntityBody = true;
        // If getSendFileAsPostBody returned true, it's sure that file is not null
        File reservedFile = FileServer.getFileServer().getResolvedFile(files[0].getPath());
        // no need for content-type here
        FileEntity fileRequestEntity = new FileEntity(reservedFile);
        entity.setEntity(fileRequestEntity);
    } else // just send all the values as the entity body
    if (getSendParameterValuesAsPostBody()) {
        hasEntityBody = true;
        // Just append all the parameter values, and use that as the entity body
        Arguments arguments = getArguments();
        StringBuilder entityBodyContent = new StringBuilder(arguments.getArgumentCount() * 15);
        for (JMeterProperty jMeterProperty : arguments) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            // Note: if "Encoded?" is not selected, arg.getEncodedValue is equivalent to arg.getValue
            if (charset != null) {
                entityBodyContent.append(arg.getEncodedValue(charset));
            } else {
                entityBodyContent.append(arg.getEncodedValue());
            }
        }
        StringEntity requestEntity = new StringEntity(entityBodyContent.toString(), charset);
        entity.setEntity(requestEntity);
    }
    // Check if we have any content to send for body
    if (hasEntityBody) {
        // If the request entity is repeatable, we can send it first to
        // our own stream, so we can return it
        final HttpEntity entityEntry = entity.getEntity();
        // Buffer to hold the entity body
        StringBuilder entityBody = null;
        if (entityEntry.isRepeatable()) {
            entityBody = new StringBuilder(1000);
            // FIXME Charset
            try (InputStream in = entityEntry.getContent();
                InputStream bounded = new BoundedInputStream(in, MAX_BODY_RETAIN_SIZE)) {
                entityBody.append(IOUtils.toString(bounded));
            }
            if (entityEntry.getContentLength() > MAX_BODY_RETAIN_SIZE) {
                entityBody.append("<actual file content shortened>");
            }
        } else {
            entityBody = new StringBuilder(65);
            // this probably cannot happen
            entityBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
        }
        return entityBody.toString();
    }
    // may be the empty string
    return "";
}
Also used : FileEntity(org.apache.http.entity.FileEntity) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HttpEntity(org.apache.http.HttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) DeflateInputStream(org.apache.http.client.entity.DeflateInputStream) BrotliInputStream(org.brotli.dec.BrotliInputStream) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) InputStream(java.io.InputStream) Arguments(org.apache.jmeter.config.Arguments) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) StringEntity(org.apache.http.entity.StringEntity) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) File(java.io.File)

Example 23 with JMeterProperty

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

the class HTTPHC4Impl method sendPostData.

// TODO needs cleaning up
/**
     * 
     * @param post {@link HttpPost}
     * @return String posted body if computable
     * @throws IOException if sending the data fails due to I/O
     */
protected String sendPostData(HttpPost post) throws IOException {
    // Buffer to hold the post body, except file content
    StringBuilder postedBody = new StringBuilder(1000);
    HTTPFileArg[] files = getHTTPFiles();
    final String contentEncoding = getContentEncodingOrNull();
    final boolean haveContentEncoding = contentEncoding != null;
    // application/x-www-form-urlencoded post request
    if (getUseMultipartForPost()) {
        // If a content encoding is specified, we use that as the
        // encoding of any parameter values
        Charset charset = null;
        if (haveContentEncoding) {
            charset = Charset.forName(contentEncoding);
        } else {
            charset = MIME.DEFAULT_CHARSET;
        }
        if (log.isDebugEnabled()) {
            log.debug("Building multipart with:getDoBrowserCompatibleMultipart(): {}, with charset:{}, haveContentEncoding:{}", getDoBrowserCompatibleMultipart(), charset, haveContentEncoding);
        }
        // Write the request to our own stream
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(charset);
        if (getDoBrowserCompatibleMultipart()) {
            multipartEntityBuilder.setLaxMode();
        } else {
            multipartEntityBuilder.setStrictMode();
        }
        // Add any parameters
        for (JMeterProperty jMeterProperty : getArguments()) {
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            String parameterName = arg.getName();
            if (arg.isSkippable(parameterName)) {
                continue;
            }
            StringBody stringBody = new StringBody(arg.getValue(), ContentType.create("text/plain", charset));
            FormBodyPart formPart = FormBodyPartBuilder.create(parameterName, stringBody).build();
            multipartEntityBuilder.addPart(formPart);
        }
        // Add any files
        // Cannot retrieve parts once added to the MultiPartEntity, so have to save them here.
        ViewableFileBody[] fileBodies = new ViewableFileBody[files.length];
        for (int i = 0; i < files.length; i++) {
            HTTPFileArg file = files[i];
            File reservedFile = FileServer.getFileServer().getResolvedFile(file.getPath());
            fileBodies[i] = new ViewableFileBody(reservedFile, file.getMimeType());
            multipartEntityBuilder.addPart(file.getParamName(), fileBodies[i]);
        }
        HttpEntity entity = multipartEntityBuilder.build();
        post.setEntity(entity);
        if (entity.isRepeatable()) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            for (ViewableFileBody fileBody : fileBodies) {
                fileBody.hideFileData = true;
            }
            entity.writeTo(bos);
            for (ViewableFileBody fileBody : fileBodies) {
                fileBody.hideFileData = false;
            }
            bos.flush();
            // We get the posted bytes using the encoding used to create it
            postedBody.append(bos.toString(// $NON-NLS-1$ this is the default used by HttpClient
            contentEncoding == null ? // $NON-NLS-1$ this is the default used by HttpClient
            "US-ASCII" : contentEncoding));
            bos.close();
        } else {
            // $NON-NLS-1$
            postedBody.append("<Multipart was not repeatable, cannot view what was sent>");
        }
    //            // Set the content type TODO - needed?
    //            String multiPartContentType = multiPart.getContentType().getValue();
    //            post.setHeader(HEADER_CONTENT_TYPE, multiPartContentType);
    } else {
        // not multipart
        // Check if the header manager had a content type header
        // This allows the user to specify his own content-type for a POST request
        Header contentTypeHeader = post.getFirstHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() > 0;
        // TODO: needs a multiple file upload scenerio
        if (!hasArguments() && getSendFileAsPostBody()) {
            // If getSendFileAsPostBody returned true, it's sure that file is not null
            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) {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                } else {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
            }
            // TODO is null correct?
            FileEntity fileRequestEntity = new FileEntity(new File(file.getPath()), (ContentType) null);
            post.setEntity(fileRequestEntity);
            // We just add placeholder text for file content
            postedBody.append("<actual file content, not shown here>");
        } else {
            // the post body will be encoded in the specified content encoding
            if (haveContentEncoding) {
                post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, contentEncoding);
            }
            // just send all the values as the post body
            if (getSendParameterValuesAsPostBody()) {
                // 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) {
                        post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, file.getMimeType());
                    } else {
                        // TODO - is this the correct default?
                        post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                    }
                }
                // Just append all the parameter values, and use that as the post body
                StringBuilder postBody = new StringBuilder();
                for (JMeterProperty jMeterProperty : getArguments()) {
                    HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
                    // Note: if "Encoded?" is not selected, arg.getEncodedValue is equivalent to arg.getValue
                    if (haveContentEncoding) {
                        postBody.append(arg.getEncodedValue(contentEncoding));
                    } else {
                        postBody.append(arg.getEncodedValue());
                    }
                }
                // Let StringEntity perform the encoding
                StringEntity requestEntity = new StringEntity(postBody.toString(), contentEncoding);
                post.setEntity(requestEntity);
                postedBody.append(postBody.toString());
            } else {
                // Set the content type
                if (!hasContentTypeHeader) {
                    post.setHeader(HTTPConstants.HEADER_CONTENT_TYPE, HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED);
                }
                // Add the parameters
                PropertyIterator args = getArguments().iterator();
                List<NameValuePair> nvps = new ArrayList<>();
                String urlContentEncoding = contentEncoding;
                if (urlContentEncoding == null || urlContentEncoding.length() == 0) {
                    // Use the default encoding for urls
                    urlContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
                }
                while (args.hasNext()) {
                    HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
                    // The HTTPClient always urlencodes both name and value,
                    // so if the argument is already encoded, we have to decode
                    // it before adding it to the post request
                    String parameterName = arg.getName();
                    if (arg.isSkippable(parameterName)) {
                        continue;
                    }
                    String parameterValue = arg.getValue();
                    if (!arg.isAlwaysEncoded()) {
                        // The value is already encoded by the user
                        // Must decode the value now, so that when the
                        // httpclient encodes it, we end up with the same value
                        // as the user had entered.
                        parameterName = URLDecoder.decode(parameterName, urlContentEncoding);
                        parameterValue = URLDecoder.decode(parameterValue, urlContentEncoding);
                    }
                    // Add the parameter, httpclient will urlencode it
                    nvps.add(new BasicNameValuePair(parameterName, parameterValue));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, urlContentEncoding);
                post.setEntity(entity);
                if (entity.isRepeatable()) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    post.getEntity().writeTo(bos);
                    bos.flush();
                    // We get the posted bytes using the encoding used to create it
                    postedBody.append(bos.toString(contentEncoding != null ? contentEncoding : SampleResult.DEFAULT_HTTP_ENCODING));
                    bos.close();
                } else {
                    postedBody.append("<RequestEntity was not repeatable, cannot view what was sent>");
                }
            }
        }
    }
    return postedBody.toString();
}
Also used : MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) FormBodyPart(org.apache.http.entity.mime.FormBodyPart) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) FileEntity(org.apache.http.entity.FileEntity) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) StringBody(org.apache.http.entity.mime.content.StringBody) File(java.io.File)

Example 24 with JMeterProperty

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

the class SmtpSamplerGui method configure.

/**
     * Copy the data from the test element to the GUI, method has to be implemented by interface
     * @param element Test-element to be used as data-input
     * @see org.apache.jmeter.gui.AbstractJMeterGuiComponent#configure(org.apache.jmeter.testelement.TestElement)
     */
@Override
public void configure(TestElement element) {
    if (smtpPanel == null) {
        smtpPanel = new SmtpPanel();
    }
    smtpPanel.setServer(element.getPropertyAsString(SmtpSampler.SERVER));
    smtpPanel.setPort(element.getPropertyAsString(SmtpSampler.SERVER_PORT));
    smtpPanel.setTimeout(element.getPropertyAsString(SmtpSampler.SERVER_TIMEOUT));
    smtpPanel.setConnectionTimeout(element.getPropertyAsString(SmtpSampler.SERVER_CONNECTION_TIMEOUT));
    smtpPanel.setMailFrom(element.getPropertyAsString(SmtpSampler.MAIL_FROM));
    smtpPanel.setMailReplyTo(element.getPropertyAsString(SmtpSampler.MAIL_REPLYTO));
    smtpPanel.setReceiverTo(element.getPropertyAsString(SmtpSampler.RECEIVER_TO));
    smtpPanel.setReceiverCC(element.getPropertyAsString(SmtpSampler.RECEIVER_CC));
    smtpPanel.setReceiverBCC(element.getPropertyAsString(SmtpSampler.RECEIVER_BCC));
    smtpPanel.setBody(element.getPropertyAsString(SmtpSampler.MESSAGE));
    smtpPanel.setPlainBody(element.getPropertyAsBoolean(SmtpSampler.PLAIN_BODY));
    smtpPanel.setSubject(element.getPropertyAsString(SmtpSampler.SUBJECT));
    smtpPanel.setSuppressSubject(element.getPropertyAsBoolean(SmtpSampler.SUPPRESS_SUBJECT));
    smtpPanel.setIncludeTimestamp(element.getPropertyAsBoolean(SmtpSampler.INCLUDE_TIMESTAMP));
    JMeterProperty headers = element.getProperty(SmtpSampler.HEADER_FIELDS);
    if (headers instanceof CollectionProperty) {
        // Might be NullProperty
        smtpPanel.setHeaderFields((CollectionProperty) headers);
    } else {
        smtpPanel.setHeaderFields(new CollectionProperty());
    }
    smtpPanel.setAttachments(element.getPropertyAsString(SmtpSampler.ATTACH_FILE));
    smtpPanel.setUseEmlMessage(element.getPropertyAsBoolean(SmtpSampler.USE_EML));
    smtpPanel.setEmlMessage(element.getPropertyAsString(SmtpSampler.EML_MESSAGE_TO_SEND));
    SecuritySettingsPanel secPanel = smtpPanel.getSecuritySettingsPanel();
    secPanel.configure(element);
    smtpPanel.setUseAuth(element.getPropertyAsBoolean(SmtpSampler.USE_AUTH));
    smtpPanel.setUsername(element.getPropertyAsString(SmtpSampler.USERNAME));
    smtpPanel.setPassword(element.getPropertyAsString(SmtpSampler.PASSWORD));
    smtpPanel.setMessageSizeStatistic(element.getPropertyAsBoolean(SmtpSampler.MESSAGE_SIZE_STATS));
    smtpPanel.setEnableDebug(element.getPropertyAsBoolean(SmtpSampler.ENABLE_DEBUG));
    super.configure(element);
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty)

Example 25 with JMeterProperty

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

the class LDAPExtSampler method getUserAttributes.

/***************************************************************************
     * Collect all the values from the table (Arguments), using this create the
     * Attributes, this will create the Attributes for the User
     * defined TestCase for Add Test
     *
     * @return The Attributes
     **************************************************************************/
private Attributes getUserAttributes() {
    Attributes attrs = new BasicAttributes(true);
    Attribute attr;
    for (JMeterProperty jMeterProperty : getArguments()) {
        Argument item = (Argument) jMeterProperty.getObjectValue();
        attr = attrs.get(item.getName());
        if (attr == null) {
            attr = getBasicAttribute(item.getName(), item.getValue());
        } else {
            attr.add(item.getValue());
        }
        attrs.put(attr);
    }
    return attrs;
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) LDAPArgument(org.apache.jmeter.protocol.ldap.config.gui.LDAPArgument) Argument(org.apache.jmeter.config.Argument) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes)

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