Search in sources :

Example 1 with HTTPSampleResult

use of org.apache.jmeter.protocol.http.sampler.HTTPSampleResult in project jmeter by apache.

the class ProxyControl method deliverSampler.

/**
 * Receives the recorded sampler from the proxy server for placing in the
 * test tree; this is skipped if the sampler is null (e.g. for recording SSL errors)
 * Always sends the result to any registered sample listeners.
 *
 * @param sampler      the sampler, may be null
 * @param testElements the test elements to be added (e.g. header manager) under the Sampler
 * @param result       the sample result, not null
 *                     TODO param serverResponse to be added to allow saving of the
 *                     server's response while recording.
 */
public synchronized void deliverSampler(final HTTPSamplerBase sampler, final TestElement[] testElements, final SampleResult result) {
    boolean notifySampleListeners = true;
    if (sampler != null) {
        if (USE_REDIRECT_DISABLING && (samplerRedirectAutomatically || samplerFollowRedirects) && result instanceof HTTPSampleResult) {
            final HTTPSampleResult httpSampleResult = (HTTPSampleResult) result;
            final String urlAsString = httpSampleResult.getUrlAsString();
            if (urlAsString.equals(LAST_REDIRECT)) {
                // the url matches the last redirect
                sampler.setEnabled(false);
                sampler.setComment("Detected a redirect from the previous sample");
            } else {
                // this is not the result of a redirect
                // so break the chain
                LAST_REDIRECT = null;
            }
            if (httpSampleResult.isRedirect()) {
                // Save Location so resulting sample can be disabled
                if (LAST_REDIRECT == null) {
                    sampler.setComment("Detected the start of a redirect chain");
                }
                LAST_REDIRECT = httpSampleResult.getRedirectLocation();
            } else {
                LAST_REDIRECT = null;
            }
        }
        if (filterContentType(result) && filterUrl(sampler)) {
            JMeterTreeNode myTarget = findTargetControllerNode();
            // OK, because find only returns correct element types
            @SuppressWarnings("unchecked") Collection<ConfigTestElement> defaultConfigurations = (Collection<ConfigTestElement>) findApplicableElements(myTarget, ConfigTestElement.class, false);
            // OK, because find only returns correct element types
            @SuppressWarnings("unchecked") Collection<Arguments> userDefinedVariables = (Collection<Arguments>) findApplicableElements(myTarget, Arguments.class, true);
            removeValuesFromSampler(sampler, defaultConfigurations);
            replaceValues(sampler, testElements, userDefinedVariables);
            sampler.setAutoRedirects(samplerRedirectAutomatically);
            sampler.setFollowRedirects(samplerFollowRedirects);
            sampler.setUseKeepAlive(useKeepAlive);
            sampler.setImageParser(samplerDownloadImages);
            Authorization authorization = createAuthorization(testElements, result);
            if (authorization != null) {
                setAuthorization(authorization, myTarget);
            }
            sampleQueue.add(new SamplerInfo(sampler, testElements, myTarget, getPrefixHTTPSampleName(), groupingMode));
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Sample excluded based on url or content-type: {} - {}", result.getUrlAsString(), result.getContentType());
            }
            notifySampleListeners = notifyChildSamplerListenersOfFilteredSamples;
            result.setSampleLabel("[" + result.getSampleLabel() + "]");
        }
    }
    if (notifySampleListeners) {
        // SampleEvent is not passed JMeterVariables, because they don't make sense for Proxy Recording
        notifySampleListeners(new SampleEvent(result, "WorkBench"));
    } else {
        log.debug("Sample not delivered to Child Sampler Listener based on url or content-type: {} - {}", result.getUrlAsString(), result.getContentType());
    }
}
Also used : Arguments(org.apache.jmeter.config.Arguments) SampleEvent(org.apache.jmeter.samplers.SampleEvent) Authorization(org.apache.jmeter.protocol.http.control.Authorization) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) Collection(java.util.Collection) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement)

Example 2 with HTTPSampleResult

use of org.apache.jmeter.protocol.http.sampler.HTTPSampleResult in project jmeter by apache.

the class AnchorModifier method process.

/**
 * Modifies an Entry object based on HTML response text.
 */
