From: Christian Hansen Date: Fri, 25 Mar 2005 19:57:41 +0000 (+0000) Subject: move C::E::LWP to C::E::Test and make C::E::H::D a subclass of C::E::T X-Git-Tag: 5.7099_04~1682 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=75fd617a0395f9a49cd215c17bb21f4fadf14db4 move C::E::LWP to C::E::Test and make C::E::H::D a subclass of C::E::T --- diff --git a/lib/Catalyst/Engine/HTTP/Daemon.pm b/lib/Catalyst/Engine/HTTP/Daemon.pm index 65129ee..6d90e39 100644 --- a/lib/Catalyst/Engine/HTTP/Daemon.pm +++ b/lib/Catalyst/Engine/HTTP/Daemon.pm @@ -1,7 +1,7 @@ package Catalyst::Engine::HTTP::Daemon; use strict; -use base 'Catalyst::Engine::LWP'; +use base 'Catalyst::Engine::Test'; use IO::Socket qw(AF_INET); @@ -31,7 +31,7 @@ This is the Catalyst engine specialized for development and testing. =head1 OVERLOADED METHODS -This class overloads some methods from C. +This class overloads some methods from C. =over 4 @@ -62,7 +62,7 @@ sub run { $request->uri->scheme('http'); # Force URI::http - my $lwp = Catalyst::Engine::LWP::HTTP->new( + my $lwp = Catalyst::Engine::Test::LWP->new( request => $request, address => $connection->peerhost, hostname => gethostbyaddr( $connection->peeraddr, AF_INET ) diff --git a/lib/Catalyst/Engine/LWP.pm b/lib/Catalyst/Engine/LWP.pm deleted file mode 100644 index ae24063..0000000 --- a/lib/Catalyst/Engine/LWP.pm +++ /dev/null @@ -1,233 +0,0 @@ -package Catalyst::Engine::LWP; - -use strict; -use base 'Catalyst::Engine'; - -use CGI::Simple::Cookie; -use Class::Struct (); -use HTTP::Headers::Util 'split_header_words'; -use HTTP::Request; -use HTTP::Response; -use IO::File; -use URI; - -__PACKAGE__->mk_accessors(qw/lwp/); - -Class::Struct::struct 'Catalyst::Engine::LWP::HTTP' => { - request => 'HTTP::Request', - response => 'HTTP::Response', - hostname => '$', - address => '$' -}; - -=head1 NAME - -Catalyst::Engine::LWP - Catalyst LWP Engine - -=head1 SYNOPSIS - -L. - -=head1 DESCRIPTION - -This Catalyst engine is meant to be subclassed. - -=head1 OVERLOADED METHODS - -This class overloads some methods from C. - -=over 4 - -=item $c->finalize_headers - -=cut - -sub finalize_headers { - my $c = shift; - - my $status = $c->response->status || 200; - my $headers = $c->response->headers; - my $response = HTTP::Response->new( $status, undef, $headers ); - - while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { - my $cookie = CGI::Simple::Cookie->new( - -name => $name, - -value => $cookie->{value}, - -expires => $cookie->{expires}, - -domain => $cookie->{domain}, - -path => $cookie->{path}, - -secure => $cookie->{secure} || 0 - ); - - $response->header( 'Set-Cookie' => $cookie->as_string ); - } - - $c->lwp->response($response); -} - -=item $c->finalize_output - -=cut - -sub finalize_output { - my $c = shift; - $c->lwp->response->content_ref( \$c->response->{output} ); -} - -=item $c->prepare_connection - -=cut - -sub prepare_connection { - my $c = shift; - $c->req->hostname( $c->lwp->hostname ); - $c->req->address( $c->lwp->address ); -} - -=item $c->prepare_cookies - -=cut - -sub prepare_cookies { - my $c = shift; - - if ( my $header = $c->request->headers->header('Cookie') ) { - $c->req->cookies( { CGI::Simple::Cookie->parse($header) } ); - } -} - -=item $c->prepare_headers - -=cut - -sub prepare_headers { - my $c = shift; - $c->req->method( $c->lwp->request->method ); - $c->req->headers( $c->lwp->request->headers ); -} - -=item $c->prepare_parameters - -=cut - -sub prepare_parameters { - my $c = shift; - - my @params = (); - my $request = $c->lwp->request; - - push( @params, $request->uri->query_form ); - - if ( $request->content_type eq 'application/x-www-form-urlencoded' ) { - my $uri = URI->new('http:'); - $uri->query( $request->content ); - push( @params, $uri->query_form ); - } - - if ( $request->content_type eq 'multipart/form-data' ) { - - for my $part ( $request->parts ) { - - my $disposition = $part->header('Content-Disposition'); - my %parameters = @{ ( split_header_words($disposition) )[0] }; - - if ( $parameters{filename} ) { - - my $fh = IO::File->new_tmpfile; - $fh->write( $part->content ) or die $!; - $fh->seek( SEEK_SET, 0 ) or die $!; - - $c->req->uploads->{ $parameters{filename} } = { - fh => $fh, - size => ( stat $fh )[7], - type => $part->content_type - }; - - push( @params, $parameters{filename}, $fh ); - } - else { - push( @params, $parameters{name}, $part->content ); - } - } - } - - my $parameters = $c->req->parameters; - - while ( my ( $name, $value ) = splice( @params, 0, 2 ) ) { - - if ( exists $parameters->{$name} ) { - for ( $parameters->{$name} ) { - $_ = [$_] unless ref($_) eq "ARRAY"; - push( @$_, $value ); - } - } - else { - $parameters->{$name} = $value; - } - } -} - -=item $c->prepare_path - -=cut - -sub prepare_path { - my $c = shift; - - my $base; - { - my $scheme = $c->lwp->request->uri->scheme; - my $host = $c->lwp->request->uri->host; - my $port = $c->lwp->request->uri->port; - - $base = URI->new; - $base->scheme($scheme); - $base->host($host); - $base->port($port); - - $base = $base->canonical->as_string; - } - - my $path = $c->lwp->request->uri->path || '/'; - $path =~ s/^\///; - - $c->req->base($base); - $c->req->path($path); -} - -=item $c->prepare_request($r) - -=cut - -sub prepare_request { - my ( $c, $lwp ) = @_; - $c->lwp($lwp); -} - -=item $c->prepare_uploads - -=cut - -sub prepare_uploads { - my $c = shift; -} - -=back - -=head1 SEE ALSO - -L. - -=head1 AUTHOR - -Sebastian Riedel, C -Christian Hansen, C - -=head1 COPYRIGHT - -This program is free software, you can redistribute it and/or modify it under -the same terms as Perl itself. - -=cut - -1; diff --git a/lib/Catalyst/Engine/Test.pm b/lib/Catalyst/Engine/Test.pm index fcd145e..59a6246 100644 --- a/lib/Catalyst/Engine/Test.pm +++ b/lib/Catalyst/Engine/Test.pm @@ -1,7 +1,24 @@ package Catalyst::Engine::Test; use strict; -use base 'Catalyst::Engine::LWP'; +use base 'Catalyst::Engine'; + +use CGI::Simple::Cookie; +use Class::Struct (); +use HTTP::Headers::Util 'split_header_words'; +use HTTP::Request; +use HTTP::Response; +use IO::File; +use URI; + +__PACKAGE__->mk_accessors(qw/lwp/); + +Class::Struct::struct 'Catalyst::Engine::Test::LWP' => { + request => 'HTTP::Request', + response => 'HTTP::Response', + hostname => '$', + address => '$' +}; =head1 NAME @@ -29,10 +46,184 @@ This is the Catalyst engine specialized for testing. =head1 OVERLOADED METHODS -This class overloads some methods from C. +This class overloads some methods from C. =over 4 +=item $c->finalize_headers + +=cut + +sub finalize_headers { + my $c = shift; + + my $status = $c->response->status || 200; + my $headers = $c->response->headers; + my $response = HTTP::Response->new( $status, undef, $headers ); + + while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) { + my $cookie = CGI::Simple::Cookie->new( + -name => $name, + -value => $cookie->{value}, + -expires => $cookie->{expires}, + -domain => $cookie->{domain}, + -path => $cookie->{path}, + -secure => $cookie->{secure} || 0 + ); + + $response->header( 'Set-Cookie' => $cookie->as_string ); + } + + $c->lwp->response($response); +} + +=item $c->finalize_output + +=cut + +sub finalize_output { + my $c = shift; + $c->lwp->response->content_ref( \$c->response->{output} ); +} + +=item $c->prepare_connection + +=cut + +sub prepare_connection { + my $c = shift; + $c->req->hostname( $c->lwp->hostname ); + $c->req->address( $c->lwp->address ); +} + +=item $c->prepare_cookies + +=cut + +sub prepare_cookies { + my $c = shift; + + if ( my $header = $c->request->headers->header('Cookie') ) { + $c->req->cookies( { CGI::Simple::Cookie->parse($header) } ); + } +} + +=item $c->prepare_headers + +=cut + +sub prepare_headers { + my $c = shift; + $c->req->method( $c->lwp->request->method ); + $c->req->headers( $c->lwp->request->headers ); +} + +=item $c->prepare_parameters + +=cut + +sub prepare_parameters { + my $c = shift; + + my @params = (); + my $request = $c->lwp->request; + + push( @params, $request->uri->query_form ); + + if ( $request->content_type eq 'application/x-www-form-urlencoded' ) { + my $uri = URI->new('http:'); + $uri->query( $request->content ); + push( @params, $uri->query_form ); + } + + if ( $request->content_type eq 'multipart/form-data' ) { + + for my $part ( $request->parts ) { + + my $disposition = $part->header('Content-Disposition'); + my %parameters = @{ ( split_header_words($disposition) )[0] }; + + if ( $parameters{filename} ) { + + my $fh = IO::File->new_tmpfile; + $fh->write( $part->content ) or die $!; + $fh->seek( SEEK_SET, 0 ) or die $!; + + $c->req->uploads->{ $parameters{filename} } = { + fh => $fh, + size => ( stat $fh )[7], + type => $part->content_type + }; + + push( @params, $parameters{filename}, $fh ); + } + else { + push( @params, $parameters{name}, $part->content ); + } + } + } + + my $parameters = $c->req->parameters; + + while ( my ( $name, $value ) = splice( @params, 0, 2 ) ) { + + if ( exists $parameters->{$name} ) { + for ( $parameters->{$name} ) { + $_ = [$_] unless ref($_) eq "ARRAY"; + push( @$_, $value ); + } + } + else { + $parameters->{$name} = $value; + } + } +} + +=item $c->prepare_path + +=cut + +sub prepare_path { + my $c = shift; + + my $base; + { + my $scheme = $c->lwp->request->uri->scheme; + my $host = $c->lwp->request->uri->host; + my $port = $c->lwp->request->uri->port; + + $base = URI->new; + $base->scheme($scheme); + $base->host($host); + $base->port($port); + + $base = $base->canonical->as_string; + } + + my $path = $c->lwp->request->uri->path || '/'; + $path =~ s/^\///; + + $c->req->base($base); + $c->req->path($path); +} + +=item $c->prepare_request($r) + +=cut + +sub prepare_request { + my ( $c, $lwp ) = @_; + $c->lwp($lwp); +} + +=item $c->prepare_uploads + +=cut + +sub prepare_uploads { + my $c = shift; +} + =item $c->run =cut @@ -54,7 +245,7 @@ sub run { $request = HTTP::Request->new( 'GET', $request ); } - my $lwp = Catalyst::Engine::LWP::HTTP->new( + my $lwp = Catalyst::Engine::Test::LWP->new( request => $request, address => '127.0.0.1', hostname => 'localhost' diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index 4ca73ee..fc29985 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -96,7 +96,7 @@ sub remote_request { require LWP::UserAgent; - my $remote = URI->new( $ENV{CATALYST_SERVER} ); + my $server = URI->new( $ENV{CATALYST_SERVER} ); unless ( ref $request ) { @@ -108,9 +108,9 @@ sub remote_request { $request = $uri->canonical; } - $request->scheme( $remote->scheme ); - $request->host( $remote->host ); - $request->port( $remote->port ); + $request->scheme( $server->scheme ); + $request->host( $server->host ); + $request->port( $server->port ); unless ( ref $request eq 'HTTP::Request' ) { $request = HTTP::Request->new( 'GET', $request );