Search in sources :

Example 1 with JMeterProperty

use of org.apache.jmeter.testelement.property.JMeterProperty 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 JMeterProperty

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

the class JavaConfigGui method configureClassName.

/**
     * 
     */
private void configureClassName() {
    String className = classNameLabeledChoice.getText().trim();
    try {
        JavaSamplerClient client = (JavaSamplerClient) Class.forName(className, true, Thread.currentThread().getContextClassLoader()).newInstance();
        Arguments currArgs = new Arguments();
        argsPanel.modifyTestElement(currArgs);
        Map<String, String> currArgsMap = currArgs.getArgumentsAsMap();
        Arguments newArgs = new Arguments();
        Arguments testParams = null;
        try {
            testParams = client.getDefaultParameters();
        } catch (AbstractMethodError e) {
            log.warn("JavaSamplerClient doesn't implement " + "getDefaultParameters.  Default parameters won't " + "be shown.  Please update your client class: " + className);
        }
        if (testParams != null) {
            for (JMeterProperty jMeterProperty : testParams.getArguments()) {
                Argument arg = (Argument) jMeterProperty.getObjectValue();
                String name = arg.getName();
                String value = arg.getValue();
                // values that they did in the original test.
                if (currArgsMap.containsKey(name)) {
                    String newVal = currArgsMap.get(name);
                    if (newVal != null && newVal.length() > 0) {
                        value = newVal;
                    }
                }
                newArgs.addArgument(name, value);
            }
        }
        argsPanel.configure(newArgs);
        warningLabel.setVisible(false);
    } catch (Exception e) {
        log.error("Error getting argument list for " + className, e);
        warningLabel.setVisible(true);
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) Arguments(org.apache.jmeter.config.Arguments) JavaSamplerClient(org.apache.jmeter.protocol.java.sampler.JavaSamplerClient)

Example 3 with JMeterProperty

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

the class HTTPSamplerBase method downloadPageResources.

/**
     * Download the resources of an HTML page.
     *
     * @param pRes
     *            result of the initial request - must contain an HTML response
     * @param container
     *            for storing the results, if any
     * @param frameDepth
     *            Depth of this target in the frame structure. Used only to
     *            prevent infinite recursion.
     * @return res if no resources exist, otherwise the "Container" result with one subsample per request issued
     */
protected HTTPSampleResult downloadPageResources(final HTTPSampleResult pRes, final HTTPSampleResult container, final int frameDepth) {
    HTTPSampleResult res = pRes;
    Iterator<URL> urls = null;
    try {
        final byte[] responseData = res.getResponseData();
        if (responseData.length > 0) {
            // Bug 39205
            final LinkExtractorParser parser = getParser(res);
            if (parser != null) {
                String userAgent = getUserAgent(res);
                urls = parser.getEmbeddedResourceURLs(userAgent, responseData, res.getURL(), res.getDataEncodingWithDefault());
            }
        }
    } catch (LinkExtractorParseException e) {
        // Don't break the world just because this failed:
        res.addSubResult(errorResult(e, new HTTPSampleResult(res)));
        setParentSampleSuccess(res, false);
    }
    HTTPSampleResult lContainer = container;
    // Iterate through the URLs and download each image:
    if (urls != null && urls.hasNext()) {
        if (lContainer == null) {
            lContainer = new HTTPSampleResult(res);
            lContainer.addRawSubResult(res);
        }
        res = lContainer;
        // Get the URL matcher
        String re = getEmbeddedUrlRE();
        Perl5Matcher localMatcher = null;
        Pattern pattern = null;
        if (re.length() > 0) {
            try {
                pattern = JMeterUtils.getPattern(re);
                // don't fetch unless pattern compiles
                localMatcher = JMeterUtils.getMatcher();
            } catch (MalformedCachePatternException e) {
                // NOSONAR
                log.warn("Ignoring embedded URL match string: " + e.getMessage());
            }
        }
        // For concurrent get resources
        final List<Callable<AsynSamplerResultHolder>> list = new ArrayList<>();
        // init with default value
        int maxConcurrentDownloads = CONCURRENT_POOL_SIZE;
        boolean isConcurrentDwn = isConcurrentDwn();
        if (isConcurrentDwn) {
            try {
                maxConcurrentDownloads = Integer.parseInt(getConcurrentPool());
            } catch (NumberFormatException nfe) {
                log.warn(// $NON-NLS-1$
                "Concurrent download resources selected, " + // $NON-NLS-1$
                "but pool size value is bad. Use default value");
            }
            // no need to use another thread, do the sample on the current thread
            if (maxConcurrentDownloads == 1) {
                log.warn("Number of parallel downloads set to 1, (sampler name=" + getName() + ")");
                isConcurrentDwn = false;
            }
        }
        while (urls.hasNext()) {
            // See catch clause below
            Object binURL = urls.next();
            try {
                URL url = (URL) binURL;
                if (url == null) {
                    log.warn("Null URL detected (should not happen)");
                } else {
                    try {
                        url = escapeIllegalURLCharacters(url);
                    } catch (Exception e) {
                        // NOSONAR
                        res.addSubResult(errorResult(new Exception(url.toString() + " is not a correct URI", e), new HTTPSampleResult(res)));
                        setParentSampleSuccess(res, false);
                        continue;
                    }
                    // I don't think localMatcher can be null here, but check just in case
                    if (pattern != null && localMatcher != null && !localMatcher.matches(url.toString(), pattern)) {
                        // we have a pattern and the URL does not match, so skip it
                        continue;
                    }
                    try {
                        url = url.toURI().normalize().toURL();
                    } catch (MalformedURLException | URISyntaxException e) {
                        res.addSubResult(errorResult(new Exception(url.toString() + " URI can not be normalized", e), new HTTPSampleResult(res)));
                        setParentSampleSuccess(res, false);
                        continue;
                    }
                    if (isConcurrentDwn) {
                        // if concurrent download emb. resources, add to a list for async gets later
                        list.add(new ASyncSample(url, HTTPConstants.GET, false, frameDepth + 1, getCookieManager(), this));
                    } else {
                        // default: serial download embedded resources
                        HTTPSampleResult binRes = sample(url, HTTPConstants.GET, false, frameDepth + 1);
                        res.addSubResult(binRes);
                        setParentSampleSuccess(res, res.isSuccessful() && (binRes == null || binRes.isSuccessful()));
                    }
                }
            } catch (ClassCastException e) {
                // NOSONAR
                res.addSubResult(errorResult(new Exception(binURL + " is not a correct URI", e), new HTTPSampleResult(res)));
                setParentSampleSuccess(res, false);
            }
        }
        // IF for download concurrent embedded resources
        if (isConcurrentDwn && !list.isEmpty()) {
            ResourcesDownloader resourcesDownloader = ResourcesDownloader.getInstance();
            try {
                // sample all resources
                final List<Future<AsynSamplerResultHolder>> retExec = resourcesDownloader.invokeAllAndAwaitTermination(maxConcurrentDownloads, list);
                CookieManager cookieManager = getCookieManager();
                // add result to main sampleResult
                for (Future<AsynSamplerResultHolder> future : retExec) {
                    // this call will not block as the futures return by invokeAllAndAwaitTermination
                    //   are either done or cancelled
                    AsynSamplerResultHolder binRes = future.get();
                    if (cookieManager != null) {
                        CollectionProperty cookies = binRes.getCookies();
                        for (JMeterProperty jMeterProperty : cookies) {
                            Cookie cookie = (Cookie) jMeterProperty.getObjectValue();
                            cookieManager.add(cookie);
                        }
                    }
                    res.addSubResult(binRes.getResult());
                    setParentSampleSuccess(res, res.isSuccessful() && (binRes.getResult() != null ? binRes.getResult().isSuccessful() : true));
                }
            } catch (InterruptedException ie) {
                // $NON-NLS-1$
                log.warn("Interrupted fetching embedded resources", ie);
                Thread.currentThread().interrupt();
            } catch (ExecutionException ee) {
                // $NON-NLS-1$
                log.warn("Execution issue when fetching embedded resources", ee);
            }
        }
    }
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) Callable(java.util.concurrent.Callable) LinkExtractorParser(org.apache.jmeter.protocol.http.parser.LinkExtractorParser) ExecutionException(java.util.concurrent.ExecutionException) AsynSamplerResultHolder(org.apache.jmeter.protocol.http.sampler.ResourcesDownloader.AsynSamplerResultHolder) LinkExtractorParseException(org.apache.jmeter.protocol.http.parser.LinkExtractorParseException) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) Cookie(org.apache.jmeter.protocol.http.control.Cookie) Pattern(org.apache.oro.text.regex.Pattern) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) MalformedURLException(java.net.MalformedURLException) LinkExtractorParseException(org.apache.jmeter.protocol.http.parser.LinkExtractorParseException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future)

