Search in sources :

Example 31 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveVirtualHostHttpVsHttps.

@Test
public void testResolveVirtualHostHttpVsHttps() throws Exception {
    final String host0 = "www.host.com";
    final String host1 = "secure.host.com";
    final String content = "/content/page";
    Node virtualhost80 = mapRoot.getNode("map/http").addNode(host0 + ".80", "sling:Mapping");
    virtualhost80.setProperty(PROP_REDIRECT_INTERNAL, content);
    Node virtualhost443 = mapRoot.getNode("map/https").addNode(host0 + ".443", "sling:Mapping");
    virtualhost443.setProperty(PROP_REDIRECT_INTERNAL, content);
    // HTTP request
    try {
        saveMappings(session);
        final HttpServletRequest requestHttp0 = new FakeSlingHttpServletRequest(null, host0, -1, rootPath);
        final Resource resHttp0 = resResolver.resolve(requestHttp0, "/playground.html");
        assertNotNull(resHttp0);
        assertEquals(content + "/playground.html", resHttp0.getPath());
        final Resource resHttp1 = resResolver.resolve(requestHttp0, "/playground/index.html");
        assertNotNull(resHttp1);
        assertEquals(content + "/playground/index.html", resHttp1.getPath());
        // HTTPS request
        final HttpServletRequest requestHttps0 = new FakeSlingHttpServletRequest("https", host0, -1, rootPath);
        final Resource resHttps0 = resResolver.resolve(requestHttps0, "/playground.html");
        assertNotNull(resHttps0);
        assertEquals(content + "/playground.html", resHttps0.getPath());
        final Resource resHttps1 = resResolver.resolve(requestHttps0, "/playground/index.html");
        assertNotNull(resHttps1);
        assertEquals(content + "/playground/index.html", resHttps1.getPath());
        // HTTP Mapping
        final String mappedHttp00 = resResolver.map(resHttp0.getPath());
        assertEquals("http://" + host0 + "/playground.html", mappedHttp00);
        final String mappedHttp01 = resResolver.map(requestHttp0, resHttp0.getPath());
        assertEquals("/playground.html", mappedHttp01);
        final String mappedHttp10 = resResolver.map(resHttp1.getPath());
        assertEquals("http://" + host0 + "/playground/index.html", mappedHttp10);
        final String mappedHttp11 = resResolver.map(requestHttp0, resHttp1.getPath());
        assertEquals("/playground/index.html", mappedHttp11);
        // HTTPS Mapping
        final HttpServletRequest requestHttp1 = new FakeSlingHttpServletRequest(null, host1, -1, rootPath);
        final HttpServletRequest requestHttps1 = new FakeSlingHttpServletRequest("https", host1, -1, rootPath);
        final String mappedHttps00 = resResolver.map(resHttps0.getPath());
        assertEquals("http://" + host0 + "/playground.html", mappedHttps00);
        final String mappedHttps01 = resResolver.map(requestHttps0, resHttps0.getPath());
        assertEquals("/playground.html", mappedHttps01);
        final String mappedHttps02 = resResolver.map(requestHttp1, resHttps0.getPath());
        assertEquals("http://" + host0 + "/playground.html", mappedHttps02);
        final String mappedHttps03 = resResolver.map(requestHttps1, resHttps0.getPath());
        assertEquals("https://" + host0 + "/playground.html", mappedHttps03);
        final String mappedHttps10 = resResolver.map(resHttps1.getPath());
        assertEquals("http://" + host0 + "/playground/index.html", mappedHttps10);
        final String mappedHttps11 = resResolver.map(requestHttps0, resHttps1.getPath());
        assertEquals("/playground/index.html", mappedHttps11);
        final String mappedHttps12 = resResolver.map(requestHttp1, resHttps1.getPath());
        assertEquals("http://" + host0 + "/playground/index.html", mappedHttps12);
        final String mappedHttps13 = resResolver.map(requestHttps1, resHttps1.getPath());
        assertEquals("https://" + host0 + "/playground/index.html", mappedHttps13);
    } finally {
        virtualhost80.remove();
        virtualhost443.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 32 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveVirtualHostHttp8080Root.

@Test
public void testResolveVirtualHostHttp8080Root() 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, "/");
    try {
        saveMappings(session);
        final Resource res0 = resResolver.resolve(request, "/playground.html");
        assertNotNull(res0);
        assertEquals("/playground.html", res0.getPath());
        final Resource res1 = resResolver.resolve(request, "/playground/en.html");
        assertNotNull(res1);
        assertEquals("/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 33 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveResourceExternalRedirect.

@Test
public void testResolveResourceExternalRedirect() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest("https", null, -1, rootPath);
    Node localhost443 = mapRoot.getNode("map/https/localhost.443");
    localhost443.setProperty(PROP_REDIRECT_EXTERNAL, "http://localhost");
    try {
        saveMappings(session);
        Resource res = resResolver.resolve(request, rootPath);
        assertNotNull(res);
        assertEquals(rootPath, res.getPath());
        assertEquals("sling:redirect", res.getResourceType());
        assertNotNull(res.adaptTo(ValueMap.class));
        assertEquals("http://localhost" + rootPath, res.adaptTo(ValueMap.class).get("sling:target", String.class));
    } finally {
        localhost443.getProperty(PROP_REDIRECT_EXTERNAL).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) ValueMap(org.apache.sling.api.resource.ValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 34 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveResourceInternalRedirectExact.

@Test
public void testResolveResourceInternalRedirectExact() throws Exception {
    HttpServletRequest request = new FakeSlingHttpServletRequest("https", null, -1, rootPath);
    Node localhost443 = mapRoot.getNode("map/https/localhost.443");
    Node toContent = localhost443.addNode("virtual", "sling:Mapping");
    toContent.setProperty("sling:match", "virtual$");
    toContent.setProperty(PROP_REDIRECT_INTERNAL, "/content/virtual.html");
    try {
        saveMappings(session);
        Resource res = resResolver.resolve(request, "/virtual");
        assertNotNull(res);
        assertEquals("/content/virtual.html", res.getPath());
        res = resResolver.resolve(request, "/virtual.html");
        assertNotNull(res);
        assertEquals("/virtual.html", res.getPath());
        res = resResolver.resolve(request, "/virtual/child.html");
        assertNotNull(res);
        assertEquals("/virtual/child.html", res.getPath());
        String url = resResolver.map(null, "/content/virtual.html");
        assertNotNull(url);
        assertEquals("https://localhost/virtual", url);
        url = resResolver.map(request, "/content/virtual.html");
        assertNotNull(url);
        assertEquals("/virtual", url);
    } finally {
        toContent.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 35 with FakeSlingHttpServletRequest

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

the class ResourceResolverWithVanityBloomFilterTest method testResolveResourceAliasWithUpdate.

@Test
public void testResolveResourceAliasWithUpdate() throws Exception {
    // define an alias for the rootPath
    String[] alias = { "testAlias", "testAliasToUpdate" };
    rootNode.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" };
        rootNode.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 {
        rootNode.getProperty("sling:alias").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