spring-testwiremock

What's the deal with url and urlPattern in WireMock?


I use WireMock to stub the following URL to an external service: /users/{username}/repos. The official docs says that using regexes is discouraged

note Using the Path and query regex is generally not advised. This exists primarily for compatibility with projects exported to/from WireMock.

I'm not sure if they talk about their cloud tool I don't even know if they have one, or they talk about the dependency/library that I'm using with Spring Testing.

However, using url works as a plain text matcher and works with neither regex nor path templates.

{
  "request": {
    "method": "GET",
    "url": "/users/{username}/repos"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "bodyFileName": "github-user-repos.json"
  }
}

causes an error

java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@6a1d6ef2 testClass = ...
Caused by: com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file ...\demo\wiremock\mappings\github-repo-users.json:
Illegal repetition near index 8
/users/{username}/repos
       ^

And when I change it to

"url": "/users/[^/]+/repos"

ends up in trouble as well

2025-08-16T12:08:36.783+02:00 ERROR 3816 --- [demo] [tp2010350054-56] WireMock.wiremock                        : 
                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
GET                                                        | GET
/users/[^/]+/repos                                         | /users/octocat/repos                                <<<<< URL does not match
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

I fixed it using

"urlPattern": "/users/[^/]+/repos"

Somebody else is even using urlPathPattern which I don't have any idea what is.

Explain me please the difference between url, urlPattern, urlPathPattern and why does the docs discourage regex usage if their path template doesn't work at all.


Solution

  • The documentation that you are linking to is the WireMock Cloud documentation. This is fine but as you are using WireMock open source, it might be better to refer to those docs first to avoid any confusion around the features that are in WireMock Cloud that are not is the OSS version.

    In terms of URL matching, the following applies:

    Path templates were introduced in WireMock 3.0.0 and supports matching on URL path templates conforming to the RFC 6570 standard. For example:

    {
      "request": {
        "urlPathTemplate": "/contacts/{contactId}/addresses/{addressId}"
        "method" : "GET"
      },
      "response" : {
        "status" : 200
      }
    }
    

    You can find the docs for all of this here - https://wiremock.org/docs/request-matching/

    In your case it looks like urlPathTemplate will do the trick:

    {
      "request": {
        "method": "GET",
        "urlPathTemplate": "/users/{username}/repos"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "bodyFileName": "github-user-repos.json"
      }
    }
    

    The nice thing about path templates is that you can reference the individual segments in your response templates - {{request.path.username}}