RequestMatcher

Utility object for matching HTTP request paths against configured endpoint paths.

This matcher supports path parameters using curly braces notation (e.g., /users/{userId}) and performs intelligent matching to determine if an incoming request matches a configured endpoint pattern. This is a core component of the network mock feature's request interception and is intentionally agnostic of any specific HTTP client implementation.

Path Parameter Syntax

Path parameters are defined using curly braces:

  • /api/users/{userId} matches /api/users/123, /api/users/abc, etc.

  • /api/posts/{postId}/comments/{commentId} matches any values for both IDs

  • /api/v1/products/{productId} matches /api/v1/products/prod-456

Matching Rules

  1. Paths must have the same number of segments (separated by /)

  2. Non-parameter segments must match exactly

  3. Parameter segments (in curly braces) match any value

  4. Matching is case-sensitive for non-parameter segments

  5. Empty segments are ignored (leading/trailing slashes handled automatically)

Usage

val configPath = "/api/v1/user/{userId}"
val requestPath = "/api/v1/user/123"

if (RequestMatcher.matchesPath(configPath, requestPath)) {
// Request matches the configured endpoint
// Proceed with mock response lookup
}

Examples

Simple Paths (No Parameters)

RequestMatcher.matchesPath("/api/users", "/api/users")        // true
RequestMatcher.matchesPath("/api/users", "/api/posts") // false
RequestMatcher.matchesPath("/api/users", "/api/users/123") // false (different segment count)

Single Parameter

RequestMatcher.matchesPath("/api/users/{id}", "/api/users/123")     // true
RequestMatcher.matchesPath("/api/users/{id}", "/api/users/abc") // true
RequestMatcher.matchesPath("/api/users/{id}", "/api/users/user-1") // true
RequestMatcher.matchesPath("/api/users/{id}", "/api/posts/123") // false

Multiple Parameters

RequestMatcher.matchesPath(
"/api/posts/{postId}/comments/{commentId}",
"/api/posts/123/comments/456"
) // true

RequestMatcher.matchesPath(
"/api/posts/{postId}/comments/{commentId}",
"/api/posts/abc/comments/xyz"
) // true

RequestMatcher.matchesPath(
"/api/posts/{postId}/comments/{commentId}",
"/api/posts/123/likes/456"
) // false (comments != likes)

Leading/Trailing Slashes

RequestMatcher.matchesPath("/api/users", "api/users")      // true (leading slash ignored)
RequestMatcher.matchesPath("/api/users/", "/api/users") // true (trailing slash ignored)
RequestMatcher.matchesPath("api/users", "/api/users/") // true (both ignored)

Future Enhancements

For post-MVP iterations, the following features could be added:

  • Parameter constraints (e.g., {userId:\d+} for numeric IDs only)

  • Wildcard matching

  • Regular expression support for complex patterns

  • Query parameter matching

See also

Functions

Link copied to clipboard
fun matchesPath(configPath: String, requestPath: String): Boolean

Checks if a request path matches a configured endpoint path pattern.