【问题标题】:How to Test Podio Webhooks Locally如何在本地测试 Podio Webhook
【发布时间】:2019-06-04 04:44:12
【问题描述】:

我想在本地测试 Podio webhook。我正在使用Conveyor.cloud 进行隧道传输,并使用他们的 Twilio 示例成功对其进行了测试。我在将代码转换为使用 Podio 时遇到的问题是 Twilio 示例使用了控制器,而 http://podio.github.io/podio-dotnet/webhooks/ 的 Podio webhook 示例使用了 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;}
        }
    }
}

我错过了什么?

【问题讨论】:

  • 为什么要实现HttpHandler?您可以使用普通的控制器操作来完成
  • @AkbarBadhusha 我尝试只使用正常的控制器操作,但我不知道如何访问context。在带有 Twilio 示例的原始控制器中,我有一个 Index 方法:public TwiMLResult Index(SmsRequest request) { var response = new MessagingResponse(); response.Message("Hello World"); return TwiML(response); }This 提供了一个 SmsRequest 对象。如何获取 Podio 对象?我的方向正确吗?
  • 为什么需要上下文?

标签: c# podio


【解决方案1】:

您可以按照下面的方式来代替使用 HttpContext。我基本上使用 requestbin 来获取请求,然后直接使用 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 框架将处理请求到模型的转换。

请求 bin 的链接https://requestbin.com/

【讨论】:

  • 我在上面使用了您的代码,添加了依赖项并进行了编译。但是当我尝试验证 Podio ProcessRequest 中的 webhook 没有触发时。我尝试将 webhooksproject.conveyor.cloud/Web/ProcessRequestwebhooksproject.conveyor.cloud/Web 用于 webhook。我仍在使用传送带。我是否需要使用 RequestBin 或 Postman 来运行此代码?我以前没有使用过它们。我花了过去 3 周的时间试图弄清楚这一点,但似乎无法理解。谢谢你帮助我。
  • 我建议使用 requestbin 和 postman。它不会像在隧道中那样感到舒适,但它工作得很好。使用 requestbin 提供的公共 url 创建 podio 挂钩。然后通过requestbin获取podio api发布的json。使用邮递员模拟流程。如果您需要上述任何方面的帮助,请告诉我
  • 我不明白。所以我从 RequestBin 得到一个 url。然后我使用该 url 在 Podio 中创建一个 webhook。到目前为止是正确的吗?如果是这样,那么当 Podio 发送验证请求时,该请求如何通过 Postman 发送到我的本地计算机?
  • 在本地手动测试 podio 挂钩的步骤 1. 从 requestbin 获取公共 uri 并使用该 url 创建挂钩 2. 从 requestbin 获取 json 3. 使用邮递员手动将该 json 发布到本地机器。您需要手动完成。我不确定是否还有其他选择。但我觉得这很容易。
  • 我将您的 PodioHook 代码应用到了我与 Twilio 和 Conveyor 一起使用的控制器上,并且成功了。谢谢!
【解决方案2】:

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

单元测试

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

集成测试

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

端到端测试

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

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

通过在端到端测试期间在本地运行您的服务,您将可以访问您的 IDE 进行更改以及您的所有调试工具,这意味着您将提高工作效率并节省时间。有使用expose.sh 测试webhooks 的指南here

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多