Search in sources :

Example 1 with MockSlingHttpServletRequest

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest in project sling by apache.

the class HelperTestBase method makeRequest.

protected MockSlingHttpServletRequest makeRequest(String method, String selectors, String extension) {
    final MockSlingHttpServletRequest result = new MockSlingHttpServletRequest(resourcePath, selectors, extension, null, null);
    result.setMethod(method);
    result.setResourceResolver(resourceResolver);
    result.setResource(resource);
    return result;
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest)

Example 2 with MockSlingHttpServletRequest

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest in project sling by apache.

the class ScriptSelectionTest method assertScript.

/** Given a list of available scripts and the request method, selectors
     *  and extension, check that the expected script is selected.
     *  The resource type is foo:bar, set by HelperTestBase
     *
     *  @param method the HTTP method of the simulated request
     *  @param selectors the selectors of the simulated request
     *  @param extension the extension of the simulated request
     *  @param scripts the list of scripts that would be available in the repository
     *  @param expectedScript the script that we expect to be selected
     */
protected void assertScript(String method, String selectors, String extension, String[] scripts, String expectedScript) {
    // Add given scripts to our mock resource resolver
    for (String script : scripts) {
        final MockResource r = new MockResource(resourceResolver, script, "nt:file");
        resourceResolver.addResource(r);
    }
    // Create mock request and get scripts from ResourceCollector
    final MockSlingHttpServletRequest req = makeRequest(method, selectors, extension);
    final ResourceCollector u = ResourceCollector.create(req, null, new String[] { "html" });
    final Collection<Resource> s = u.getServlets(req.getResourceResolver());
    if (expectedScript == null) {
        assertFalse("No script must be found", s.iterator().hasNext());
    } else {
        // Verify that the expected script is the first in the list of candidates
        assertTrue("A script must be found", s.iterator().hasNext());
        final String scriptPath = s.iterator().next().getPath();
        assertEquals("First script is the expected one", expectedScript, scriptPath);
    }
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockResource(org.apache.sling.commons.testing.sling.MockResource) Resource(org.apache.sling.api.resource.Resource) MockResource(org.apache.sling.commons.testing.sling.MockResource)

Example 3 with MockSlingHttpServletRequest

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest in project sling by apache.

the class RedirectServletTest method toRedirect.

private static String toRedirect(String basePath, String selectors, String extension, String suffix, String queryString, String targetPath) {
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(basePath, selectors, extension, suffix, queryString, basePath, TEST_SCHEME, TEST_HOST, TEST_PORT, "");
    request.setResourceResolver(new MockResourceResolver());
    return RedirectServlet.toRedirectPath(targetPath, request);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) MockResourceResolver(org.apache.sling.commons.testing.sling.MockResourceResolver)

Example 4 with MockSlingHttpServletRequest

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest in project sling by apache.

the class SlingServletResolverTest method testAcceptsRequest.

@Test
public void testAcceptsRequest() {
    MockSlingHttpServletRequest secureRequest = new MockSlingHttpServletRequest(SERVLET_PATH, null, SERVLET_EXTENSION, null, null);
    secureRequest.setResourceResolver(mockResourceResolver);
    secureRequest.setSecure(true);
    Servlet result = servletResolver.resolveServlet(secureRequest);
    assertEquals("Did not resolve to correct servlet", servlet, result);
}
Also used : MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) Test(org.junit.Test)

Example 5 with MockSlingHttpServletRequest

use of org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest in project sling by apache.

the class SlingPostServletTest method testGetJsonResponse.

public void testGetJsonResponse() {
    MockSlingHttpServletRequest req = new MockSlingHttpServlet3Request(null, null, null, null, null) {

        @Override
        public String getHeader(String name) {
            return name.equals(MediaRangeList.HEADER_ACCEPT) ? "application/json" : super.getHeader(name);
        }

        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            return null;
        }
    };
    PostResponse result = servlet.createPostResponse(req);
    assertTrue(result instanceof JSONResponse);
}
Also used : MockSlingHttpServlet3Request(org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request) MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) JSONResponse(org.apache.sling.servlets.post.JSONResponse) PostResponse(org.apache.sling.servlets.post.PostResponse)

Aggregations

MockSlingHttpServletRequest (org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest)20 Test (org.junit.Test)10 MockSlingHttpServletResponse (org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse)6 Servlet (javax.servlet.Servlet)2 HttpServlet (javax.servlet.http.HttpServlet)2 OptingServlet (org.apache.sling.api.servlets.OptingServlet)2 MockResource (org.apache.sling.commons.testing.sling.MockResource)2 MockResourceResolver (org.apache.sling.commons.testing.sling.MockResourceResolver)2 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)1 HashMap (java.util.HashMap)1 Resource (org.apache.sling.api.resource.Resource)1 JSONArray (org.apache.sling.commons.json.JSONArray)1 JsonRendererServlet (org.apache.sling.servlets.get.impl.helpers.JsonRendererServlet)1 JSONResponse (org.apache.sling.servlets.post.JSONResponse)1 PostResponse (org.apache.sling.servlets.post.PostResponse)1 MockSlingHttpServlet3Request (org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request)1 Before (org.junit.Before)1