我有一些代码如下所示:
use SomeApp;
use Test::WWW::Mechanize::PSGI;
my $mech = Test::WWW::Mechanize::PSGI->new(
app => sub { SomeApp->run(@_) },
);
$mech->get_ok('/');但是,一旦调用了get_ok(),我就会收到以下警告:
PSGI error: failed to listen to port 8080: Address already in use at .../5.18.1/HTTP/Server/PSGI.pm line 94.
HTTP::Server::PSGI::setup_listener('HTTP::Server::PSGI=HASH(0x7fe6622fad60)') called at .../5.18.1/HTTP/Server/PSGI.pm line 54是的,我在用那个端口做别的事。来自the docs of Test::WWW::Mechanize::PSGI
此模块允许您测试PSGI web应用程序,但不需要服务器或发出HTTP请求。相反,它直接将HTTP请求对象传递给PSGI。
因此,从理论上讲,我不需要指定端口,但是我会得到上面的警告,获取的页面返回500 (它们在浏览器中工作得很好)。我遗漏了什么?
将MyApp->run更改为MyApp->psgi_app会导致:
Can't call method "request" on an undefined value at .../5.18.1/Test/WWW/Mechanize/PSGI.pm line 47.此错误可以通过以下方法复制:
catalyst.pl MyApp
cd MyApp
# run the test program above发布于 2013-12-28 10:36:21
催化剂的run方法实际上将运行HTTP服务器(通过Plack/PSGI!)对于开发,这不是您希望通过PSGI (不运行服务器)进行测试的结果。您需要:app => MyApp->psgi_app,没有额外的sub块,因为psgi_app应该返回PSGI应用程序本身。
错误消息“无法在.上调用方法‘请求’”是一个常见的错误,当您的应用程序返回的东西是不正确的PSGI规范。git主服务器上的消息已经得到了一些改进,但它本质上是一个用户错误,因为您基本上是在返回sub { $app },而它只希望只返回$app。
有关催化剂支持PSGI的更多文档可以在perldoc Catalyst::PSGI中获得。
发布于 2013-12-28 10:04:58
马特·特劳特提到LWP::Protocol::PSGI是一种解决办法。它劫持了HTTP以使其工作:
use Test::WWW::Mechanize;
use LWP::Protocol::PSGI;
use MyApp;
LWP::Protocol::PSGI->register( MyApp->psgi_app(@_) );
my $mech = Test::WWW::Mechanize->new;
# first GET must be absolute
$mech->get('http://localhost/login');
say $mech->content;
# then we can switch to relative
$mech->get('/login');
say $mech->content;简而言之,上面的内容或多或少都是“货”(因为我不明白为什么第一个版本失败了),但对我来说已经足够了。
https://stackoverflow.com/questions/20812992
复制相似问题