Search in sources :

Example 1 with MultipartUrlConfig

use of org.apache.jmeter.protocol.http.config.MultipartUrlConfig in project jmeter by apache.

the class RequestViewHTTP method setSamplerResult.

/* (non-Javadoc)
     * @see org.apache.jmeter.visualizers.request.RequestView#setSamplerResult(java.lang.Object)
     */
@Override
public void setSamplerResult(Object objectResult) {
    this.searchTextExtension.resetTextToFind();
    if (objectResult instanceof HTTPSampleResult) {
        HTTPSampleResult sampleResult = (HTTPSampleResult) objectResult;
        // Display with same order HTTP protocol
        requestModel.addRow(new RowResult(//$NON-NLS-1$
        JMeterUtils.getResString("view_results_table_request_http_method"), sampleResult.getHTTPMethod()));
        // Parsed request headers
        LinkedHashMap<String, String> lhm = JMeterUtils.parseHeaders(sampleResult.getRequestHeaders());
        for (Entry<String, String> entry : lhm.entrySet()) {
            headersModel.addRow(new RowResult(entry.getKey(), entry.getValue()));
        }
        URL hUrl = sampleResult.getURL();
        if (hUrl != null) {
            // can be null - e.g. if URL was invalid
            requestModel.addRow(new RowResult(JMeterUtils.getResString(//$NON-NLS-1$
            "view_results_table_request_http_protocol"), hUrl.getProtocol()));
            requestModel.addRow(new RowResult(//$NON-NLS-1$
            JMeterUtils.getResString("view_results_table_request_http_host"), hUrl.getHost()));
            int port = hUrl.getPort() == -1 ? hUrl.getDefaultPort() : hUrl.getPort();
            requestModel.addRow(new RowResult(//$NON-NLS-1$
            JMeterUtils.getResString("view_results_table_request_http_port"), Integer.valueOf(port)));
            requestModel.addRow(new RowResult(//$NON-NLS-1$
            JMeterUtils.getResString("view_results_table_request_http_path"), hUrl.getPath()));
            //$NON-NLS-1$
            String queryGet = hUrl.getQuery() == null ? "" : hUrl.getQuery();
            boolean isMultipart = isMultipart(lhm);
            // Concatenate query post if exists
            String queryPost = sampleResult.getQueryString();
            if (!isMultipart && StringUtils.isNotBlank(queryPost)) {
                if (queryGet.length() > 0) {
                    queryGet += PARAM_CONCATENATE;
                }
                queryGet += queryPost;
            }
            if (StringUtils.isNotBlank(queryGet)) {
                Set<Entry<String, String[]>> keys = RequestViewHTTP.getQueryMap(queryGet).entrySet();
                for (Entry<String, String[]> entry : keys) {
                    for (String value : entry.getValue()) {
                        paramsModel.addRow(new RowResult(entry.getKey(), value));
                    }
                }
            }
            if (isMultipart && StringUtils.isNotBlank(queryPost)) {
                String contentType = lhm.get(HTTPConstants.HEADER_CONTENT_TYPE);
                String boundaryString = extractBoundary(contentType);
                MultipartUrlConfig urlconfig = new MultipartUrlConfig(boundaryString);
                urlconfig.parseArguments(queryPost);
                for (JMeterProperty prop : urlconfig.getArguments()) {
                    Argument arg = (Argument) prop.getObjectValue();
                    paramsModel.addRow(new RowResult(arg.getName(), arg.getValue()));
                }
            }
        }
        // Display cookie in headers table (same location on http protocol)
        String cookie = sampleResult.getCookies();
        if (cookie != null && cookie.length() > 0) {
            headersModel.addRow(new RowResult(//$NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_table_request_http_cookie"), sampleResult.getCookies()));
        }
    } else {
        // add a message when no http sample
        requestModel.addRow(new //$NON-NLS-1$
        RowResult(//$NON-NLS-1$
        "", //$NON-NLS-1$
        JMeterUtils.getResString("view_results_table_request_http_nohttp")));
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) URL(java.net.URL) RowResult(org.apache.jmeter.visualizers.SamplerResultTab.RowResult) Entry(java.util.Map.Entry) MultipartUrlConfig(org.apache.jmeter.protocol.http.config.MultipartUrlConfig) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult)

Example 2 with MultipartUrlConfig

use of org.apache.jmeter.protocol.http.config.MultipartUrlConfig in project jmeter by apache.

the class DefaultSamplerCreator method computeFromPostBody.

/**
     * Compute sampler informations from Request Header
     * @param sampler {@link HTTPSamplerBase}
     * @param request {@link HttpRequestHdr}
     * @throws Exception when something fails
     */
protected void computeFromPostBody(HTTPSamplerBase sampler, HttpRequestHdr request) throws Exception {
    // to do parse the rest of the request if it is not a GET request
    if ((!HTTPConstants.CONNECT.equals(request.getMethod())) && (!HTTPConstants.GET.equals(request.getMethod()))) {
        // Check if it was a multipart http post request
        final String contentType = request.getContentType();
        MultipartUrlConfig urlConfig = request.getMultipartConfig(contentType);
        String contentEncoding = sampler.getContentEncoding();
        // Get the post data using the content encoding of the request
        String postData = null;
        if (log.isDebugEnabled()) {
            if (!StringUtils.isEmpty(contentEncoding)) {
                log.debug("Using encoding " + contentEncoding + " for request body");
            } else {
                log.debug("No encoding found, using JRE default encoding for request body");
            }
        }
        if (!StringUtils.isEmpty(contentEncoding)) {
            postData = new String(request.getRawPostData(), contentEncoding);
        } else {
            // Use default encoding
            postData = new String(request.getRawPostData(), PostWriter.ENCODING);
        }
        if (urlConfig != null) {
            urlConfig.parseArguments(postData);
            // Tell the sampler to do a multipart post
            sampler.setDoMultipartPost(true);
            // Remove the header for content-type and content-length, since
            // those values will most likely be incorrect when the sampler
            // performs the multipart request, because the boundary string
            // will change
            request.getHeaderManager().removeHeaderNamed(HttpRequestHdr.CONTENT_TYPE);
            request.getHeaderManager().removeHeaderNamed(HttpRequestHdr.CONTENT_LENGTH);
            // Set the form data
            sampler.setArguments(urlConfig.getArguments());
            // Set the file uploads
            sampler.setHTTPFiles(urlConfig.getHTTPFileArgs().asArray());
            // we are parsing browser input here
            sampler.setDoBrowserCompatibleMultipart(true);
        // used when postData is pure xml (eg. an xml-rpc call) or for PUT
        } else if (postData.trim().startsWith("<?") || HTTPConstants.PUT.equals(sampler.getMethod()) || isPotentialXml(postData)) {
            sampler.addNonEncodedArgument("", postData, "");
        } else if (contentType == null || (contentType.startsWith(HTTPConstants.APPLICATION_X_WWW_FORM_URLENCODED) && !isBinaryContent(contentType))) {
            // It is the most common post request, with parameter name and values
            // We also assume this if no content type is present, to be most backwards compatible,
            // but maybe we should only parse arguments if the content type is as expected
            //standard name=value postData
            sampler.parseArguments(postData.trim(), contentEncoding);
        } else if (postData.length() > 0) {
            if (isBinaryContent(contentType)) {
                try {
                    File tempDir = new File(getBinaryDirectory());
                    File out = File.createTempFile(request.getMethod(), getBinaryFileSuffix(), tempDir);
                    FileUtils.writeByteArrayToFile(out, request.getRawPostData());
                    HTTPFileArg[] files = { new HTTPFileArg(out.getPath(), "", contentType) };
                    sampler.setHTTPFiles(files);
                } catch (IOException e) {
                    log.warn("Could not create binary file: " + e);
                }
            } else {
                // Just put the whole postbody as the value of a parameter
                //used when postData is pure xml (ex. an xml-rpc call)
                sampler.addNonEncodedArgument("", postData, "");
            }
        }
    }
}
Also used : MultipartUrlConfig(org.apache.jmeter.protocol.http.config.MultipartUrlConfig) IOException(java.io.IOException) File(java.io.File) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg)

Aggregations

MultipartUrlConfig (org.apache.jmeter.protocol.http.config.MultipartUrlConfig)2 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Entry (java.util.Map.Entry)1 Argument (org.apache.jmeter.config.Argument)1 HTTPSampleResult (org.apache.jmeter.protocol.http.sampler.HTTPSampleResult)1 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)1 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)1 RowResult (org.apache.jmeter.visualizers.SamplerResultTab.RowResult)1