如何在本地测试跑道 Webhooks

How to Test Podio Webhooks Locally

我想在本地测试跑道 webhooks。我正在使用 Conveyor.cloud for tunneling and tested it successfully with their Twilio example. The problem I am having with converting the code to work with Podio is that the Twilio example used a Controller and the Podio webhook example at http://podio.github.io/podio-dotnet/webhooks/ 使用 IHttpHandler。

我尝试在下面的代码中为控制器实现 IHttpHandler,但它不起作用。

using System;
using System.Web;
using System.Web.Mvc;
using PodioAPI;

namespace WebhooksProject.Controllers
{
    public class WebController : IHttpHandler
    {
        public static string clientId = "abcd";
        public static string clientSecret = "abcd";
        public static string username = "a@b.com";
        public static string password = "abcd";

        public static Podio podio = new Podio(clientId, clientSecret);

        public void ProcessRequest(HttpContext context)
        {
            podio.AuthenticateWithPassword(username, password);

            var request = context.Request;

            switch (request["type"])
            {
                case "hook.verify":
                    podio.HookService.ValidateHookVerification(int.Parse(request["hook_id"]), request["code"]);
                    break;
                // An item was created
                case "item.create":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int itemIdOfCreatedItem = int.Parse(request["item_id"]);
                    // Fetch the item and do what ever you want
                    break;

                // An item was updated
                case "item.update":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int itemIdOfUpdatedItem = int.Parse(request["item_id"]);
                    // Fetch the item and do what ever you want
                    break;

                // An item was deleted    
                case "item.delete":
                    // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                    int deletedItemId = int.Parse(request["item_id"]);
                    break;
            }
        }

        public bool IsReusable
        {
            get{return false;}
        }
    }
}

我错过了什么?

您可以按照下面的方式来代替使用 HttpContext。我基本上是用requestbin来获取request然后直接用Postman试试。

    [HttpPost]
    public HttpResponseMessage ProcessRequest(PodioHook hook)
    {
        var oAuth = _podio.AuthenticateWithApp(_appId, _appToken); // keep the oauth if you are reusing it.

        switch (hook.type)
        {
            case "hook.verify":
                _podio.HookService.ValidateHookVerification(hook.hook_id, hook.code);
                break;
            // An item was created
            case "item.create":
                // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                long itemIdOfCreatedItem = hook.item_id;
                // Fetch the item and do what ever you want
                break;

            // An item was updated
            case "item.update":
                // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                long itemIdOfUpdatedItem = hook.item_id;
                // Fetch the item and do what ever you want
                break;

            // An item was deleted    
            case "item.delete":
                // For item events you will get "item_id", "item_revision_id" and "external_id". in post params
                long deletedItemId = hook.item_id;
                break;
        }

        return new HttpResponseMessage(HttpStatusCode.OK);
    }

我的 PodioHook 模型如下所示

public class PodioHook
{
    public string code { get; set; }
    public string type { get; set; }
    public long item_id { get; set; }
    public int hook_id { get; set; }
}

.NET 框架将处理模型转换请求。

link 请求 bin https://requestbin.com/

有多种方法可以在本地测试您的 webhook。

单元测试

您可以单元编写单元测试,然后使用模拟库(例如 .NET 的 Moq)在您的方法中模拟 context 参数。这是最快的方法,但最好最后完成,因为您需要知道 webhook 请求格式的确切结构才能模拟它。

集成测试

您可以使用 Postman 或类似工具或编写代码来创建对端点的 http 请求,使用与 webhook 相同的请求格式。同样,您还需要知道您的 webhook 提供商将发送给您的请求格式。

端到端测试

这是您触发提供商访问您的 webhook 的地方。与生产中将发生的情况相比,这是最现实的方法。我喜欢先执行此方法,因为我可以在集成和单元测试中重新使用 webhook 请求的请求结构。

您需要在 public URL 处公开您的端点。虽然您可以将代码部署到 public 服务器,但更简单的方法是使用 expose.sh。例如,您可以 运行 expose 80,它会通过 public URL 公开您的本地服务器,例如 https://a2cD.expose.sh.

通过 运行 在端到端测试期间将您的服务本地化,您将可以访问您的 IDE 进行更改和所有调试工具,这意味着您将获得更多高效并节省时间。有一个使用 expose.sh 测试 webhooks here

的指南

免责声明:我构建了 expose.sh