LWP::Protocol::PSGI->register($app, %options); my $guard = LWP::Protocol::PSGI->register($app, %options);
Registers an override hook to hijack HTTP requests. If called in a non-void context, returns a Guard object that automatically resets the override when it goes out of context.
When %options is specified, the option limits which URL and hosts this handler overrides. You can either pass host or uri to match requests, and if it doesnt match, the handler falls back to the original LWP HTTP protocol implementor.
The options can take either a string, where it does a complete match, a regular expression or a subroutine reference that returns boolean given the value of host (only the hostname) or uri (the whole URI, including query parameters).
There are similar modules on CPAN that allows you to emulate LWP requests and responses. Most of them are implemented as a mock library, which means it doesnt go through the LWP guts and just gives you a wrapper for receiving HTTP::Request and returning HTTP::Response back.
Test::LWP::UserAgent has the similar concept of overriding LWP request method with particular PSGI applications. It has more features and options such as passing through the requests to the native LWP handler, while LWP::Protocol::PSGI only allows to map certain hosts and ports.
Test::LWP::UserAgent requires you to change the instantiation of UserAgent from LWP::UserAgent->new to Test::LWP::UserAgent->new somehow and its your responsibility to do so. This mechanism gives you more control which requests should go through the PSGI app, and it might not be difficult if the creation is done in one place in your code base. However it might be hard or even impossible when you are dealing with third party modules that calls LWP::UserAgent inside.
LWP::Protocol::PSGI affects the LWP calling code more globally, while having an option to enable it only in a specific block, thus theres no need to change the UserAgent object manually, whether it is in your code or CPAN modules.
Tatsuhiko Miyagawa <email@example.com>
Copyright 2011- Tatsuhiko Miyagawa
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|perl v5.20.3||LWP::PROTOCOL::PSGI (3)||2014-05-19|