I am trying to chain some requests in Nock with Regex matching on the path. All of the examples I see of Regex path matching are get requests. How do I do this properly? Is the below right?
nock('https://base-url.com')
.post('/v1/secrets/*$', {
username: 'pgte',
email: '[email protected]',
})
.reply(200)
The following code demonstrates a successful Nock on a POST request using a regex on the path.
Things to note about the value passed to the
postmethod; If you want to use regex matching, you need to pass an instance of aRegExpas the first argument. Here I'm using the short-hand notation, but you could also create an instance using thenew RegExp("...")notation, if desired.Also, the example in your question had the asterisk character directly after the last slash. Which translates to the path needs to end with zero or more slashes. I assume that's not what you want, but my point is, use something like https://regex101.com to validate your regexs.
Nock will test the
pathnameof the URL (as defined here) against the regex, including the leading slash.