What is the correct way to validate Webhooks?

I have a ReSTFul API in Meteor. I am using hooks from Mandrill, Stripe and other libraries to update the collection.

Router.route('/mandrill/message_rejected', { where: 'server' })
  .post(function () {
    var request = EJSON.parse(this.request.body.mandrill_events);

    var rejects = _.map(_.where(request, {
      event: 'reject'
    }, {
      return object.msg.email;
    });

    Meteor.users.update({
      emails: {
        $elemMatch: {
          "address": {
            $in: rejects
          }
        }
      }
    }, {
      $set: { status: 'rejected' }
    });

    this.response.end();
  });

      

My question is, how can I automate tests for this? The request must come from Mandrill. Is there a way to test web host messages in a consistent way?

+3


source to share


1 answer


I am using Mocha (although you can use other testing frameworks like Jasmine ).

I am combining tests with a superagent library that allows HTTP requests.

The next part does the trick: set up the log and save the received JSON from Mandril or other hooks you received and create a library (or binding) of incoming responses.

Then you can create the various cases you want, for example:



  • Removing an expected field
  • Submitting duplicates
  • Etc

Ensuring compliance with this method requires you to spend time thinking about what hooks you expect to get by reading the documentation to assess if the case you think is impossible to address, and so on.

I recommend that you keep a log of the hooks you receive to improve your tests during this time.

+5


source







All Articles