| 1 | |||
| 2 | package ASP4::UserAgent; | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Carp 'confess'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Cwd 'cwd'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use HTTP::Request::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use HTTP::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | ▶ | use ASP4::HTTPContext; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use ASP4::ConfigLoader; | ||
| 13 | use ASP4::SimpleCGI; | ||
| 14 | use ASP4::Mock::RequestRec; | ||
| 15 | |||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | return bless { | ||
| 20 | cwd => cwd(), | ||
| 21 | http_cookie => '', | ||
| 22 | cookies => { }, | ||
| 23 | referer => '', | ||
| 24 | }, shift; | ||
| 25 | }# end new() | ||
| 26 | |||
| 27 | sub context { ASP4::HTTPContext->current } | ||
| 28 | sub config { ASP4::ConfigLoader->load } | ||
| 29 | |||
| 30 | |||
| 31 | sub get | ||
| 32 | { | ||
| 33 | my ($s, $uri) = @_; | ||
| 34 | |||
| 35 | chdir( $s->{cwd} ); | ||
| 36 | undef($ASP4::HTTPContext::_instance); | ||
| 37 | my $req = GET $uri; | ||
| 38 | %ENV = ( | ||
| 39 | DOCUMENT_ROOT => $s->config->web->www_root, | ||
| 40 | REQUEST_METHOD => 'GET', | ||
| 41 | CONTENT_TYPE => 'application/x-www-form-urlencoded', | ||
| 42 | HTTP_COOKIE => $s->http_cookie, | ||
| 43 | ); | ||
| 44 | my $cgi = $s->_setup_cgi( $req ); | ||
| 45 | my ($uri_no_args, $querystring) = split /\?/, $uri; | ||
| 46 | my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring ); | ||
| 47 | $s->context->setup_request( $r, $cgi ); | ||
| 48 | return $s->_setup_response( $s->context->execute() ); | ||
| 49 | }# end get() | ||
| 50 | |||
| 51 | |||
| 52 | sub post | ||
| 53 | { | ||
| 54 | my ($s, $uri, $args) = @_; | ||
| 55 | |||
| 56 | chdir( $s->{cwd} ); | ||
| 57 | undef($ASP4::HTTPContext::_instance); | ||
| 58 | $args ||= [ ]; | ||
| 59 | my $req = POST $uri, $args; | ||
| 60 | %ENV = ( | ||
| 61 | DOCUMENT_ROOT => $s->config->web->www_root, | ||
| 62 | REQUEST_METHOD => 'POST', | ||
| 63 | CONTENT_TYPE => 'application/x-www-form-urlencoded', | ||
| 64 | HTTP_COOKIE => $s->http_cookie, | ||
| 65 | ); | ||
| 66 | my $cgi = $s->_setup_cgi( $req ); | ||
| 67 | my ($uri_no_args, $querystring) = split /\?/, $uri; | ||
| 68 | my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring ); | ||
| 69 | $s->context->setup_request( $r, $cgi ); | ||
| 70 | return $s->_setup_response( $s->context->execute() ); | ||
| 71 | }# end post() | ||
| 72 | |||
| 73 | |||
| 74 | sub upload | ||
| 75 | { | ||
| 76 | my ($s, $uri, $args) = @_; | ||
| 77 | |||
| 78 | chdir( $s->{cwd} ); | ||
| 79 | undef($ASP4::HTTPContext::_instance); | ||
| 80 | $args ||= [ ]; | ||
| 81 | my $req = POST $uri, Content_Type => 'form-data', Content => $args; | ||
| 82 | %ENV = ( | ||
| 83 | DOCUMENT_ROOT => $s->config->web->www_root, | ||
| 84 | REQUEST_METHOD => 'POST', | ||
| 85 | CONTENT_TYPE => 'multipart/form-data', | ||
| 86 | HTTP_COOKIE => $s->http_cookie, | ||
| 87 | ); | ||
| 88 | my $cgi = $s->_setup_cgi( $req ); | ||
| 89 | my ($uri_no_args, $querystring) = split /\?/, $uri; | ||
| 90 | my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring ); | ||
| 91 | $s->context->setup_request( $r, $cgi ); | ||
| 92 | return $s->_setup_response( $s->context->execute() ); | ||
| 93 | }# end upload() | ||
| 94 | |||
| 95 | |||
| 96 | sub submit_form | ||
| 97 | { | ||
| 98 | my ($s, $form) = @_; | ||
| 99 | |||
| 100 | chdir( $s->{cwd} ); | ||
| 101 | undef($ASP4::HTTPContext::_instance); | ||
| 102 | |||
| 103 | my $temp_referrer = $ENV{HTTP_REFERER}; | ||
| 104 | my $req = $form->click; | ||
| 105 | %ENV = ( | ||
| 106 | DOCUMENT_ROOT => $s->config->web->www_root, | ||
| 107 | REQUEST_METHOD => uc( $req->method ), | ||
| 108 | CONTENT_TYPE => $form->enctype ? $form->enctype : 'application/x-www-form-urlencoded', | ||
| 109 | HTTP_COOKIE => $s->http_cookie, | ||
| 110 | ); | ||
| 111 | my $cgi = $s->_setup_cgi( $req ); | ||
| 112 | my ($uri_no_args, $querystring) = split /\?/, $req->uri; | ||
| 113 | my $r = ASP4::Mock::RequestRec->new( uri => $uri_no_args, args => $querystring ); | ||
| 114 | $s->context->setup_request( $r, $cgi ); | ||
| 115 | return $s->_setup_response( $s->context->execute() ); | ||
| 116 | }# end submit_form() | ||
| 117 | |||
| 118 | |||
| 119 | sub add_cookie | ||
| 120 | { | ||
| 121 | my ($s, $name, $value) = @_; | ||
| 122 | |||
| 123 | $s->{cookies}->{$name} = $value; | ||
| 124 | }# end add_cookie() | ||
| 125 | |||
| 126 | |||
| 127 | sub remove_cookie | ||
| 128 | { | ||
| 129 | my ($s, $name) = @_; | ||
| 130 | |||
| 131 | delete( $s->{cookies}->{$name} ); | ||
| 132 | }# end remove_cookie() | ||
| 133 | |||
| 134 | |||
| 135 | sub http_cookie | ||
| 136 | { | ||
| 137 | my $s = shift; | ||
| 138 | |||
| 139 | join '; ', | ||
| 140 | map { ASP4::SimpleCGI->escape($_) . '=' . ASP4::SimpleCGI->escape($s->{cookies}->{$_}) } | ||
| 141 | keys %{$s->{cookies}}; | ||
| 142 | }# end http_cookie() | ||
| 143 | |||
| 144 | |||
| 145 | sub _setup_response | ||
| 146 | { | ||
| 147 | my ($s, $response_code) = @_; | ||
| 148 | |||
| 149 | $response_code = 200 if $response_code eq '0'; | ||
| 150 | my $response = HTTP::Response->new( $response_code ); | ||
| 151 | $response->content( $s->context->r->buffer ); | ||
| 152 | |||
| 153 | $response->header( 'Content-Type' => $s->context->response->{ContentType} ); | ||
| 154 | |||
| 155 | foreach my $header ( $s->context->response->Headers, $s->context->r->err_headers_out ) | ||
| 156 | { | ||
| 157 | while( my ($k,$v) = each(%$header) ) | ||
| 158 | { | ||
| 159 | $response->header( $k => $v ); | ||
| 160 | if( lc($k) eq 'set-cookie' ) | ||
| 161 | { | ||
| 162 | my ($data) = split /;/, $v; | ||
| 163 | my ($name,$val) = map { ASP4::SimpleCGI->unescape( $_ ) } split /\=/, $data; | ||
| 164 | $s->add_cookie( $name => $val ); | ||
| 165 | }# end if() | ||
| 166 | }# end while() | ||
| 167 | }# end foreach() | ||
| 168 | |||
| 169 | if( $s->context->session && $s->context->session->{SessionID} ) | ||
| 170 | { | ||
| 171 | $s->add_cookie( | ||
| 172 | $s->config->data_connections->session->cookie_name => $s->context->session->{SessionID} | ||
| 173 | ); | ||
| 174 | }# end if() | ||
| 175 | |||
| 176 | return $response; | ||
| 177 | }# end _setup_response() | ||
| 178 | |||
| 179 | |||
| 180 | sub _setup_cgi | ||
| 181 | { | ||
| 182 | my ($s, $req) = @_; | ||
| 183 | |||
| 184 | if( $s->{referer} ) | ||
| 185 | { | ||
| 186 | ($s->{referer}) = $req->uri =~ m/.*?(\/[^\?]+)/; | ||
| 187 | $req->referer( $s->{referer} ); | ||
| 188 | } | ||
| 189 | else | ||
| 190 | { | ||
| 191 | $req->referer(''); | ||
| 192 | }# end if() | ||
| 193 | |||
| 194 | no warnings 'uninitialized'; | ||
| 195 | (my ($uri_no_args), $ENV{QUERY_STRING} ) = split /\?/, $req->uri; | ||
| 196 | $ENV{SERVER_NAME} = $ENV{HTTP_HOST} = 'localhost'; | ||
| 197 | |||
| 198 | unless( $req->uri =~ m@^/handlers@ ) | ||
| 199 | { | ||
| 200 | $ENV{SCRIPT_FILENAME} = $s->config->web->www_root . $uri_no_args; | ||
| 201 | $ENV{SCRIPT_NAME} = $uri_no_args; | ||
| 202 | }# end unless() | ||
| 203 | |||
| 204 | # User-Agent: | ||
| 205 | $req->header( 'User-Agent' => 'test-useragent v1.0' ); | ||
| 206 | $ENV{HTTP_USER_AGENT} = 'test-useragent v2.0'; | ||
| 207 | |||
| 208 | # Cookies: | ||
| 209 | $req->header( 'Cookie' => $ENV{HTTP_COOKIE} = $s->http_cookie ); | ||
| 210 | |||
| 211 | if( $ENV{REQUEST_METHOD} =~ m/^post$/i ) | ||
| 212 | { | ||
| 213 | # Set up the basic params: | ||
| 214 | return ASP4::SimpleCGI->new( | ||
| 215 | querystring => $ENV{QUERY_STRING}, | ||
| 216 | body => $req->content, | ||
| 217 | content_type => $req->headers->{'content-type'}, | ||
| 218 | content_length => $req->headers->{'content-length'}, | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | else | ||
| 222 | { | ||
| 223 | # Simple 'GET' request: | ||
| 224 | return ASP4::SimpleCGI->new( querystring => $ENV{QUERY_STRING} ); | ||
| 225 | }# end if() | ||
| 226 | }# end _setup_cgi() | ||
| 227 | |||
| 228 | |||
| 229 | sub DESTROY | ||
| 230 | { | ||
| 231 | my $s = shift; | ||
| 232 | undef(%$s); | ||
| 233 | }# end DESTROY() | ||
| 234 | |||
| 235 | 1;# return true: | ||
| 236 | |||
| 237 | =pod | ||
| 238 | |||
| 239 | =head1 NAME | ||
| 240 | |||
| 241 | ASP4::UserAgent - Execute ASP4 requests without a web server. | ||
| 242 | |||
| 243 | =head1 SYNOPSIS | ||
| 244 | |||
| 245 | B<NOTE:> 99.99% of the time you will access this via L<ASP4::API>. | ||
| 246 | |||
| 247 | my HTTP::Response $res = $api->ua->get('/index.asp?foo=bar'); | ||
| 248 | |||
| 249 | my $res = $api->ua->post('/handlers/user.login', [ | ||
| 250 | username => 'willy', | ||
| 251 | password => 'wonka', | ||
| 252 | ]); | ||
| 253 | |||
| 254 | my $res = $api->ua->upload('/handlers/file.upload', [ | ||
| 255 | foo => 'bar', | ||
| 256 | baz => 'bux', | ||
| 257 | file => ['/home/john/avatar.jpg'] | ||
| 258 | ]); | ||
| 259 | |||
| 260 | # Some form testing: | ||
| 261 | my ($form) = HTML::Form->parse( $res->content, '/' ); | ||
| 262 | $form->find_input('username')->value('bob'); | ||
| 263 | my $res = $api->ua->submit_form( $form ); | ||
| 264 | |||
| 265 | # Add/remove a cookie: | ||
| 266 | $api->ua->add_cookie( "the-boss" => "me" ); | ||
| 267 | $api->remove_cookie( "the-boss" ); | ||
| 268 | |||
| 269 | =head1 DESCRIPTION | ||
| 270 | |||
| 271 | Enables unit-testing ASP4 applications by providing the ability to execuite web | ||
| 272 | pages from your code, without a webserver. | ||
| 273 | |||
| 274 | =head1 PUBLIC METHODS | ||
| 275 | |||
| 276 | =head2 get( $url ) | ||
| 277 | |||
| 278 | Calls C<$url> and returns the L<HTTP::Response> result. | ||
| 279 | |||
| 280 | =head2 post( $url, $args ) | ||
| 281 | |||
| 282 | Calls C<$url> with C<$args> and returns the L<HTTP::Response> result. | ||
| 283 | |||
| 284 | =head2 upload( $url, $args ) | ||
| 285 | |||
| 286 | Calls C<$url> with C<$args> and returns the L<HTTP::Response> result. | ||
| 287 | |||
| 288 | =head2 submit_form( HTML::Form $form ) | ||
| 289 | |||
| 290 | Submits the C<$form> and returns the L<HTTP::Response> result. | ||
| 291 | |||
| 292 | =head2 add_cookie( $name, $value ) | ||
| 293 | |||
| 294 | Adds the cookie to all subsequent requests. | ||
| 295 | |||
| 296 | =head2 remove_cookie( $name ) | ||
| 297 | |||
| 298 | Removes the cookie (if it exists). | ||
| 299 | |||
| 300 | =head1 SEE ALSO | ||
| 301 | |||
| 302 | L<HTTP::Response> and L<HTML::Form> | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||