Search in sources :

Example 16 with FakeSlingHttpServletRequest

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

the class ResourceResolverTest method testResolveResource.

@Test
public void testResolveResource() throws Exception {
    // existing resource
    HttpServletRequest request = new FakeSlingHttpServletRequest(rootPath);
    Resource res = resResolver.resolve(request, rootPath);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    // missing resource below root should resolve "missing resource"
    String path = rootPath + "/missing";
    res = resResolver.resolve(new FakeSlingHttpServletRequest(path), path);
    assertNotNull(res);
    assertEquals(path, res.getPath());
    assertEquals(Resource.RESOURCE_TYPE_NON_EXISTING, res.getResourceType());
    assertNull(res.adaptTo(Node.class));
    // root with selectors/ext should resolve root
    path = rootPath + ".print.a4.html";
    res = resResolver.resolve(new FakeSlingHttpServletRequest(path), path);
    assertNotNull(res);
    assertEquals(rootPath, res.getPath());
    assertEquals(rootNode.getPrimaryNodeType().getName(), res.getResourceType());
    assertNotNull(res.adaptTo(Node.class));
    assertTrue(rootNode.isSame(res.adaptTo(Node.class)));
    // missing resource should return NON_EXISTING Resource
    path = rootPath + System.currentTimeMillis();
    res = resResolver.resolve(new FakeSlingHttpServletRequest(path), path);
    assertNotNull(res);
    assertTrue(ResourceUtil.isNonExistingResource(res));
    assertEquals(path, res.getPath());
    assertEquals(Resource.RESOURCE_TYPE_NON_EXISTING, res.getResourceType());
}
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 17 with FakeSlingHttpServletRequest

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

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

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

the class ResourceResolverTest method testResolveVanityPath.

@Test
public void testResolveVanityPath() throws Exception {
    String path = ResourceUtil.normalize(ResourceUtil.getParent(rootPath) + "/" + vanity[0] + ".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) + "/" + vanity[0] + ".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) + "/" + vanity[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) + "/" + vanity[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)));
}
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 19 with FakeSlingHttpServletRequest

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

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

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

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

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