3

I'm using rspec and capybara to write test cases to integrate with Cheddargetter (a payment solution provider). I have no problems testing my requests to CG's API however I'm not sure how to best test when CG's API giving a web callback to the Rails app.

This is similar to PayPal's IPN feature, where after a customer has paid a web hook callback is sent to your app.

Just wondering if anyone know what is the best way to test / simulate this?

redshift5
  • 1,966
  • 1
  • 18
  • 27

1 Answers1

3

You probably are using a controller to handle POST requests, let's call it WebhookController

You can simply test that a post with the params of what you need is doing what you want. E.g.,I ntegration tests (in test unit but rspec does the same thing).

Rspec may have a different version of fixture_file_upload for uploading/adding an xml file, but according to this stack question it looks like you can use it too. Stick the file in say spec/files.

Regardless, for the web and noobs, you would be testing that your Delayed::Job call actually works in another test. Something like:


class GetWebhookTest < ActionController::IntegrationTest
  fixtures :all
  def recieve_webhook
    post '/webhook/338782', fixture_file_upload('webhook.xml', 'application/xml')
  end
  #Test you do what the outcome of your POST would be.
  #this is refactored but you can shove the post line where receive_webhook is
  test "recieve a webhook via xml" do
    assert_difference('RawData.count') do
      receive_webhook
    end
  end

  test "make sure the status is 200" do
    recieve_webhook
    assert_response :success
  end
  #Test 1 will fail before this, but i was more/too thorough back in the day
  test "Delayed Job increases" do
    assert_difference "Delayed::Job.count", 1 do
      recieve_webhook
    end
  end
end

Again, Rspec has things like response.should be_success and their Object.count difference method too. Adjust for your situation. The key is fixture_file_upload

Community
  • 1
  • 1
pjammer
  • 9,489
  • 5
  • 46
  • 56
  • This looks like a good way to do it. Any there any existing gems to help with this kind of testing? Particularly to help with saving an existing POST request and storing it to a file to reuse later (like VCR, but for callbacks instead of requests) – redshift5 Nov 09 '12 at 00:53
  • i think you want to record an actual call in a `postbin` (i think the service is called). Like this http://requestb.in/ and you'll get the xml you need. After that, it's fixture_file_upload and normal tests really. Most gems I've seen are Outgoing, not for incoming and that's the role of fixture_file_upload. imo. – pjammer Nov 09 '12 at 02:44
  • Thanks, although the requestb.in service seems to be broken for some reason. I get a 500 internal error if I check the AJAX call in the web console. I even checked out and deployed my own version to Heroku and had the same issue. I'll look for an alternative service. Can you think of another on top your head? Thanks by the way – redshift5 Nov 09 '12 at 03:37
  • It's quite refreshing to use something like [Runscope](https://www.runscope.com/docs/request-capture) to receive webhooks as an intermediary to your app. You can then use it to search and troubleshoot past hook data.There's some additional info in the [Cheddar Webhooks](http://support.getcheddar.com/kb/api-8/web-hooks-service-hooks-and-captain-hooks#custom-url-testing) docs. – marcguyer Feb 26 '18 at 13:30
  • Note that Runscope is sunsetting their Traffic Inspector so my suggestion is no longer valid. [Check out their blog post for alternative solutions](https://blog.runscope.com/posts/phasing-out-traffic-inspector) – marcguyer Mar 30 '18 at 19:14
  • Hmm, is xml the only supported file format for this? I have a json request or a yaml request but can't seem to use these files as fixtures in this way. – Justin Oct 21 '22 at 02:21