Class HTTPSamplerBase

All Implemented Interfaces:
Serializable, Cloneable, ConfigMergabilityIndicator, Replaceable, Searchable, HTTPConstantsInterface, Sampler, org.apache.jmeter.testelement.TestElement, TestIterationListener, TestStateListener, ThreadListener
Direct Known Subclasses:
AjpSampler, HTTPSampler, HTTPSamplerProxy

Common constants and methods for HTTP samplers
See Also:
  • Field Details

  • Constructor Details

    • HTTPSamplerBase

      protected HTTPSamplerBase()
  • Method Details

    • getSchema

      public org.apache.jmeter.protocol.http.sampler.HTTPSamplerBaseSchema getSchema()
      Specified by:
      getSchema in interface org.apache.jmeter.testelement.TestElement
    • getProps

      public org.apache.jmeter.testelement.schema.PropertiesAccessor<? extends HTTPSamplerBase,? extends org.apache.jmeter.protocol.http.sampler.HTTPSamplerBaseSchema> getProps()
      Specified by:
      getProps in interface org.apache.jmeter.testelement.TestElement
    • getSourceTypeList

      public static String[] getSourceTypeList()
    • getSendFileAsPostBody

      public boolean getSendFileAsPostBody()
      Determine if the file should be sent as the entire Content body, i.e. without any additional wrapping.
      Returns:
      true if specified file is to be sent as the body, i.e. there is a single file entry which has a non-empty path and an empty Parameter name.
    • getSendParameterValuesAsPostBody

      public boolean 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
      Returns:
      true if there are parameters and none of these have a name specified, or getPostBodyRaw() returns true
    • getUseMultipartForPost

      public boolean getUseMultipartForPost()
      Determine if we should use multipart/form-data or application/x-www-form-urlencoded for the post
      Returns:
      true if multipart/form-data should be used and method is POST
    • getUseMultipart

      public boolean getUseMultipart()
      Determine if we should use multipart/form-data or application/x-www-form-urlencoded for the post
      Returns:
      true if multipart/form-data should be used and method is POST
    • setProtocol

      public void setProtocol(String value)
    • getProtocol

      public String getProtocol()
      Gets the protocol, with default.
      Returns:
      the protocol
    • setPath

      public void setPath(String path)
      Sets the Path attribute of the UrlConfig object Also calls parseArguments to extract and store any query arguments
      Parameters:
      path - The new Path value
    • setPath

      public void setPath(String path, String contentEncoding)
      Sets the PATH property; if the request is a GET or DELETE (and the path does not start with http[s]://) it also calls parseArguments(String, String) to extract and store any query arguments.
      Parameters:
      path - The new Path value
      contentEncoding - The encoding used for the querystring parameter values
    • getPath

      public String getPath()
    • setFollowRedirects

      public void setFollowRedirects(boolean value)
    • getFollowRedirects

      public boolean getFollowRedirects()
    • setAutoRedirects

      public void setAutoRedirects(boolean value)
    • getAutoRedirects

      public boolean getAutoRedirects()
    • setMethod

      public void setMethod(String value)
    • getMethod

      public String getMethod()
    • setContentEncoding

      public void setContentEncoding(String charsetName)
      Sets the value of the encoding to be used for the content.
      Parameters:
      charsetName - the name of the encoding to be used
    • getContentEncoding

      public String getContentEncoding()
      Returns:
      the encoding of the content, i.e. its charset name
    • setUseKeepAlive

      public void setUseKeepAlive(boolean value)
    • getUseKeepAlive

      public boolean getUseKeepAlive()
    • setDoMultipartPost

      @Deprecated public void setDoMultipartPost(boolean value)
      Parameters:
      value - flag whether multiparts should be used
    • getDoMultipartPost

      @Deprecated public boolean getDoMultipartPost()
      Deprecated.
      Returns:
      flag whether multiparts should be used
    • setDoMultipart

      public void setDoMultipart(boolean value)
    • getDoMultipart

      public boolean getDoMultipart()
    • setDoBrowserCompatibleMultipart

      public void setDoBrowserCompatibleMultipart(boolean value)
    • getDoBrowserCompatibleMultipart

      public boolean getDoBrowserCompatibleMultipart()
    • setMonitor

      public void setMonitor(String value)
    • setMonitor

      public void setMonitor(boolean truth)
    • getMonitor

      @Deprecated public String getMonitor()
      Deprecated.
      since 3.2 always returns false
      Returns:
      boolean
    • isMonitor

      @Deprecated public boolean isMonitor()
      Deprecated.
      since 3.2 always returns false
      Returns:
      boolean
    • setImplementation

      public void setImplementation(String value)
    • getImplementation

      public String getImplementation()
    • useMD5

      public boolean useMD5()
    • setMD5

      public void setMD5(boolean value)
    • addEncodedArgument

      public void addEncodedArgument(String name, String value)
      Add an argument which has already been encoded
      Parameters:
      name - name of the argument
      value - value of the argument
    • addEncodedArgument

      public void addEncodedArgument(String name, String value, String metaData, String contentEncoding)
      Creates an HTTPArgument and adds it to the current set getArguments() of arguments.
      Parameters:
      name - - the parameter name
      value - - the parameter value
      metaData - - normally just '='
      contentEncoding - - the encoding, may be null
    • addEncodedArgument

      public void addEncodedArgument(String name, String value, String metaData)
    • addNonEncodedArgument

      public void addNonEncodedArgument(String name, String value, String metadata)
    • addNonEncodedArgument

      public void addNonEncodedArgument(String name, String value, String metadata, String contentType)
    • addArgument

      public void addArgument(String name, String value)
    • addArgument

      public void addArgument(String name, String value, String metadata)
    • hasArguments

      public boolean hasArguments()
    • addTestElement

      public void addTestElement(org.apache.jmeter.testelement.TestElement el)
      Description copied from class: AbstractTestElement
      Specified by:
      addTestElement in interface org.apache.jmeter.testelement.TestElement
      Overrides:
      addTestElement in class AbstractTestElement
    • clearTestElementChildren

      public void clearTestElementChildren()

      Default implementation - does nothing

      Clears the Header Manager property so subsequent loops don't keep merging more elements

      Specified by:
      clearTestElementChildren in interface org.apache.jmeter.testelement.TestElement
      Overrides:
      clearTestElementChildren in class AbstractTestElement
    • setPort

      public void setPort(int value)
    • getDefaultPort

      public static int getDefaultPort(String protocol, int port)
      Get the port number for a URL, applying defaults if necessary. (Called by CookieManager.)
      Parameters:
      protocol - from URL.getProtocol()
      port - number from URL.getPort()
      Returns:
      the default port for the protocol
    • getPortIfSpecified

      public int getPortIfSpecified()
      Get the port number from the port string, allowing for trailing blanks.
      Returns:
      port number or UNSPECIFIED_PORT (== 0)
    • isProtocolDefaultPort

      public boolean isProtocolDefaultPort()
      Tell whether the default port for the specified protocol is used
      Returns:
      true if the default port number for the protocol is used, false otherwise
    • getPort

      public int getPort()
      Get the port; apply the default for the protocol if necessary.
      Returns:
      the port number, with default applied if required.
    • setDomain

      public void setDomain(String value)
    • getDomain

      public String getDomain()
    • setConnectTimeout

      public void setConnectTimeout(String value)
    • getConnectTimeout

      public int getConnectTimeout()
    • setResponseTimeout

      public void setResponseTimeout(String value)
    • getResponseTimeout

      public int getResponseTimeout()
    • getProxyScheme

      public String getProxyScheme()
    • setProxyScheme

      public void setProxyScheme(String schema)
    • getProxyHost

      public String getProxyHost()
    • setProxyHost

      public void setProxyHost(String host)
    • getProxyPortInt

      public int getProxyPortInt()
    • setProxyPortInt

      public void setProxyPortInt(String port)
    • getProxyUser

      public String getProxyUser()
    • setProxyUser

      public void setProxyUser(String user)
    • getProxyPass

      public String getProxyPass()
    • setProxyPass

      public void setProxyPass(String pass)
    • setArguments

      public final void setArguments(Arguments value)
    • getArguments

      public Arguments getArguments()
    • setPostBodyRaw

      public void setPostBodyRaw(boolean value)
      Parameters:
      value - Boolean that indicates body will be sent as is
    • getPostBodyRaw

      public boolean getPostBodyRaw()
      Returns:
      boolean that indicates body will be sent as is
    • setAuthManager

      public void setAuthManager(AuthManager value)
    • getAuthManager

      public AuthManager getAuthManager()
    • setHeaderManager

      public void setHeaderManager(HeaderManager value)
    • getHeaderManager

      public HeaderManager getHeaderManager()
    • setCookieManager

      public void setCookieManager(CookieManager value)
    • getCookieManager

      public CookieManager getCookieManager()
    • setKeystoreConfig

      public void setKeystoreConfig(KeystoreConfig value)
    • getKeystoreConfig

      public KeystoreConfig getKeystoreConfig()
    • setCacheManager

      public void setCacheManager(CacheManager value)
    • getCacheManager

      public CacheManager getCacheManager()
    • getDNSResolver

      public DNSCacheManager getDNSResolver()
    • setDNSResolver

      public void setDNSResolver(DNSCacheManager cacheManager)
    • isImageParser

      public boolean isImageParser()
    • setImageParser

      public void setImageParser(boolean parseImages)
    • getEmbeddedUrlRE

      public String getEmbeddedUrlRE()
      Get the regular expression URLs must match.
      Returns:
      regular expression (or empty) string
    • setEmbeddedUrlRE

      public void setEmbeddedUrlRE(String regex)
    • getEmbededUrlExcludeRE

      public String getEmbededUrlExcludeRE()
      Returns:
      the regular (as String) expression that embedded URLs must not match
    • setEmbeddedUrlExcludeRE

      public void setEmbeddedUrlExcludeRE(String regex)
    • errorResult

      protected HTTPSampleResult errorResult(Throwable e, HTTPSampleResult res)
      Populates the provided HTTPSampleResult with details from the Exception. Does not create a new instance, so should not be used directly to add a subsample.
      Parameters:
      e - Exception representing the error.
      res - SampleResult to be modified
      Returns:
      the modified sampling result containing details of the Exception.
    • getUrl

      public URL getUrl() throws MalformedURLException
      Get the URL, built from its component parts.

      As a special case, if the path starts with "http[s]://", then the path is assumed to be the entire URL.

      Returns:
      The URL to be requested by this sampler.
      Throws:
      MalformedURLException - if url is malformed
    • getQueryString

      public String getQueryString()
      Gets the QueryString attribute of the UrlConfig object, using UTF-8 to encode the URL
      Returns:
      the QueryString value
    • getQueryString

      public String getQueryString(String contentEncoding)
      Gets the QueryString attribute of the UrlConfig object, using the specified encoding to encode the parameter values put into the URL
      Parameters:
      contentEncoding - the encoding to use for encoding parameter values
      Returns:
      the QueryString value
    • parseArguments

      public void parseArguments(String queryString, String contentEncoding)
      This method allows a proxy server to send over the raw text from a browser's output stream to be parsed and stored correctly into the UrlConfig object. For each name found, addArgument() is called
      Parameters:
      queryString - - the query string, might be the post body of a http post request.
      contentEncoding - - the content encoding of the query string; if non-null then it is used to decode the
    • parseArguments

      public void parseArguments(String queryString)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • sample

      public SampleResult sample(Entry e)
      Do a sampling and return its results.
      Specified by:
      sample in interface Sampler
      Parameters:
      e - Entry to be sampled
      Returns:
      results of the sampling
    • sample

      public SampleResult sample()
      Perform a sample, and return the results
      Returns:
      results of the sampling
    • sample

      protected abstract HTTPSampleResult sample(URL u, String method, boolean areFollowingRedirect, int depth)
      Samples the URL passed in and stores the result in HTTPSampleResult, following redirects and downloading page resources as appropriate.

      When getting a redirect target, redirects are not followed and resources are not downloaded. The caller will take care of this.

      Parameters:
      u - URL to sample
      method - HTTP method: GET, POST,...
      areFollowingRedirect - whether we're getting a redirect target
      depth - Depth of this target in the frame structure. Used only to prevent infinite recursion.
      Returns:
      results of the sampling, can be null if u is in CacheManager
    • downloadPageResources

      protected HTTPSampleResult downloadPageResources(HTTPSampleResult pRes, HTTPSampleResult container, int frameDepth)
      Download the resources of an HTML page.
      Parameters:
      pRes - result of the initial request - must contain an HTML response
      container - for storing the results, if any
      frameDepth - Depth of this target in the frame structure. Used only to prevent infinite recursion.
      Returns:
      res if no resources exist, otherwise the "Container" result with one subsample per request issued
    • encodeSpaces

      protected String encodeSpaces(String path)
    • testEnded

      public void testEnded()

      Called once for all threads after the end of a test. This will use the same element instances as at the start of the test.

      N.B. testStarted() and testEnded() are called from different threads.

      Specified by:
      testEnded in interface TestStateListener
      See Also:
    • testEnded

      public void testEnded(String host)

      Called once for all threads after the end of a test. This will use the same element instances as at the start of the test.

      N.B. testStarted() and testEnded() are called from different threads.

      Specified by:
      testEnded in interface TestStateListener
      Parameters:
      host - name of host
      See Also:
    • testStarted

      public void testStarted()

      Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

      N.B. testStarted() and testEnded() are called from different threads.

      Specified by:
      testStarted in interface TestStateListener
      See Also:
    • testStarted

      public void testStarted(String host)

      Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.

      N.B. testStarted() and testEnded() are called from different threads.

      Specified by:
      testStarted in interface TestStateListener
      Parameters:
      host - name of host
      See Also:
    • followRedirects

      protected HTTPSampleResult followRedirects(HTTPSampleResult res, int frameDepth)
      Iteratively download the redirect targets of a redirect response.

      The returned result will contain one subsample for each request issued, including the original one that was passed in. It will be an HTTPSampleResult that should mostly look as if the final destination of the redirect chain had been obtained in a single shot.

      Parameters:
      res - result of the initial request - must be a redirect response
      frameDepth - Depth of this target in the frame structure. Used only to prevent infinite recursion.
      Returns:
      "Container" result with one subsample per request issued
    • resultProcessing

      protected HTTPSampleResult resultProcessing(boolean pAreFollowingRedirect, int frameDepth, HTTPSampleResult pRes)
      Follow redirects and download page resources if appropriate. this works, but the container stuff here is what's doing it. followRedirects() is actually doing the work to make sure we have only one container to make this work more naturally, I think this method - sample() - needs to take an HTTPSamplerResult container parameter instead of a boolean:areFollowingRedirect.
      Parameters:
      pAreFollowingRedirect - flag whether we are getting a redirect target
      frameDepth - Depth of this target in the frame structure. Used only to prevent infinite recursion.
      pRes - sample result to process
      Returns:
      the sample result
    • isSuccessCode

      protected boolean isSuccessCode(int code)
      Determine if the HTTP status code is successful or not i.e. in range 200 to 399 inclusive
      Parameters:
      code - status code to check
      Returns:
      whether in range 200-399 or not
    • encodeBackSlashes

      protected static String encodeBackSlashes(String value)
    • getHTTPFiles

      public HTTPFileArg[] getHTTPFiles()
      Get the collection of files as a list. The list is built up from the filename/filefield/mimetype properties, plus any additional entries saved in the FILE_ARGS property. If there are no valid file entries, then an empty list is returned.
      Returns:
      an array of file arguments (never null)
    • getHTTPFileCount

      public int getHTTPFileCount()
    • setHTTPFiles

      public void setHTTPFiles(HTTPFileArg[] files)
      Saves the list of files. The first file is saved in the Filename/field/mimetype properties. Any additional files are saved in the FILE_ARGS array.
      Parameters:
      files - list of files to save
    • getValidMethodsAsArray

      public static String[] getValidMethodsAsArray()
    • isSecure

      public static boolean isSecure(String protocol)
    • isSecure

      public static boolean isSecure(URL url)
    • threadStarted

      public void threadStarted()
      Description copied from interface: ThreadListener
      Called for each thread before starting sampling. WARNING: this is called before any Config test elements are processed, so any properties they define will not have been merged in yet. See org.apache.jmeter.threads.JMeterThread#threadStarted()
      Specified by:
      threadStarted in interface ThreadListener
    • threadFinished

      public void threadFinished()
      Description copied from interface: ThreadListener
      Called for each thread after all samples have been processed. See org.apache.jmeter.threads.JMeterThread#threadFinished(org.apache.jmeter.engine.event.LoopIterationListener)
      Specified by:
      threadFinished in interface ThreadListener
    • testIterationStart

      public void testIterationStart(LoopIterationEvent event)
      Description copied from interface: TestIterationListener
      Each time through a Thread Group's test script, an iteration event is fired for each thread. This will be after the test elements have been cloned, so in general the instance will not be the same as the ones the start/end methods call.
      Specified by:
      testIterationStart in interface TestIterationListener
      Parameters:
      event - the iteration event
    • readResponse

      public byte[] readResponse(SampleResult sampleResult, InputStream in, long length) throws IOException
      Read response from the input stream, converting to MD5 digest if the useMD5 property is set.

      For the MD5 case, the result byte count is set to the size of the original response.

      Closes the inputStream

      Parameters:
      sampleResult - sample to store information about the response into
      in - input stream from which to read the response
      length - expected input length or zero
      Returns:
      the response or the MD5 of the response
      Throws:
      IOException - if reading the result fails
    • setIpSource

      public void setIpSource(String value)
      set IP source to use - does not apply to Java HTTP implementation currently
      Parameters:
      value - IP source to use
    • getIpSource

      public String getIpSource()
      get IP source to use - does not apply to Java HTTP implementation currently
      Returns:
      IP source to use
    • setIpSourceType

      public void setIpSourceType(int value)
      set IP/address source type to use
      Parameters:
      value - type of the IP/address source
    • getIpSourceType

      public int getIpSourceType()
      get IP/address source type to use
      Returns:
      address source type
    • isConcurrentDwn

      public boolean isConcurrentDwn()
      Return if used a concurrent thread pool to get embedded resources.
      Returns:
      true if used
    • setConcurrentDwn

      public void setConcurrentDwn(boolean concurrentDwn)
    • getConcurrentPool

      public String getConcurrentPool()
      Get the pool size for concurrent thread pool to get embedded resources.
      Returns:
      the pool size
    • setConcurrentPool

      public void setConcurrentPool(String poolSize)
    • applies

      public boolean applies(ConfigTestElement configElement)
      Description copied from class: AbstractSampler
      Does configElement apply to Sampler
      Specified by:
      applies in interface ConfigMergabilityIndicator
      Overrides:
      applies in class AbstractSampler
      Parameters:
      configElement - ConfigTestElement
      Returns:
      boolean
      See Also:
    • replace

      public int replace(String regex, String replaceBy, boolean caseSensitive) throws Exception
      Replace by replaceBy in path and body (arguments) properties
      Specified by:
      replace in interface Replaceable
      Parameters:
      regex - Regular expression to search for
      replaceBy - Text used as replacement
      caseSensitive - flag, whether search should be done case sensitive
      Returns:
      number of replacements
      Throws:
      Exception - when something fails while replacing