8

I need to mock a request using nock module, which is issued by a module that adds extra headers (x-md5-checksum). And the request does not match because of those headers.

How do I force nock to ignore this header and match the request anyways?

Thanks.

moltar
  • 1,372
  • 1
  • 11
  • 18
  • the header isn't being matched because you don't know the `md5-checksum`? – Marcos Casagrande Jun 06 '18 at 23:21
  • 1. I don't know the checksum 2. The checksum is not stored in the recorded set. – moltar Jun 07 '18 at 19:00
  • The final problem was not with the fact that the headers were interfering, but that I was not using `filteringPath` correctly, was calling it twice, which stomped on the first call and thereby did not change the URL correctly and it never matched. Now I am not sure what to do about this question on SO. What is the etiquette to close this down when the question was not even correct to begin with. – moltar Jun 07 '18 at 20:05

1 Answers1

6

Aaccording to the docs, if reqHeaders is not specified, it will skip them.

If no request headers are specified for mocking then Nock will automatically skip matching of request headers

In case you're validating other headers, and if x-md5-checksum is present, but you don't know it's value, you can use a function or a regex to validate any value, or just a valid md5

nock('http://www.example-com', {
   reqHeaders: {
     'x-md5-checksum': /[a-fA-F0-9]{32}/
     // or
     'x-md5-checksum': value => true // I don't care about the value
   }
})
// or use .matchHeader
.matchHeader('x-md5-checksum', value => true)
.get('/')
.reply(200)
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • I tried that header option, but on nockBack interface and it didn't make any difference. In fact the matching fn never ran. Is this different when running via nockBack? – moltar Jun 07 '18 at 19:02
  • I don't know about nock.back, you didn't specified, you said nock, and my answer is how you do it in nock. Always provide either code or the full details, so we don't assume. – Marcos Casagrande Jun 07 '18 at 21:44