在使用网络任务处理Perl模块时,我必须在测试时检查网络可用性或失败。更确切地说,我需要测试网络可用性,然后测试特定的API可用性。
下面是我的api-interraction.t测试中的一些代码:
use strict;
use warnings;
use Test::More;
# Foo::Module is using WWW::Curl::Simple
my $requester = Foo::Module->new();
my $query = "http://existing-doma.in?with=some&useful=parameters";
# We should be able to test a bad request too
my $wrong_query = "http://deaddoma.in";
my $api_test_code = $requester->get_api_status($query);
if ($api_test_code == 200) {
cmp_ok($api_test_code, "==", 200, "API reachable - HTTP status = 200");
} else {
cmp_ok($api_test_code, "!=", 200, "API not reachable - HTTP status != 200");
} 当网络失败时,它似乎无法工作。关于我如何能更好地实现这一点,有什么建议吗?
发布于 2014-01-01 13:06:44
use strict;
use warnings;
use LWP::Simple;
use Test::More tests => 1;
my $query = "http://existing-doma.in";
my $browser = LWP::UserAgent->new;
my $response = $browser->get( $query );
is $response->code, 200;https://stackoverflow.com/questions/20868550
复制相似问题