Back to Repositories

Testing Static Resource URL Pattern Validation in Stirling-PDF

This test suite validates the RequestUriUtils class functionality for identifying static resources in the Stirling-PDF application. It ensures proper classification of various URL patterns and resource types through comprehensive assertion testing.

Test Coverage Overview

The test suite provides thorough coverage of static resource identification logic with multiple URL patterns.

Key areas tested include:
  • Common static file extensions (.css, .js, .png, .html)
  • Various URL path patterns and structures
  • API endpoint validation
  • Authentication-related endpoints
  • Edge cases including root path and registration endpoints

Implementation Analysis

The testing approach utilizes JUnit Jupiter’s assertion framework to validate the isStaticResource method behavior. It implements a systematic pattern of true/false assertions to verify correct classification of different URL types, leveraging JUnit’s @Test annotation for test case organization.

The implementation demonstrates clean separation of test cases and clear boolean logic validation.

Technical Details

Testing Framework: JUnit Jupiter
Key Components:
  • Static assertion methods (assertTrue, assertFalse)
  • Individual test method organization
  • RequestUriUtils class integration
  • URL pattern validation logic

Best Practices Demonstrated

The test suite exemplifies several testing best practices including comprehensive case coverage, clear test method naming, and effective use of assertion methods. Notable practices include:
  • Systematic test case organization
  • Comprehensive edge case coverage
  • Clear and consistent assertion patterns
  • Focused test scope

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/utils/RequestUriUtilsTest.java

            
package stirling.software.SPDF.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RequestUriUtilsTest {

    @Test
    public void testIsStaticResource() {
        assertTrue(RequestUriUtils.isStaticResource("/css/styles.css"));
        assertTrue(RequestUriUtils.isStaticResource("/js/script.js"));
        assertTrue(RequestUriUtils.isStaticResource("/images/logo.png"));
        assertTrue(RequestUriUtils.isStaticResource("/public/index.html"));
        assertTrue(RequestUriUtils.isStaticResource("/pdfjs/pdf.worker.js"));
        assertTrue(RequestUriUtils.isStaticResource("/api/v1/info/status"));
        assertTrue(RequestUriUtils.isStaticResource("/some-path/icon.svg"));
        assertFalse(RequestUriUtils.isStaticResource("/api/v1/users"));
        assertFalse(RequestUriUtils.isStaticResource("/api/v1/orders"));
        assertFalse(RequestUriUtils.isStaticResource("/"));
        assertTrue(RequestUriUtils.isStaticResource("/login"));
        assertFalse(RequestUriUtils.isStaticResource("/register"));
        assertFalse(RequestUriUtils.isStaticResource("/api/v1/products"));
    }
}