jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

Custom response based on headers #77

Closed grug closed 5 years ago

grug commented 5 years ago

I know there have been similar issues opened in here but I think this is slightly different (feel free to correct me here)

Is there a way to have multiple mocks for the same endpoint, but the responses are different based on headers

i.e.

a mock of GET /foo that responds with {hello: 'world'}

AND

a mock of GET /foo with an auth token in the request to respond with {something: 'else'}

Please let me know if my question is not clear enough and I'll provide more details.

Thanks

jameslnewell commented 5 years ago

Yeah!

mock.get('/foo', (req, res) => {
  if (!req.header('Authorisation')) {
      return res.body(JSON.stringify({hello: 'world'}));   
  } else {
      return res.body(JSON.stringify({something: 'else'}));   
  }
});
grug commented 5 years ago

Oh, perfect!

grug commented 5 years ago

I'll give that a go and let you know :)