@Override
public void process() {
    JMeterContext context = getThreadContext();
    Sampler sam = context.getCurrentSampler();
    SampleResult res = context.getPreviousResult();
    HTTPSamplerBase sampler;
    HTTPSampleResult result;
    if (!(sam instanceof HTTPSamplerBase) || !(res instanceof HTTPSampleResult)) {
        log.info("Can't apply HTML Link Parser when the previous" + " sampler run is not an HTTP Request.");
        return;
    } else {
        sampler = (HTTPSamplerBase) sam;
        result = (HTTPSampleResult) res;
    }
    List<HTTPSamplerBase> potentialLinks = new ArrayList<>();
    String responseText = result.getResponseDataAsString();
    // $NON-NLS-1$
    int index = responseText.indexOf('<');
    if (index == -1) {
        index = 0;
    }
    if (log.isDebugEnabled()) {
        log.debug("Check for matches against: " + sampler.toString());
    }
    Document html = (Document) HtmlParsingUtils.getDOM(responseText.substring(index));
    addAnchorUrls(html, result, sampler, potentialLinks);
    addFormUrls(html, result, sampler, potentialLinks);
    addFramesetUrls(html, result, sampler, potentialLinks);
    if (!potentialLinks.isEmpty()) {
        HTTPSamplerBase url = potentialLinks.get(ThreadLocalRandom.current().nextInt(potentialLinks.size()));
        if (log.isDebugEnabled()) {
            log.debug("Selected: " + url.toString());
        }
        sampler.setDomain(url.getDomain());
        sampler.setPath(url.getPath());
        if (url.getMethod().equals(HTTPConstants.POST)) {
            for (JMeterProperty jMeterProperty : sampler.getArguments()) {
                Argument arg = (Argument) jMeterProperty.getObjectValue();
                modifyArgument(arg, url.getArguments());
            }
        } else {
            sampler.setArguments(url.getArguments());
        }
        sampler.setProtocol(url.getProtocol());
    } else {
        log.debug("No matches found");
    }
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) JMeterContext(org.apache.jmeter.threads.JMeterContext) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) Sampler(org.apache.jmeter.samplers.Sampler) ArrayList(java.util.ArrayList) SampleResult(org.apache.jmeter.samplers.SampleResult) HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) Document(org.w3c.dom.Document) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)

Example 3 with HTTPSampleResult

use of org.apache.jmeter.protocol.http.sampler.HTTPSampleResult in project jmeter by apache.

the class TestCacheManagerBase method testCacheHEAD.

@Test
public void testCacheHEAD() throws Exception {
    this.cacheManager.setUseExpires(true);
    this.cacheManager.testIterationStart(null);
    assertNoSuchEntry();
    setExpires(makeDate(new Date(System.currentTimeMillis())));
    setCacheControl("public, max-age=5");
    HTTPSampleResult sampleResultHEAD = getSampleResultWithSpecifiedResponseCode("200");
    sampleResultHEAD.setHTTPMethod("HEAD");
    cacheResult(sampleResultHEAD);
    assertNoSuchEntry();
}
Also used : HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with HTTPSampleResult

use of org.apache.jmeter.protocol.http.sampler.HTTPSampleResult in project jmeter by apache.

the class TestAnchorModifier method testSimpleParse4.

@Test
public void testSimpleParse4() throws Exception {
    HTTPSamplerBase config = makeUrlConfig("/subdir/index\\..*");
    HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
    String responseText = "<html><head><title>Test page</title></head><body>" + "<A HREF=\"index.html\">Goto index page</A></body></html>";
    HTTPSampleResult result = new HTTPSampleResult();
    result.setResponseData(responseText, null);
    result.setSampleLabel(context.toString());
    result.setURL(context.getUrl());
    jmctx.setCurrentSampler(context);
    jmctx.setCurrentSampler(config);
    jmctx.setPreviousResult(result);
    parser.process();
    String newUrl = config.getUrl().toString();
    assertEquals("http://www.apache.org/subdir/index.html", newUrl);
}
Also used : HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase) Test(org.junit.jupiter.api.Test)

Example 5 with HTTPSampleResult

use of org.apache.jmeter.protocol.http.sampler.HTTPSampleResult in project jmeter by apache.

the class TestAnchorModifier method testSimpleParse2.

@Test
public void testSimpleParse2() throws Exception {
    HTTPSamplerBase config = makeUrlConfig("/index\\.html");
    HTTPSamplerBase context = makeContext("http://www.apache.org/subdir/previous.html");
    String responseText = "<html><head><title>Test page</title></head><body>" + "<a href=\"/index.html\">Goto index page</a>" + "hfdfjiudfjdfjkjfkdjf" + "<b>bold text</b><a href=lowerdir/index.html>lower</a>" + "</body></html>";
    HTTPSampleResult result = new HTTPSampleResult();
    result.setResponseData(responseText, null);
    result.setSampleLabel(context.toString());
    result.setURL(context.getUrl());
    jmctx.setCurrentSampler(context);
    jmctx.setCurrentSampler(config);
    jmctx.setPreviousResult(result);
    parser.process();
    String newUrl = config.getUrl().toString();
    assertTrue("http://www.apache.org/index.html".equals(newUrl) || "http://www.apache.org/subdir/lowerdir/index.html".equals(newUrl));
}
Also used : HTTPSampleResult(org.apache.jmeter.protocol.http.sampler.HTTPSampleResult) HTTPSamplerBase(org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase) Test(org.junit.jupiter.api.Test)

Aggregations

HTTPSampleResult (org.apache.jmeter.protocol.http.sampler.HTTPSampleResult)24 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)14 Test (org.junit.jupiter.api.Test)14 URL (java.net.URL)3 Argument (org.apache.jmeter.config.Argument)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Arguments (org.apache.jmeter.config.Arguments)1 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)1 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)1 MultipartUrlConfig (org.apache.jmeter.protocol.http.config.MultipartUrlConfig)1 Authorization (org.apache.jmeter.protocol.http.control.Authorization)1 SampleEvent (org.apache.jmeter.samplers.SampleEvent)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1