测试 Catalyst REST API 最简单的方法是什么

发布于 2024-12-18 14:53:26 字数 374 浏览 1 评论 0原文

我正在使用 Catalyst::Controller::REST 构建 RESTful Web 服务。通常对于网络测试,我使用 Test::WWW::Mechanize,但这似乎更适合“GET/POST HTML RPC”测试。是否有任何测试模块可以使用 GET/POST/PUT/DELETE 等和 JSON 轻松地通过基本身份验证进行 HTTP 测试?也许可以与 Catalyst/PSGI 很好地集成,这样我就不必启动网络服务器了?

I'm building a RESTful web service, using Catalyst::Controller::REST. Usually for web testing I use Test::WWW::Mechanize, but that seems more appropriate for "GET/POST HTML RPC" testing. Are there any Test modules that would make testing of HTTP with basic auth, using GET/POST/PUT/DELETE etc and JSON easy? perhaps something that integrates well with Catalyst/PSGI so I don't have to start a webserver?

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

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

发布评论

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

评论(2

时光瘦了 2024-12-25 14:53:26

Catalyst::Test 是 LWP::UserAgent 的子类。下面应该给你正确的想法:

#!/usr/bin/env perl
use warnings;
use strict;

use Test::More;
use Catalyst::Test 'MyApp';
use HTTP::Request::Common;
use JSON::Any; # or whatever json module you usually use
my $data = 'some_json_data_here';
my $res = request(
    POST '/some_path',
    Content_Type => 'text/xml',
    Content => $data,
);

my $content = json_decode($res->content); # or whatever, can't remember the interface.
my $expected = "some_data";
is_deeply ( $content, $expected); 

Catalyst::Test is a subclass of LWP::UserAgent. The below should give you the right idea:

#!/usr/bin/env perl
use warnings;
use strict;

use Test::More;
use Catalyst::Test 'MyApp';
use HTTP::Request::Common;
use JSON::Any; # or whatever json module you usually use
my $data = 'some_json_data_here';
my $res = request(
    POST '/some_path',
    Content_Type => 'text/xml',
    Content => $data,
);

my $content = json_decode($res->content); # or whatever, can't remember the interface.
my $expected = "some_data";
is_deeply ( $content, $expected); 
み零 2024-12-25 14:53:26

或者用更现代的说法:

  my $data = '{"username":"xyz","password":"xyz"}';
  my $res = request
    (
     POST '/bar/thing',
     Content_Type => 'application/json',
     Content => $data,
    );

;)

Or in more modern parlance:

  my $data = '{"username":"xyz","password":"xyz"}';
  my $res = request
    (
     POST '/bar/thing',
     Content_Type => 'application/json',
     Content => $data,
    );

;)

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