Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nock + multipart form data = No match for request

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.

like image 326
psixdev Avatar asked Aug 27 '15 07:08

psixdev


2 Answers

Another solution would be to use a RegExp:

nock(baseUrl)
    .post(`/url', /form-data; name="field"[^]*value/m)
    .reply(200, 'some data');

Note

  • the beginning of input character (^) within the regexp (because the form data might contain line breaks)
  • the m flag for multiline
  • the example above corresponds to: form.append('field', value);
  • for a real-world example see here. This also shows how to use a variable within the regex, using the RegExp class.
like image 32
schnatterer Avatar answered Sep 17 '22 18:09

schnatterer


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

like image 163
Carlos Rymer Avatar answered Sep 20 '22 18:09

Carlos Rymer