Debian Codegraph @

README

raw
1 NAME
2
3 LWP::Protocol::PSGI - Override LWP's HTTP/HTTPS backend with your own
4 PSGI application
5
6 SYNOPSIS
7
8 use LWP::UserAgent;
9 use LWP::Protocol::PSGI;
10
11 # $app can be any PSGI application: Mojolicious, Catalyst or your own
12 my $app = do {
13 use Dancer;
14 set apphandler => 'PSGI';
15 get '/search' => sub {
16 return 'searching for ' . params->{q};
17 };
18 dance;
19 };
20
21 # Register the $app to handle all LWP requests
22 LWP::Protocol::PSGI->register($app);
23
24 # can hijack any code or module that uses LWP::UserAgent underneath, with no changes
25 my $ua = LWP::UserAgent->new;
26 my $res = $ua->get("http://www.google.com/search?q=bar");
27 print $res->content; # "searching for bar"
28
29 # Only hijacks specific host (and port)
30 LWP::Protocol::PSGI->register($psgi_app, host => 'localhost:3000');
31
32 my $ua = LWP::UserAgent->new;
33 $ua->get("http://localhost:3000/app"); # this routes $app
34 $ua->get("http://google.com/api"); # this doesn't - handled with actual HTTP requests
35
36 DESCRIPTION
37
38 LWP::Protocol::PSGI is a module to hijack any code that uses
39 LWP::UserAgent underneath such that any HTTP or HTTPS requests can be
40 routed to your own PSGI application.
41
42 Because it works with any code that uses LWP, you can override various
43 WWW::*, Net::* or WebService::* modules such as WWW::Mechanize, without
44 modifying the calling code or its internals.
45
46 use WWW::Mechanize;
47 use LWP::Protocol::PSGI;
48
49 LWP::Protocol::PSGI->register($my_psgi_app);
50
51 my $mech = WWW::Mechanize->new;
52 $mech->get("http://amazon.com/"); # $my_psgi_app runs
53
54 TESTING
55
56 This module is extremely handy if you have tests that run HTTP requests
57 against your application and want them to work with both internal and
58 external instances.
59
60 # in your .t file
61 use Test::More;
62 use LWP::UserAgent;
63
64 unless ($ENV{TEST_LIVE}) {
65 require LWP::Protocol::PSGI;
66 my $app = Plack::Util::load_psgi("app.psgi");
67 LWP::Protocol::PSGI->register($app);
68 }
69
70 my $ua = LWP::UserAgent->new;
71 my $res = $ua->get("http://myapp.example.com/");
72 is $res->code, 200;
73 like $res->content, qr/Hello/;
74
75 This test script will by default route all HTTP requests to your own
76 PSGI app defined in $app, but with the environment variable TEST_LIVE
77 set, runs the requests against the live server.
78
79 You can also combine Plack::App::Proxy with LWP::Protocol::PSGI to
80 route all requests made in your test against a specific server.
81
82 use LWP::Protocol::PSGI;
83 use Plack::App::Proxy;
84
85 my $app = Plack::App::Proxy->new(remote => "http://testapp.local:3000")->to_app;
86 LWP::Protocol::PSGI->register($app);
87
88 my $ua = LWP::UserAgent->new;
89 my $res = $ua->request("http://testapp.com"); # this hits testapp.local:3000
90
91 METHODS
92
93 register
94
95 LWP::Protocol::PSGI->register($app, %options);
96 my $guard = LWP::Protocol::PSGI->register($app, %options);
97
98 Registers an override hook to hijack HTTP requests. If called in a
99 non-void context, returns a guard object that automatically resets
100 the override when it goes out of context.
101
102 {
103 my $guard = LWP::Protocol::PSGI->register($app);
104 # hijack the code using LWP with $app
105 }
106
107 # now LWP uses the original HTTP implementations
108
109 When %options is specified, the option limits which URL and hosts
110 this handler overrides. You can either pass host or uri to match
111 requests, and if it doesn't match, the handler falls back to the
112 original LWP HTTP protocol implementor.
113
114 LWP::Protocol::PSGI->register($app, host => 'www.google.com');
115 LWP::Protocol::PSGI->register($app, host => qr/\.google\.com$/);
116 LWP::Protocol::PSGI->register($app, uri => sub { my $uri = shift; ... });
117
118 The options can take either a string, where it does a complete match,
119 a regular expression or a subroutine reference that returns boolean
120 given the value of host (only the hostname) or uri (the whole URI,
121 including query parameters).
122
123 unregister
124
125 LWP::Protocol::PSGI->unregister;
126
127 Resets all the overrides for LWP. If you use the guard interface
128 described above, it will be automatically called for you.
129
130 DIFFERENCES WITH OTHER MODULES
131
132 Mock vs Protocol handlers
133
134 There are similar modules on CPAN that allows you to emulate LWP
135 requests and responses. Most of them are implemented as a mock library,
136 which means it doesn't go through the LWP guts and just gives you a
137 wrapper for receiving HTTP::Request and returning HTTP::Response back.
138
139 LWP::Protocol::PSGI is implemented as an LWP protocol handler and it
140 allows you to use most of the LWP extensions to add capabilities such
141 as manipulating headers and parsing cookies.
142
143 Test::LWP::UserAgent
144
145 Test::LWP::UserAgent has the similar concept of overriding LWP request
146 method with particular PSGI applications. It has more features and
147 options such as passing through the requests to the native LWP handler,
148 while LWP::Protocol::PSGI only allows one to map certain hosts and
149 ports.
150
151 Test::LWP::UserAgent requires you to change the instantiation of
152 UserAgent from LWP::UserAgent->new to Test::LWP::UserAgent->new somehow
153 and it's your responsibility to do so. This mechanism gives you more
154 control which requests should go through the PSGI app, and it might not
155 be difficult if the creation is done in one place in your code base.
156 However it might be hard or even impossible when you are dealing with
157 third party modules that calls LWP::UserAgent inside.
158
159 LWP::Protocol::PSGI affects the LWP calling code more globally, while
160 having an option to enable it only in a specific block, thus there's no
161 need to change the UserAgent object manually, whether it is in your
162 code or CPAN modules.
163
164 AUTHOR
165
166 Tatsuhiko Miyagawa <[email protected]>
167
168 COPYRIGHT
169
170 Copyright 2011- Tatsuhiko Miyagawa
171
172 LICENSE
173
174 This library is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
176
177 SEE ALSO
178
179 Plack::Client LWP::UserAgent
180