This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch 5.9.x-javax in repository https://gitbox.apache.org/repos/asf/tapestry-5.git
commit 03c3807d694f91bd54f28ef38fc8d64f2575c5b4 Author: Ben Weidig <[email protected]> AuthorDate: Wed Oct 7 16:46:16 2026 +0200 Fix ContextAssetRequestHandlerTest after asset resolution reordering We need an actual root to resolve against now. --- .../assets/ContextAssetRequestHandlerTest.java | 104 ++++++++++++++++----- 1 file changed, 81 insertions(+), 23 deletions(-) diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/assets/ContextAssetRequestHandlerTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/assets/ContextAssetRequestHandlerTest.java index b80c078ca..34a3a41d4 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/assets/ContextAssetRequestHandlerTest.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/assets/ContextAssetRequestHandlerTest.java @@ -1,4 +1,4 @@ -// Copyright 2010, 2013 The Apache Software Foundation +// Copyright 2010, 2013, 2026 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,36 +14,94 @@ package org.apache.tapestry5.internal.services.assets; -import org.apache.tapestry5.test.ioc.TestBase; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.tapestry5.commons.Resource; +import org.apache.tapestry5.internal.services.ResourceStreamer; +import org.apache.tapestry5.ioc.internal.util.ClasspathResource; +import org.apache.tapestry5.services.ContextAssetProtectionRule; +import org.apache.tapestry5.services.assets.StreamableResource; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; +import java.util.Set; -public class ContextAssetRequestHandlerTest extends TestBase +class ContextAssetRequestHandlerTest { - @DataProvider - public Object[][] invalid_paths() + private static final ContextAssetProtectionRule NEVER_BLOCK = (path) -> false; + + private static final ContextAssetProtectionRule BLOCK_MAP_FILES = (path) -> path.toLowerCase().endsWith(".map"); + + private static final Resource CONTEXT_ROOT = new ClasspathResource(""); + + private static final ResourceStreamer ALWAYS_STREAM = new ResourceStreamer() { - return new Object[][] - { - {"web-Inf/classes/hibernate.cfg.xml"}, - {"Meta-Inf/MANIFEST.mf"}, - {"Index.tml"}, - {"folder/FolderIndex.TML"}, - {"\\WEB-INF/something.jpg"}, - {"\\//WEB-INF/something.jpg"}, - {"//WEB-INF/something.jpg"}, - {"//\\\\WEB-INF/something.jpg"} - }; + @Override + public boolean streamResource(Resource resource, String providedChecksum, Set<Options> options) + { + return true; + } + + @Override + public boolean streamResource(StreamableResource resource, String providedChecksum, Set<Options> options) + { + throw new UnsupportedOperationException(); + } + }; + + @ParameterizedTest + @ValueSource(strings = { + "web-Inf/classes/hibernate.cfg.xml", + "Meta-Inf/MANIFEST.mf", + "Index.tml", + "folder/FolderIndex.TML", + "\\WEB-INF/something.jpg", + "\\//WEB-INF/something.jpg", + "//WEB-INF/something.jpg", + "//\\\\WEB-INF/something.jpg" + }) + void ensureAssetsAreRejected(String path) throws IOException + { + // ARRANGE + ContextAssetRequestHandler handler = new ContextAssetRequestHandler(null, CONTEXT_ROOT, NEVER_BLOCK); + + // ACT + boolean providedResponse = handler.handleAssetRequest(null, null, "fake-checksum/" + path); + + // ASSERT + assertFalse(providedResponse, "Handler should return false for invalid path."); } - @Test(dataProvider = "invalid_paths") - public void ensure_assets_are_rejected(String path) throws IOException + @Test + void contextAssetProtectionRuleIsConsulted() throws IOException { - ContextAssetRequestHandler handler = new ContextAssetRequestHandler(null, null); + // ARRANGE + ContextAssetRequestHandler handler = new ContextAssetRequestHandler(null, CONTEXT_ROOT, BLOCK_MAP_FILES); + + // ACT + boolean providedResponse = handler.handleAssetRequest(null, null, "fake-checksum/app.js.map"); + + // ASSERT + assertFalse(providedResponse, "Handler should return false when the protection rule blocks the path."); + } + + @ParameterizedTest + @ValueSource(strings = { + "context-asset-request-handler-test/valid-asset.js", + "context-asset-request-handler-test/valid-asset.js.map" + }) + void validAssetsAreStreamedWhenNotBlocked(String path) throws IOException + { + // ARRANGE + ContextAssetRequestHandler handler = new ContextAssetRequestHandler(ALWAYS_STREAM, CONTEXT_ROOT, NEVER_BLOCK); + + // ACT + boolean providedResponse = handler.handleAssetRequest(null, null, "fake-checksum/" + path); - assertFalse(handler.handleAssetRequest(null, null, "fake-checksum/" + path), - "Handler should return false for invalid path."); + // ASSERT + assertTrue(providedResponse, "Handler should return true for a valid, existing asset that no rule blocks."); } }
