Search in sources :

Example 11 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveResourceAliasJcrContent.

@Test
public void testResolveResourceAliasJcrContent() 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());
        Node child = rootNode.addNode("child", "nt:unstructured");
        child.setProperty("sling:alias", alias);
        try {
            saveMappings(session);
            res = resResolver.resolve(request, path);
            assertEquals(child.getPath(), res.getPath());
        } finally {
            child.remove();
            session.save();
        }
    } 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 12 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveRemovedMixinVanityPath.

@Test
public void testResolveRemovedMixinVanityPath() throws Exception {
    Node childNode = null;
    try {
        // create child now without vanity path first
        childNode = maybeCreateNode(rootNode, "rootChild", "nt:unstructured");
        session.save();
        final String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/childVanity.print.html");
        HttpServletRequest request = new FakeSlingHttpServletRequest(path);
        Resource res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertTrue(ResourceUtil.isNonExistingResource(res));
        // now add vanity path without mixin
        childNode.setProperty("sling:vanityPath", "childVanity");
        saveMappings(session);
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode.getPath(), res.getPath());
        //add mixin
        childNode.addMixin("sling:VanityPath");
        saveMappings(session);
        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)));
        //remove mixin
        childNode.removeMixin("sling:VanityPath");
        saveMappings(session);
        session.refresh(false);
        assertTrue(childNode.hasProperty("sling:vanityPath"));
        assertEquals("childVanity", childNode.getProperty("sling:vanityPath").getString());
        request = new FakeSlingHttpServletRequest(path);
        res = resResolver.resolve(request, path);
        assertNotNull(res);
        assertEquals(childNode.getPath(), res.getPath());
    } finally {
        if (childNode != null) {
            childNode.remove();
            saveMappings(session);
        }
    }
}
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 13 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveVirtualHostHttp80Multiple.

@Test
public void testResolveVirtualHostHttp80Multiple() throws Exception {
    final String de = "de";
    final String en = "en";
    final String hostDE = de + ".host.com";
    final String hostEN = en + ".host.com";
    final String contentDE = "/content/" + de;
    final String contentEN = "/content/" + en;
    Node virtualhost80a = mapRoot.getNode("map/http").addNode(hostDE + ".80", "sling:Mapping");
    virtualhost80a.setProperty(PROP_REDIRECT_INTERNAL, contentDE);
    Node virtualhost80 = mapRoot.getNode("map/http").addNode(hostEN + ".80", "sling:Mapping");
    virtualhost80.setProperty(PROP_REDIRECT_INTERNAL, contentEN);
    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(contentDE + "/playground.html", resDE0.getPath());
        final Resource resDE1 = resResolver.resolve(requestDE, "/playground/index.html");
        assertNotNull(resDE1);
        assertEquals(contentDE + "/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(contentEN + "/playground.html", resEN0.getPath());
        final Resource resEN1 = resResolver.resolve(requestEN, "/playground/index.html");
        assertNotNull(resEN1);
        assertEquals(contentEN + "/playground/index.html", resEN1.getPath());
        final String mappedEN00 = resResolver.map(resEN0.getPath());
        assertEquals("http://" + hostEN + "/playground.html", mappedEN00);
        final String mappedEN01 = resResolver.map(requestEN, resEN0.getPath());
        assertEquals("/playground.html", mappedEN01);
        final String mappedEN10 = resResolver.map(resEN1.getPath());
        assertEquals("http://" + hostEN + "/playground/index.html", mappedEN10);
        final String mappedEN11 = resResolver.map(requestEN, resEN1.getPath());
        assertEquals("/playground/index.html", mappedEN11);
    } 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 14 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveVirtualHostHttp8080.

@Test
public void testResolveVirtualHostHttp8080() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest(null, "virtual.host.com", 8080, rootPath);
    Node virtualhost80 = mapRoot.getNode("map/http").addNode("virtual.host.com.8080", "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:8080/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:8080/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 15 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testMapNamespaceMangling.

@Test
public void testMapNamespaceMangling() throws Exception {
    final String mapHost = "virtual.host.com";
    final String mapRootPath = "/content/virtual";
    final String contextPath = "/context";
    Node virtualhost80 = mapRoot.getNode("map/http").addNode(mapHost + ".80", "sling:Mapping");
    virtualhost80.setProperty(PROP_REDIRECT_INTERNAL, mapRootPath);
    try {
        saveMappings(session);
        // ---------------------------------------------------------------------
        // tests expecting paths without context
        final HttpServletRequest virtualRequest = new FakeSlingHttpServletRequest(null, mapHost, -1, rootPath);
        // simple mapping - cut off prefix and add host
        final String pathv0 = "/sample";
        final String mappedv0 = resResolver.map(virtualRequest, mapRootPath + pathv0);
        assertEquals("Expect unmangled path", pathv0, mappedv0);
        // expected name mangling without host prefix
        final String pathv1 = "/sample/jcr:content";
        final String mangledv1 = "/sample/_jcr_content";
        final String mappedv1 = resResolver.map(virtualRequest, mapRootPath + pathv1);
        assertEquals("Expect mangled path", mangledv1, mappedv1);
        // ---------------------------------------------------------------------
        // tests expecting paths with context "/context"
        ((FakeSlingHttpServletRequest) virtualRequest).setContextPath(contextPath);
        // simple mapping - cut off prefix and add host
        final String pathvc0 = "/sample";
        final String mappedvc0 = resResolver.map(virtualRequest, mapRootPath + pathvc0);
        assertEquals("Expect unmangled path", contextPath + pathv0, mappedvc0);
        // expected name mangling without host prefix
        final String pathvc1 = "/sample/jcr:content";
        final String mangledvc1 = "/sample/_jcr_content";
        final String mappedvc1 = resResolver.map(virtualRequest, mapRootPath + pathvc1);
        assertEquals("Expect mangled path", contextPath + mangledvc1, mappedvc1);
        // ---------------------------------------------------------------------
        // tests expecting absolute URLs without context
        final HttpServletRequest foreignRequest = new FakeSlingHttpServletRequest(null, "foreign.host.com", -1, rootPath);
        final String pathf0 = "/sample";
        final String mappedf0 = resResolver.map(foreignRequest, mapRootPath + pathf0);
        assertEquals("Expect unmangled absolute URI", "http://" + mapHost + pathf0, mappedf0);
        final String pathf1 = "/sample/jcr:content";
        final String mangledf1 = "/sample/_jcr_content";
        final String mappedf1 = resResolver.map(foreignRequest, mapRootPath + pathf1);
        assertEquals("Expect mangled absolute URI", "http://" + mapHost + mangledf1, mappedf1);
        // ---------------------------------------------------------------------
        // tests expecting absolute URLs with context "/context"
        ((FakeSlingHttpServletRequest) foreignRequest).setContextPath(contextPath);
        final String pathfc0 = "/sample";
        final String mappedfc0 = resResolver.map(foreignRequest, mapRootPath + pathfc0);
        assertEquals("Expect unmangled absolute URI", "http://" + mapHost + contextPath + pathfc0, mappedfc0);
        final String pathfc1 = "/sample/jcr:content";
        final String mangledfc1 = "/sample/_jcr_content";
        final String mappedfc1 = resResolver.map(foreignRequest, mapRootPath + pathfc1);
        assertEquals("Expect mangled absolute URI", "http://" + mapHost + contextPath + mangledfc1, mappedfc1);
    } 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) 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