Skip to content

Feature: Wildcards#8

Merged
bstien merged 3 commits intomainfrom
feature/wildcards
Sep 13, 2025
Merged

Feature: Wildcards#8
bstien merged 3 commits intomainfrom
feature/wildcards

Conversation

@bstien
Copy link
Owner

@bstien bstien commented Sep 13, 2025

Why?

We currently only supports exact string matching for host and path patterns, which limits flexibility when it comes to either reuse the same mock for dynamic URLs, having a versionated API and/or environment specific subdomains. Currently the user will have to register each of these variations individually, which causes a lot of overhead and unnecessary maintenance.

How?

This PR adds wildcard pattern matching using glob-style syntax for segments in either/or host or path. A segment for a host is the content between two ., a segment for a path is the content between two /.

  • Single segment wildcards (*): match within one segment only.
  • Multi-segment wildcards (**): match across multiple segments (zero or more).
  • Specificity ranking: exact matches beat wildcards, fewer wildcards beat more wildcards.
  • Compiled regexes are cached and reused/shared between all HTTPMock instances.

What?

  • Add HTTPMockMatcher.
  • Add HTTPMockMatcher.ExpressionStorage.
  • Replace old matching logic with 👆.
  • Add tests.

Show me

Single segment wildcards

HTTPMock.shared.addResponses(
    forPath: "/api/*/greeting",
    host: "*.example.com",
    responses: [.plaintext("Hello, world!")]
)

Matches:

  • api.example.com/api/v1/greeting
  • staging.example.com/api/v2/greeting

Doesn't match:

  • api.staging.example.com/api/v1/beta/greeting

Multi-segment wildcards

HTTPMock.shared.registerResponses {
    Host("**.example.com") {
        Path("/api/**/data") {
            MockResponse.plaintext("matched")
        }
    }
}

Matches:

  • api.example.com/api/data
  • api.staging.example.com/api/v1/beta/data

Automatic specificity ranking

let request = "api.example.com/api/users"

Given patterns:

  • api.example.com
  • *.example.com
  • **.example.com

Result: Chooses exact match api.example.com (exact match, aka. highest priority)

Complex patterns with multiple wildcards

HTTPMock.shared.addResponses(
    forPath: "/api/*-*/users",
    host: "api-*.example.com",
    responses: [.plaintext("Hello, world!")]
)

Matches:

  • api-staging.example.com/api/v1-test/users

@bstien bstien merged commit efeda79 into main Sep 13, 2025
1 check passed
@bstien bstien deleted the feature/wildcards branch September 13, 2025 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant