测试 Snap Web 框架处理程序

发布于 2024-12-26 05:21:42 字数 1239 浏览 1 评论 0原文

我想为我的 Snap Web 处理程序编写一些集成测试,但我陷入困境。这是场景。我有一个 Snap Web 处理程序(普通风格)CRUD 类型,它看起来像这样:

create :: AppHandler ()
create = method POST $ do
         lastName  <- decodeUtf8 . fromJust <$> getParam "lastName"
         firstName <- decodeUtf8 . fromJust <$> getParam "firstName"
         createPerson $ Person firstName lastName
         modifyResponse (setResponseCode 204)

Snap.Test 模块有一些东西可以帮助构建请求,并且我用它向我的处理程序发出请求:

createOwnerReq :: RequestBuilder IO () 
createOwnerReq = postUrlEncoded "host/person/create" $
                 fromList [ ("firstName", ["Greg-Shaw"])
                          , ("lastName",  ["Snoy'Sullivan"])
                          ]

问题是,我想为此处理程序创建一个 TestUnit TestCase,因此我需要在 createOwnerReq 请求上运行处理程序。模块 Snap.Test 提供:

 runHandler :: MonadIO a => RequestBuilder m () -> Snap a -> m Response

所以

 ... do 
     resp <- runHandler createOwnerReq ??? 

但是等等!我的请求处理程序的类型为 AppHandler (),但 runHandler 需要一个 Snap a 类型的处理程序。 如何将我的 AppHandler 类型提升到 Snap monad 中?请帮忙,这让我很困惑。

I want to write little integration tests for my Snap web handlers but I am stuck. Here is the scenario. I have a Snap web handler that (run-of-the-mill style) CRUDs up a type and it looks something like this:

create :: AppHandler ()
create = method POST $ do
         lastName  <- decodeUtf8 . fromJust <
gt; getParam "lastName"
         firstName <- decodeUtf8 . fromJust <
gt; getParam "firstName"
         createPerson $ Person firstName lastName
         modifyResponse (setResponseCode 204)

The Snap.Test module has some things to help build up a request and I use it to make a request for my handler:

createOwnerReq :: RequestBuilder IO () 
createOwnerReq = postUrlEncoded "host/person/create" $
                 fromList [ ("firstName", ["Greg-Shaw"])
                          , ("lastName",  ["Snoy'Sullivan"])
                          ]

Here's the problem, I want to make a TestUnit TestCase for this handler so I need the run the handler on the createOwnerReq request. The module Snap.Test provides:

 runHandler :: MonadIO a => RequestBuilder m () -> Snap a -> m Response

so

 ... do 
     resp <- runHandler createOwnerReq ??? 

But wait!!! My request handler is of type AppHandler () but runHandler requires a Handler of type Snap a.
How do I lift my AppHandler type into the Snap monad? Help please, this is kind of trippin' me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

数理化全能战士 2025-01-02 05:21:42

Ibolla 的 return create 技巧可能无法达到您想要的效果。它可以正确编译,因为 runHandler 采用 Snap a ,它将对具有任何返回值的 Snap 操作起作用。 return create::Snap (AppHandler()),这与您可能期望的Snap()非常不同。

我们正在开发 Snap.Snaplet.Test 等效项,它将包装 Snap.Test 提供的 runHandler 函数,以允许您测试处理程序。这可能会包含在 snap 包的 0.10 版本中。

在此期间,您可以使用 runSnapletSnapletInit 转换为可传递给 Snap.Test.runHandler 的 Snap () 操作。这不会让您测试单个Handler,但它可以让您测试应用程序初始化程序中定义的任何路由。

编辑:在snap-0.10中,我们添加了 测试对 snaplet 的支持

Ibolla's return create trick probably doesn't do what you want. It compiles correctly because runHandler takes a Snap a which will work on a Snap action with any return value. return create :: Snap (AppHandler ()), which is very different from the Snap () that you were probably expecting.

We are working on a Snap.Snaplet.Test equivalent that will wrap the runHandler function provided by Snap.Test to allow you to test Handlers. This will probably be included in the 0.10 release of the snap package.

In the interim, you can solve the problem manually by using runSnaplet to convert your SnapletInit into a Snap () action that can be passed to Snap.Test.runHandler. This won't let you test an individual Handler, but it will let you test any of the routes defined in your application's initializer.

EDIT: In snap-0.10, we added test support for snaplets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文