Example 4 with JMeterProperty

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

the class HTTPSamplerBase method getSendParameterValuesAsPostBody.

/**
     * Determine if none of the parameters have a name, and if that is the case,
     * it means that the parameter values should be sent as the entity body
     *
     * @return {@code true} if there are parameters and none of these have a
     *         name specified, or {@link HTTPSamplerBase#getPostBodyRaw()} returns
     *         {@code true}
     */
public boolean getSendParameterValuesAsPostBody() {
    if (getPostBodyRaw()) {
        return true;
    } else {
        boolean hasArguments = false;
        for (JMeterProperty jMeterProperty : getArguments()) {
            hasArguments = true;
            HTTPArgument arg = (HTTPArgument) jMeterProperty.getObjectValue();
            if (arg.getName() != null && arg.getName().length() > 0) {
                return false;
            }
        }
        return hasArguments;
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument)

Example 5 with JMeterProperty

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

the class AjpSampler method setConnectionCookies.

private String setConnectionCookies(URL url, CookieManager cookies) {
    String cookieHeader = null;
    if (cookies != null) {
        cookieHeader = cookies.getCookieHeaderForURL(url);
        for (JMeterProperty jMeterProperty : cookies.getCookies()) {
            Cookie cookie = (Cookie) (jMeterProperty.getObjectValue());
            // Cookie
            setInt(0xA009);
            //$NON-NLS-1$
            setString(cookie.getName() + "=" + cookie.getValue());
        }
    }
    return cookieHeader;
}
Also used : Cookie(org.apache.jmeter.protocol.http.control.Cookie) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty)

Aggregations

JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)120 Test (org.junit.Test)30 CollectionProperty (org.apache.jmeter.testelement.property.CollectionProperty)29 NullProperty (org.apache.jmeter.testelement.property.NullProperty)24 StringProperty (org.apache.jmeter.testelement.property.StringProperty)23 ArrayList (java.util.ArrayList)14 PropertyIterator (org.apache.jmeter.testelement.property.PropertyIterator)14 Argument (org.apache.jmeter.config.Argument)11 File (java.io.File)8 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)8 Arguments (org.apache.jmeter.config.Arguments)7 TestElement (org.apache.jmeter.testelement.TestElement)7 HTTPFileArg (org.apache.jmeter.protocol.http.util.HTTPFileArg)6 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)5 IOException (java.io.IOException)4 URL (java.net.URL)4 List (java.util.List)4 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MalformedURLException (java.net.MalformedURLException)3