Class HttpTestBase

  • All Implemented Interfaces:
    junit.framework.Test
    Direct Known Subclasses:
    HttpTest

    public class HttpTestBase
    extends junit.framework.TestCase
    Base class for HTTP-based Sling Launchpad integration tests
    • Constructor Detail

      • HttpTestBase

        public HttpTestBase()
    • Method Detail

      • removeEndingSlash

        public static String removeEndingSlash​(String str)
      • setUp

        protected void setUp()
                      throws Exception
        Overrides:
        setUp in class junit.framework.TestCase
        Throws:
        Exception
      • getDefaultCredentials

        public org.apache.commons.httpclient.Credentials getDefaultCredentials()
        Generate default credentials used for HTTP requests.
      • tearDown

        protected void tearDown()
                         throws Exception
        Overrides:
        tearDown in class junit.framework.TestCase
        Throws:
        Exception
      • waitForSlingStartup

        protected void waitForSlingStartup()
                                    throws Exception
        On the server side, initialization of Sling bundles is done asynchronously once the webapp is started. This method checks that everything's ready on the server side, by calling an URL that requires the SlingPostServlet and the JCR repository to work correctly.
        Throws:
        Exception
      • slingServerReady

        protected boolean slingServerReady()
                                    throws Exception
        Return true if able to create and retrieve a node on server
        Throws:
        Exception
      • assertHttpStatus

        public org.apache.commons.httpclient.HttpMethod assertHttpStatus​(String urlString,
                                                                         int expectedStatusCode,
                                                                         String assertMessage)
                                                                  throws IOException
        Verify that given URL returns expectedStatusCode
        Returns:
        the HttpMethod executed
        Throws:
        IOException
      • assertHttpStatus

        public org.apache.commons.httpclient.HttpMethod assertHttpStatus​(String urlString,
                                                                         int expectedStatusCode)
                                                                  throws IOException
        Verify that given URL returns expectedStatusCode
        Returns:
        the HttpMethod executed
        Throws:
        IOException
      • assertPostStatus

        public org.apache.commons.httpclient.HttpMethod assertPostStatus​(String url,
                                                                         int expectedStatusCode,
                                                                         List<org.apache.commons.httpclient.NameValuePair> postParams,
                                                                         String assertMessage)
                                                                  throws IOException
        Execute a POST request and check status
        Returns:
        the HttpMethod executed
        Throws:
        IOException
      • getContent

        public String getContent​(String url,
                                 String expectedContentType)
                          throws IOException
        retrieve the contents of given URL and assert its content type (default to HTTP GET method)
        Throws:
        IOException
      • getContent

        public String getContent​(String url,
                                 String expectedContentType,
                                 List<org.apache.commons.httpclient.NameValuePair> params)
                          throws IOException
        retrieve the contents of given URL and assert its content type (default to HTTP GET method)
        Throws:
        IOException
      • getContent

        public String getContent​(String url,
                                 String expectedContentType,
                                 List<org.apache.commons.httpclient.NameValuePair> params,
                                 int expectedStatusCode)
                          throws IOException
        retrieve the contents of given URL and assert its content type (default to HTTP GET method)
        Parameters:
        expectedContentType - use CONTENT_TYPE_DONTCARE if must not be checked
        Throws:
        IOException
        org.apache.commons.httpclient.HttpException
      • getContent

        public String getContent​(String url,
                                 String expectedContentType,
                                 List<org.apache.commons.httpclient.NameValuePair> params,
                                 int expectedStatusCode,
                                 String httpMethod)
                          throws IOException
        retrieve the contents of given URL and assert its content type
        Parameters:
        expectedContentType - use CONTENT_TYPE_DONTCARE if must not be checked
        httpMethod - supports just GET and POST methods
        Throws:
        IOException
        org.apache.commons.httpclient.HttpException
      • uploadTestScript

        public String uploadTestScript​(String scriptPath,
                                       String localFilename,
                                       String filenameOnServer)
                                throws IOException
        upload rendering test script, and return its URL for future deletion
        Throws:
        IOException
      • executeScript

        public String executeScript​(String localFilename)
                             throws Exception
        Upload script, execute with no parameters and return content
        Throws:
        Exception
      • executeScript

        public String executeScript​(String localFilename,
                                    List<org.apache.commons.httpclient.NameValuePair> params)
                             throws Exception
        Upload script, execute with given parameters (optional) and return content
        Throws:
        Exception
      • getResponseBodyAsStream

        public static String getResponseBodyAsStream​(org.apache.commons.httpclient.HttpMethodBase m,
                                                     int maxLength)
                                              throws IOException
        Return m's response body as a string, optionally limiting the length that we read
        Parameters:
        maxLength - if 0, no limit
        Throws:
        IOException
      • setReadinessContentType

        public final void setReadinessContentType​(String extension,
                                                  String contentTypePrefix)
        Set the extension and content-type prefix to use for GET requests that check Sling readiness
      • isBundleVersionAtLeast

        public boolean isBundleVersionAtLeast​(String bundleSymbolicName,
                                              String version)
                                       throws IOException
        Returns true if the bundle version deployed on the Sling instance is present and at least of the specified version
        Parameters:
        bundleSymbolicName - the bundle name to check
        version - the minimal version of the bundle that should be deployed
        Returns:
        true if the bundle is deployed with the specified version, false if it is not deployed or the version it too old
        Throws:
        IOException