Search in sources :

Example 21 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest 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 22 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveVirtualHostHttps4443.

@Test
public void testResolveVirtualHostHttps4443() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest("https", "virtual.host.com", 4443, rootPath);
    Node virtualhost4443 = mapRoot.getNode("map/https").addNode("virtual.host.com.4443", "sling:Mapping");
    virtualhost4443.setProperty(PROP_REDIRECT_INTERNAL, "/content/virtual");
    try {
        saveMappings(session);
        final Resource res0 = resResolver.resolve(request, "/playground.html");
        assertNotNull(res0);
        assertEquals("/content/virtual/playground.html", res0.getPath());
        final Resource res1 = resResolver.resolve(request, "/playground/en.html");
        assertNotNull(res1);
        assertEquals("/content/virtual/playground/en.html", res1.getPath());
        final String mapped00 = resResolver.map(res0.getPath());
        assertEquals("https://virtual.host.com:4443/playground.html", mapped00);
        final String mapped01 = resResolver.map(request, res0.getPath());
        assertEquals("/playground.html", mapped01);
        final String mapped10 = resResolver.map(res1.getPath());
        assertEquals("https://virtual.host.com:4443/playground/en.html", mapped10);
        final String mapped11 = resResolver.map(request, res1.getPath());
        assertEquals("/playground/en.html", mapped11);
    } finally {
        virtualhost4443.remove();
        session.save();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 23 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveResourceAliasJcrContentWithUpdate.

@Test
public void testResolveResourceAliasJcrContentWithUpdate() throws Exception {
    // define an alias for the rootPath in the jcr:content child node
    String[] alias = { "testAlias", "testAliasToUpdate" };
    Node content = rootNode.addNode("jcr:content", "nt:unstructured");
    content.setProperty("sling:alias", alias);
    try {
        saveMappings(session);
        String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias[1] + ".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[1] + ".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)));
        //update alias  
        String[] aliasUpdated = { "testAlias", "testAliasUpdated" };
        content.setProperty("sling:alias", aliasUpdated);
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + aliasUpdated[1] + ".print.html");
        request = new FakeSlingHttpServletRequest(path);
        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) + "/" + aliasUpdated[1] + ".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)));
    } finally {
        content.remove();
        session.save();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 24 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveVirtualHostHttp80.

@Test
public void testResolveVirtualHostHttp80() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest(null, "virtual.host.com", -1, rootPath);
    Node virtualhost80 = mapRoot.getNode("map/http").addNode("virtual.host.com.80", "sling:Mapping");
    virtualhost80.setProperty(PROP_REDIRECT_INTERNAL, "/content/virtual");
    try {
        saveMappings(session);
        final Resource res0 = resResolver.resolve(request, "/playground.html");
        assertNotNull(res0);
        assertEquals("/content/virtual/playground.html", res0.getPath());
        final Resource res1 = resResolver.resolve(request, "/playground/en.html");
        assertNotNull(res1);
        assertEquals("/content/virtual/playground/en.html", res1.getPath());
        final String mapped00 = resResolver.map(res0.getPath());
        assertEquals("http://virtual.host.com/playground.html", mapped00);
        final String mapped01 = resResolver.map(request, res0.getPath());
        assertEquals("/playground.html", mapped01);
        final String mapped10 = resResolver.map(res1.getPath());
        assertEquals("http://virtual.host.com/playground/en.html", mapped10);
        final String mapped11 = resResolver.map(request, res1.getPath());
        assertEquals("/playground/en.html", mapped11);
    } finally {
        virtualhost80.remove();
        session.save();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) FakeSlingHttpServletRequest(org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest) Node(javax.jcr.Node) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 25 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest 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