Hello there! I’m seeking for help building my stu...
# help
j
Hello there! I’m seeking for help building my stubs. Currently I have the following working:
Copy code
{
      "request": {
        "method": "GET",
        "urlPath": "/service/api/v1/user/username/user%40my-company.com/"
      },
      "response": {
        "status": 200,
        "bodyFileName": "abc_getUserByUsername_user%40my-company.com.json",
        "headers": {
          "Content-Type": "application/json"
        }
      }
    },
    {
      "request": {
        "method": "GET",
        "urlPath": "/service/api/v1/user/username/user-high-clv%40my-company.com/"
      },
      "response": {
        "status": 200,
        "bodyFileName": "abc_getUserByUsername_user-high-clv%40my-company.com.json",
        "headers": {
          "Content-Type": "application/json"
        }
      }
    },
    {
      "request": {
        "method": "GET",
        "urlPath": "/service/api/v1/user/username/user-non-high-clv%40my-company.com/"
      },
      "response": {
        "status": 200,
        "bodyFileName": "abc_getUserByUsername_user-non-high-clv%40my-company.com.json",
        "headers": {
          "Content-Type": "application/json"
        }
      }
    }
I’m trying to simplify it using
urlPathPattern
, I managed to do it in other mocks but not here:
Copy code
{
  "request": {
    "method": "GET",
    "urlPathPattern": "/service/api/v1/user/username/()/"
  },
  "response": {
    "status": 200,
    "bodyFileName": "abc_getUserByUsername_{{request.pathSegments.[5]}}.json",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
The part with
()
is where I’m trying to add the regex, but I tried many variations without success
r
a nicer solution than regex may be to use
urlPathTemplate
like so,
Copy code
{
  "request": {
    "method": "GET",
    "urlPathTemplate": "/service/api/v1/user/username/{username}"
  },
  "response": {
    "status": 200,
    "bodyFileName": "abc_getUserByUsername_{{request.path.username}}.json",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}
j
I’m using the
spring-cloud-starter-contract-stub-runner
and that doesn’t seems to work
Copy code
Caused by: com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /.../src/test/resources/mappings/..._stubs.json:
Unrecognized field "urlPathTemplate" (class com.github.tomakehurst.wiremock.matching.RequestPattern), not marked as ignorable
That dependency is adding
wiremock-jre8-standalone-2.35.0
r
ah okay. then
"urlPathPattern": "/service/api/v1/user/username/(.*)/"
doesnt work?
j
I’m afraid it doesn’t 😞
r
"urlPathPattern": "/service/api/v1/user/username/.*/"
?
j
That doesn’t work either. I tried so many regex that I lost the count
r
are you seeing an error?
j
I don’t see an error. But the tests fail because that stub is not matching the request I’m doing
r
it's returning a 404?
does the request url contain that trailing slash?
j
I don’t know what’s returning. The problem is that I enabled logs but I dunno what’s happening as I don’t see any in the console. I’m afraid the trailing slash is necessary 😞
r
does the request being sent to the wiremock server definitely contain the trailing slash in its url?
j
As you can see in the original stubs I share, they have it
r
im not referring to the stubs, im referring to the request that is being sent to the wiremock server in your test that's failing
j
I can’t see anything in the logs:
Copy code
logging:
  level:
    com.github.tomakehurst.wiremock: DEBUG
    org.springframework.cloud.contract.wiremock: DEBUG
r
what does your failing test look like?
j
I perform a request with MockMvc, and then I perform some assertions in the end. When changing the stub, I get errors:
Copy code
org.opentest4j.AssertionFailedError: Expected value to be true.
The endpoint is definitely returning a 404, since I have a catch for that
Copy code
... because account was not found for email <mailto:user@my-company.com|user@my-company.com>
r
i want to know what the url that the request your test is performing is
it's really hard to diagnose the issue with the amount of information i have. if you can provide a repo with a stripped down version of your test that demonstrates the problem ill take a look
j
I’m not getting more information in the logs. The stubs with the urlPath added above are working fine, so I assume those are the requests the tests are performing
Do you know any other method to get more verbose output?
r
on the cli you can use
--verbose
in java you can set the notifier to verbose with
Copy code
WireMockConfiguration.options().notifier(new ConsoleNotifier(true))
🙌 1
j
I ran debugging, and it seems that the issue is coming from here, since the file couldn’t be found:
Copy code
"bodyFileName": "abc_getUserByUsername_{{request.pathSegments.[5]}}.json",
r
having you enabled the response templating extension?
j
I did creating a bean, it works fine for other stubs:
Copy code
@Bean
    fun wireMockConfigurationCustomizer(): WireMockConfigurationCustomizer {
        return WireMockConfigurationCustomizer { config: WireMockConfiguration ->
            config.extensions(ResponseTemplateTransformer(true))
r
what's the value you're putting in the path segment? does that abc_getUserByUsername_[YOUR_PATH_SEGMENT_VALUE].json exist?
can you see what the response from wiremock contained?
j
This is the error I can see, unfortunately I cannot read the filename:
Copy code
feign.FeignException$InternalServerError: [500 Server Error] during [GET] to [<http://localhost:11235/service/api/v1/user/username/user%40my-company.com/>] [ServiceClient#getUserByUsername(String)]: [{
"servlet":"com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet-7385c2f0",
"cause0":"java.io.FileNotFoundException: /Users/name/git/service/src/test/... (596 bytes)]
r
can you put a breakpoint into where the response is received and read the response body yourself?
j
I think I got it, in the response, the username is picked up with the ‘@’ instead of ‘%40’
r
okay that'll be because the url path segments are being decoded by wiremock. this is expected behaviour. changing your filenames to use
@
symbols should fix it
j
Yeah, I did work
But if I create the stubs with
urlPath
then I have to use ‘%40’ in both the url and the filenames
r
are you referring to the original stubs in your first message?
in those stubs, you've explicitly set
bodyFileName
to
abc_getUserByUsername_user%40my-company.com.json
. the url for these stubs has no relation to the
bodyFileName
field of the stub so you can name your files what ever you like in that scenario
j
Yes, in any case. I managed to fix it. Thanks!
r
no worries
🙌 1