Search in sources :

Example 1 with ExpiresConfiguration

use of org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration in project tomcat by apache.

the class TestExpiresFilter method testParseExpiresConfigurationMonoDuration.

@Test
public void testParseExpiresConfigurationMonoDuration() {
    ExpiresFilter expiresFilter = new ExpiresFilter();
    ExpiresConfiguration actualConfiguration = expiresFilter.parseExpiresConfiguration("access plus 2 hours");
    Assert.assertEquals(StartingPoint.ACCESS_TIME, actualConfiguration.getStartingPoint());
    Assert.assertEquals(1, actualConfiguration.getDurations().size());
    Assert.assertEquals(2, actualConfiguration.getDurations().get(0).getAmount());
    Assert.assertEquals(DurationUnit.HOUR, actualConfiguration.getDurations().get(0).getUnit());
}
Also used : ExpiresConfiguration(org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with ExpiresConfiguration

use of org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration in project tomcat by apache.

the class TestExpiresFilter method testParseExpiresConfigurationCombinedDuration.

@Test
public void testParseExpiresConfigurationCombinedDuration() {
    ExpiresFilter expiresFilter = new ExpiresFilter();
    ExpiresConfiguration actualConfiguration = expiresFilter.parseExpiresConfiguration("access plus 1 month 15 days 2 hours");
    Assert.assertEquals(StartingPoint.ACCESS_TIME, actualConfiguration.getStartingPoint());
    Assert.assertEquals(3, actualConfiguration.getDurations().size());
}
Also used : ExpiresConfiguration(org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 3 with ExpiresConfiguration

use of org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration in project tomcat by apache.

the class TestExpiresFilter method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresDefault", "access plus 1 month");
    filterDef.addInitParameter("ExpiresByType text/html", "access plus 1 month 15 days 2 hours");
    filterDef.addInitParameter("ExpiresByType image/gif", "modification plus 5 hours 3 minutes");
    filterDef.addInitParameter("ExpiresByType image/jpg", "A10000");
    filterDef.addInitParameter("ExpiresByType video/mpeg", "M20000");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "304, 503");
    ExpiresFilter expiresFilter = new ExpiresFilter();
    filterDef.setFilter(expiresFilter);
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    tomcat.start();
    try {
        // VERIFY EXCLUDED RESPONSE STATUS CODES
        int[] excludedResponseStatusCodes = expiresFilter.getExcludedResponseStatusCodesAsInts();
        Assert.assertEquals(2, excludedResponseStatusCodes.length);
        Assert.assertEquals(304, excludedResponseStatusCodes[0]);
        Assert.assertEquals(503, excludedResponseStatusCodes[1]);
        // VERIFY DEFAULT CONFIGURATION
        ExpiresConfiguration expiresConfigurationDefault = expiresFilter.getDefaultExpiresConfiguration();
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationDefault.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().size());
        Assert.assertEquals(DurationUnit.MONTH, expiresConfigurationDefault.getDurations().get(0).getUnit());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().get(0).getAmount());
        // VERIFY TEXT/HTML
        ExpiresConfiguration expiresConfigurationTextHtml = expiresFilter.getExpiresConfigurationByContentType().get("text/html");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationTextHtml.getStartingPoint());
        Assert.assertEquals(3, expiresConfigurationTextHtml.getDurations().size());
        Duration oneMonth = expiresConfigurationTextHtml.getDurations().get(0);
        Assert.assertEquals(DurationUnit.MONTH, oneMonth.getUnit());
        Assert.assertEquals(1, oneMonth.getAmount());
        Duration fifteenDays = expiresConfigurationTextHtml.getDurations().get(1);
        Assert.assertEquals(DurationUnit.DAY, fifteenDays.getUnit());
        Assert.assertEquals(15, fifteenDays.getAmount());
        Duration twoHours = expiresConfigurationTextHtml.getDurations().get(2);
        Assert.assertEquals(DurationUnit.HOUR, twoHours.getUnit());
        Assert.assertEquals(2, twoHours.getAmount());
        // VERIFY IMAGE/GIF
        ExpiresConfiguration expiresConfigurationImageGif = expiresFilter.getExpiresConfigurationByContentType().get("image/gif");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfigurationImageGif.getStartingPoint());
        Assert.assertEquals(2, expiresConfigurationImageGif.getDurations().size());
        Duration fiveHours = expiresConfigurationImageGif.getDurations().get(0);
        Assert.assertEquals(DurationUnit.HOUR, fiveHours.getUnit());
        Assert.assertEquals(5, fiveHours.getAmount());
        Duration threeMinutes = expiresConfigurationImageGif.getDurations().get(1);
        Assert.assertEquals(DurationUnit.MINUTE, threeMinutes.getUnit());
        Assert.assertEquals(3, threeMinutes.getAmount());
        // VERIFY IMAGE/JPG
        ExpiresConfiguration expiresConfigurationImageJpg = expiresFilter.getExpiresConfigurationByContentType().get("image/jpg");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationImageJpg.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationImageJpg.getDurations().size());
        Duration tenThousandSeconds = expiresConfigurationImageJpg.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, tenThousandSeconds.getUnit());
        Assert.assertEquals(10000, tenThousandSeconds.getAmount());
        // VERIFY VIDEO/MPEG
        ExpiresConfiguration expiresConfiguration = expiresFilter.getExpiresConfigurationByContentType().get("video/mpeg");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfiguration.getStartingPoint());
        Assert.assertEquals(1, expiresConfiguration.getDurations().size());
        Duration twentyThousandSeconds = expiresConfiguration.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, twentyThousandSeconds.getUnit());
        Assert.assertEquals(20000, twentyThousandSeconds.getAmount());
    } finally {
        tomcat.stop();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ExpiresConfiguration(org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration) Duration(org.apache.catalina.filters.ExpiresFilter.Duration) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ExpiresConfiguration (org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration)3 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 Test (org.junit.Test)3 Context (org.apache.catalina.Context)1 Duration (org.apache.catalina.filters.ExpiresFilter.Duration)1 Tomcat (org.apache.catalina.startup.Tomcat)1 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1