xetorthio / shmock

Simple HTTP Mocking Library
MIT License
101 stars 20 forks source link

Same url different query #2

Open tinchogob opened 10 years ago

tinchogob commented 10 years ago

Support to mock an url and reply different things acording to the query.

  mock.post("/get_query")
    .query({total: 10, limit: 1})
    .query({foo: "bar"})
    .reply(200, {name: "clark"});

  mock.post("/get_query")
    .query({foo: "bar"})
    .reply(200, {name: "kent"});

  test.post("/get_query").query({total: 10, limit: 1, foo: "bar"}).expect(200).end(function(error, response) {
    response.body.name.should.equal("clark");
    done();
  });

  test.post("/get_query").query({foo: "bar"}).expect(200).end(function(error, response) {
    response.body.should.equal("kent");
    done();
  });
kylezeeuwen commented 10 years ago

I am thinking of making an attempt to implement this. Any initial thoughts or example codes? My main concern is that query() is written to fail via exception if the query params do not match, so if I modify this behaviour it will be a breaking change. I was considering using a different verb, say querySet() or queryAlt() or something better, so that I dont have to worry about backwards compatability. For example:

  Handler1 = mock.get("/path1")
    .persist()
    .querySet({ a: 1 })
    .reply(200, response1)

  Handler2 = mock.get("/path1")
    .persist()
    .querySet({ a: 2 })
    .reply(200, response2)

Without implementing I assume that in querySet I will need to do a deep comparison of actual and expected query params and either respond, or call next(). Open question is what to do if none of the querySet's match? Is it possible to abort and return nothing? is a 500 more appropriate? Maybe a way to specify the default behaviour:

  HandlerDefault = mock.get("/path1")
    .persist()
    .queryElse()
    .reply(defaultResponseStatus, defaultResponse)

Thoughts?

kylezeeuwen commented 10 years ago

see https://github.com/xetorthio/shmock/pull/7

kylezeeuwen commented 10 years ago

7 was a poor solution I have refactored and resubmitted see https://github.com/xetorthio/shmock/pull/8