Search in sources :

Example 1 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveVirtualHostHttp80MultipleRoot.

@Test
public void testResolveVirtualHostHttp80MultipleRoot() throws Exception {
    final String de = "de";
    final String en = "en";
    final String fr = "fr";
    final String hostDE = de + ".host.com";
    final String hostEN = en + ".host.com";
    final String hostFR = fr + ".host.com";
    Node virtualhost80a = mapRoot.getNode("map/http").addNode(hostDE + ".80", "sling:Mapping");
    virtualhost80a.setProperty(PROP_REDIRECT_INTERNAL, "/");
    Node virtualhost80 = mapRoot.getNode("map/http").addNode(hostEN + ".80", "sling:Mapping");
    virtualhost80.setProperty(PROP_REDIRECT_INTERNAL, "/");
    try {
        saveMappings(session);
        // de content mapping
        final HttpServletRequest requestDE = new FakeSlingHttpServletRequest(null, hostDE, -1, rootPath);
        final Resource resDE0 = resResolver.resolve(requestDE, "/playground.html");
        assertNotNull(resDE0);
        assertEquals("/playground.html", resDE0.getPath());
        final Resource resDE1 = resResolver.resolve(requestDE, "/playground/index.html");
        assertNotNull(resDE1);
        assertEquals("/playground/index.html", resDE1.getPath());
        final String mappedDE00 = resResolver.map(resDE0.getPath());
        assertEquals("http://" + hostDE + "/playground.html", mappedDE00);
        final String mappedDE01 = resResolver.map(requestDE, resDE0.getPath());
        assertEquals("/playground.html", mappedDE01);
        final String mappedDE10 = resResolver.map(resDE1.getPath());
        assertEquals("http://" + hostDE + "/playground/index.html", mappedDE10);
        final String mappedDE11 = resResolver.map(requestDE, resDE1.getPath());
        assertEquals("/playground/index.html", mappedDE11);
        // en content mapping
        final HttpServletRequest requestEN = new FakeSlingHttpServletRequest(null, hostEN, -1, rootPath);
        final Resource resEN0 = resResolver.resolve(requestEN, "/playground.html");
        assertNotNull(resEN0);
        assertEquals("/playground.html", resEN0.getPath());
        final Resource resEN1 = resResolver.resolve(requestEN, "/playground/index.html");
        assertNotNull(resEN1);
        assertEquals("/playground/index.html", resEN1.getPath());
        // here we get back the hostDE, since this is the first configured
        // and we have no request information to map the correct of the
        // duplicate entries !
        final String mappedEN00 = resResolver.map(resEN0.getPath());
        assertEquals("http://" + hostDE + "/playground.html", mappedEN00);
        // here we expect the path without scheme/host/port since we have
        // the request and can select the right mapping
        final String mappedEN01 = resResolver.map(requestEN, resEN0.getPath());
        assertEquals("/playground.html", mappedEN01);
        // here we get back the hostDE, since this is the first configured
        // and we have no request information to map the correct of the
        // duplicate entries !
        final String mappedEN10 = resResolver.map(resEN1.getPath());
        assertEquals("http://" + hostDE + "/playground/index.html", mappedEN10);
        // here we expect the path without scheme/host/port since we have
        // the request and can select the right mapping
        final String mappedEN11 = resResolver.map(requestEN, resEN1.getPath());
        assertEquals("/playground/index.html", mappedEN11);
        final HttpServletRequest requestFR = new FakeSlingHttpServletRequest(null, hostFR, -1, rootPath);
        final Resource resFR1 = resResolver.resolve(requestFR, "/playground/index.html");
        assertNotNull(resFR1);
        assertEquals("/playground/index.html", resFR1.getPath());
        // here we get back the hostDE, since this is the first configured
        // and we have no request information to map the correct of the
        // duplicate entries !
        final String mappedFR10 = resResolver.map(resFR1.getPath());
        assertEquals("http://" + hostDE + "/playground/index.html", mappedFR10);
        // here we get back the hostDE, since this is the first configured
        // and we have request information which does not map any of the
        // configured duplicate entries !
        final String mappedFR11 = resResolver.map(requestFR, resFR1.getPath());
        assertEquals("http://" + hostDE + "/playground/index.html", mappedFR11);
    } finally {
        virtualhost80a.remove();
        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 2 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveResourceInternalRedirectPathAndVanityPath.

@Test
public void testResolveResourceInternalRedirectPathAndVanityPath() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest("https", null, -1, rootPath);
    Node localhost443 = mapRoot.getNode("map/https/localhost.443");
    localhost443.setProperty(PROP_REDIRECT_INTERNAL, "/example");
    try {
        saveMappings(session);
        Resource res = resResolver.resolve(request, vanity[0]);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        //see SLING-3428
        res = resResolver.resolve(request, vanity[1]);
        assertNotNull(res);
        assertEquals("/example/" + vanity[1], res.getPath());
    } finally {
        localhost443.getProperty(PROP_REDIRECT_INTERNAL).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 3 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest 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 4 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveRemovedesourceAliasJcrContent.

@Test
public void testResolveRemovedesourceAliasJcrContent() throws Exception {
    // define an alias for the rootPath in the jcr:content child node
    String alias = "testAlias";
    Node content = rootNode.addNode("jcr:content", "nt:unstructured");
    content.setProperty("sling:alias", alias);
    try {
        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)));
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + alias + "/" + alias + ".print.html");
        res = resResolver.resolve(request, path);
        assertEquals("GET request resolution does not go up the path", Resource.RESOURCE_TYPE_NON_EXISTING, res.getResourceType());
        //remove alias property
        content.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());
    } 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) 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 5 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveVanityPathWithVanityOrder.

@Test
public void testResolveVanityPathWithVanityOrder() throws Exception {
    Node childNode = null;
    Node childNode2 = null;
    try {
        //create new child with vanity path
        childNode = maybeCreateNode(rootNode, "rootChild", "nt:unstructured");
        childNode.setProperty("sling:vanityPath", "childVanity");
        childNode.addMixin("sling:VanityPath");
        saveMappings(session);
        String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        HttpServletRequest request = new FakeSlingHttpServletRequest(path);
        Resource res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode.getPath(), 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)));
        //create another child with the same vanity path
        childNode2 = maybeCreateNode(rootNode, "rootChild2", "nt:unstructured");
        childNode2.setProperty("sling:vanityPath", "childVanity");
        childNode2.addMixin("sling:VanityPath");
        saveMappings(session);
        assertNotNull(res);
        assertEquals(childNode.getPath(), 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)));
        //add vanityOrder to childNode2
        childNode2.setProperty("sling:vanityOrder", 100);
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode2.getPath(), res.getPath());
        assertEquals(childNode2.getPrimaryNodeType().getName(), res.getResourceType());
        assertEquals(".print.html", res.getResourceMetadata().getResolutionPathInfo());
        assertNotNull(res.adaptTo(Node.class));
        assertTrue(childNode2.isSame(res.adaptTo(Node.class)));
        //add vanityOrder to childNode higher than childNode2
        childNode.setProperty("sling:vanityOrder", 1000);
        saveMappings(session);
        path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode.getPath(), 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)));
    } finally {
        if (childNode != null) {
            childNode.remove();
        }
        if (childNode2 != null) {
            childNode2.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)

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