Search in sources :

Example 61 with FakeSlingHttpServletRequest

use of org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest in project sling by apache.

the class ResourceResolverTest method testResolveRemovedResourceAlias.

@Test
public void testResolveRemovedResourceAlias() throws Exception {
    // define an alias for the rootPath
    String alias = "testAlias";
    rootNode.setProperty("sling:alias", alias);
    saveMappings(session);
    String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
    HttpServletRequest request = new FakeSlingHttpServletRequest(path);
    Resource res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html/suffix.pdf");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    //remove alias property
    rootNode.getProperty("sling:alias").remove();
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + ".print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertTrue(res instanceof NonExistingResource);
    assertEquals("/" + alias + ".print.html", res.getPath());
    //create new child with alias
    String childNodeName = "rootChildAlias";
    Node childNode = maybeCreateNode(rootNode, childNodeName, "nt:unstructured");
    childNode.setProperty("sling:alias", "childAlias");
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias.print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath + "/" + childNodeName, res.getPath());
    assertEquals(childNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(childNode.isSame(res.adaptTo(Node.class)));
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias" + ".print.html/suffix.pdf");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertEquals(rootPath + "/" + childNodeName, res.getPath());
    assertEquals(childNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertEquals(".print.html/suffix.pdf", res.getResourceMetadata().getResolutionPathInfo());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(childNode.isSame(res.adaptTo(Node.class)));
    //remove the child node with the alias
    childNode.remove();
    saveMappings(session);
    path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + rootPath + "/childAlias" + ".print.html");
    request = new FakeSlingHttpServletRequest(path);
    res = resResolver.resolve(request, path);
    assertNotNull(res);
    assertTrue(res instanceof NonExistingResource);
    assertEquals(rootPath + "/childAlias.print.html", res.getPath());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 62 with FakeSlingHttpServletRequest

use of org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest in project sling by apache.

the class ResourceResolverTest method testGetRemovesExtensionInResolution.

@Test
public void testGetRemovesExtensionInResolution() throws Exception {
    final String path = rootPath + ".whatever";
    final Resource res = resResolver.resolve(new FakeSlingHttpServletRequest(path, "GET"), path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
}
Also used : FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 63 with FakeSlingHttpServletRequest

use of org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest in project sling by apache.

the class ResourceResolverTest method testMapURLEscaping.

@Test
public void testMapURLEscaping() throws Exception {
    final String mapHostInternal = "internal.host.com";
    final String mapRootInternal = "/content/internal";
    Node internalRedirect = mapRoot.getNode("map/http").addNode(mapHostInternal + ".80", "sling:Mapping");
    internalRedirect.setProperty(PROP_REDIRECT_INTERNAL, mapRootInternal);
    try {
        saveMappings(session);
        final String path = "/sample with spaces";
        final String escapedPath = "/sample%20with%20spaces";
        // ---------------------------------------------------------------------
        // internal redirect
        // a) test map(String)
        // => return full URL, escaped
        String mapped = resResolver.map(mapRootInternal + path);
        assertEquals("http://" + mapHostInternal + escapedPath, mapped);
        // b) test map(HttpServletRequest, String) with "localhost"
        // => return full URL, escaped
        mapped = resResolver.map(new FakeSlingHttpServletRequest(rootPath), mapRootInternal + path);
        assertEquals("http://" + mapHostInternal + escapedPath, mapped);
        // c) test map(HttpServletRequest, String) with "internal.host.com"
        // => only return path, escaped, because request host/port matches (cut
        // off host part)
        mapped = resResolver.map(new FakeSlingHttpServletRequest(null, mapHostInternal, -1, rootPath), mapRootInternal + path);
        assertEquals(escapedPath, mapped);
        // ---------------------------------------------------------------------
        // no mapping config
        // => return only escaped path
        final String unmappedRoot = "/unmappedRoot";
        // a) test map(String)
        mapped = resResolver.map(unmappedRoot + path);
        assertEquals(unmappedRoot + escapedPath, mapped);
        // b) test map(HttpServletRequest, String)
        mapped = resResolver.map(new FakeSlingHttpServletRequest(rootPath), unmappedRoot + path);
        assertEquals(unmappedRoot + escapedPath, mapped);
    } finally {
        internalRedirect.remove();
        session.save();
    }
}
Also used : FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) Node(javax.jcr.Node) Test(org.junit.Test)

Aggregations

FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)63 Test (org.junit.Test)61 Resource (org.apache.sling.api.resource.Resource)59 Node (javax.jcr.Node)57 HttpServletRequest (javax.servlet.http.HttpServletRequest)57 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)57 ValueMap (org.apache.sling.api.resource.ValueMap)2 Ignore (org.junit.Ignore)1