如何测试 mochiweb 应用程序?

发布于 2024-10-10 11:54:54 字数 1186 浏览 10 评论 0原文

我想对我的 mochiweb 应用程序进行高级测试,就像在 Python 中使用 WebTest、在 Django 中使用测试客户端以及在 Ruby on Rails 中使用功能或集成测试一样。我对如何去做并不太挑剔。基本上我只想发送或模拟 HTTP 请求并对结果进行断言。

我重构了我的代码,以便我的请求处理程序不会自行调用 Req:respond() ,而是返回一个元组 {Code, Headers, Body}。这允许我构建虚拟请求 mochiweb_request:new(),将它们传递给我的请求调度程序并使用 EUnit 对结果进行断言:

make_request(Method, Path) ->
    Request = mochiweb_request:new(nil, Method, Path, {1, 1},
        mochiweb_headers:make([{"Accept", "text/html"}])),
    Response = myapp:dispatch(Request),
    Response.


signin_test() ->
    {Code, _, _} = make_request('GET', "/signin"),
    ?assertEqual(Code, 200),
    ok.

现在我的问题是如何测试 POST 请求。我在 mochiweb 中没有找到任何可以让我这样做的东西。从阅读 mochiweb 代码来看,当调用 Req:parse_post() 时,它似乎会触发套接字上的读取。我在 src/mochiweb.erl。显然,这涉及构建 HTTP 消息、将它们写入套接字并读回响应。我尝试在自己的测试代码中执行此操作,但几个小时后我没有成功。我开始怀疑我的方向是否正确。也许我需要将我的功能与 HTTP 管道进一步解耦,即。不从我的请求处理程序中调用 Req:parse_post() 。我想到的另一个解决方案是使用第三方 Web 测试库,不必用 Erlang 编写。也许是 Ruby 或 Python 甚至 Selenium 的东西。

那么您会推荐什么解决方案来对 mochiweb 应用程序进行功能或集成测试?

I would like to do high level testing of my mochiweb app, like it is possible to do in Python with WebTest, in Django with the test client and in Ruby on Rails with functional or integration testing. I'm not too picky about how to do it. Basically I would just like to send or simulate HTTP requests and make assertions on the result.

I refactored my code so that my requests handler would not call Req:respond() themselves, but return a tuple {Code, Headers, Body}. This allows me to build dummy requests with
mochiweb_request:new(), pass them to my request dispatcher and make assertions on the result using EUnit:

make_request(Method, Path) ->
    Request = mochiweb_request:new(nil, Method, Path, {1, 1},
        mochiweb_headers:make([{"Accept", "text/html"}])),
    Response = myapp:dispatch(Request),
    Response.


signin_test() ->
    {Code, _, _} = make_request('GET', "/signin"),
    ?assertEqual(Code, 200),
    ok.

Now my problem is how to test POST requests. I didn't find anything in mochiweb that would allow me to do that. From reading mochiweb code, it seems that when Req:parse_post() is called, it triggers a read on a socket. I found some interesting test code in src/mochiweb.erl. Apparently this involves building HTTP messages, writing them on a socket and reading the response back. I tried to do this in my own test code but I didn't manage to get anywhere after a few hours. I'm beginning to wonder if I'm going in the right direction. Maybe I need to decouple even more my functionality from the HTTP plumbing, ie. not call Req:parse_post() from within my request handlers. Another solution I'm thinking of would be to use a third-party web testing library, not necessary written in Erlang. Maybe something in Ruby or Python or even maybe Selenium.

So what solution would you recommend to do functional or integration testing of a mochiweb app?

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

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

发布评论

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

评论(3

双马尾 2024-10-17 11:54:54

我们主要将测试与 HTTP 管道分离。无论如何,通常在功能环境中做正确的事情。

src/mochiweb_multipart.erl 中有一些 eunit 代码可能与您的特定用例更相关。

We mostly decouple our tests from the HTTP plumbing. Usually the right thing to do in a functional environment anyway.

There is some eunit code in src/mochiweb_multipart.erl that is probably more relevant to your particular use case.

原来是傀儡 2024-10-17 11:54:54

我使用通用测试服务器,并使用 ibrowse 进行测试。这是一个例子:

post_cap_query_no_caps(_Config) ->
A="<?xml version=\"1.0\" encoding=\"utf-8\"?><query><capabilities/></query>",
{ok, "200", _C, Body}=ibrowse:send_req("http://localhost:8000/devices",  XML_CONTENT_TYPE, post, A),
"<devices/>" == Body.

i use the common test server and for testing i use ibrowse. Here is an example :

post_cap_query_no_caps(_Config) ->
A="<?xml version=\"1.0\" encoding=\"utf-8\"?><query><capabilities/></query>",
{ok, "200", _C, Body}=ibrowse:send_req("http://localhost:8000/devices",  XML_CONTENT_TYPE, post, A),
"<devices/>" == Body.
清醇 2024-10-17 11:54:54

为什么不直接使用 http 客户端来进行调用呢?我使用 ibrowse 来系统测试我的 webmachine 服务器,包括根据需要发布数据。

Why don't you just use an http client to make the calls? I use ibrowse to system test my webmachine server, including POSTing data as needed.

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