I have a problem with testing my node application using using Nock. I record all requests via nock.recorder.rec
, but among them there multipart request. I use form-data. This module put the boundary to request body, when i use function form.append
. The problem is that the boundary is always different and when i run tests with recorded data Nock can't find match for request (because boyundary in request body not what was when recording). What can be done? Sorry for my bad English.
Another solution would be to use a RegExp
:
nock(baseUrl)
.post(`/url', /form-data; name="field"[^]*value/m)
.reply(200, 'some data');
Note
^
) within the regexp (because the form data might contain line breaks)m
flag for multilineform.append('field', value);
RegExp
class.I came across a similar problem. What you can do is use the second argument as as a function instead and match the object you're trying to send as form data. Example:
nock('localhost')
.post('/url', function(body) {
return JSON.stringify(body) === JSON.stringify(params);
})
.reply(200, 'some data');
More on that in the documentation here: https://github.com/pgte/nock#specifying-request-body
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With