[dw-free] Need a way to cleanly call controllers from tests
[commit: http://hg.dwscoalition.org/dw-free/rev/c6ee06c056d3]
http://bugs.dwscoalition.org/show_bug.cgi?id=3547
Create LJ::Test::routing_request, used in tests of controllers under the
routing system.
Patch by
exor674.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3547
Create LJ::Test::routing_request, used in tests of controllers under the
routing system.
Patch by
Files modified:
- cgi-bin/LJ/Test.pm
- t/atom-post.t
--------------------------------------------------------------------------------
diff -r b0d94d3528e4 -r c6ee06c056d3 cgi-bin/LJ/Test.pm
--- a/cgi-bin/LJ/Test.pm Mon Feb 28 18:47:21 2011 +0800
+++ b/cgi-bin/LJ/Test.pm Mon Feb 28 18:50:22 2011 +0800
@@ -16,6 +16,10 @@ use strict;
use strict;
use Carp qw(croak);
+use DW::Routing;
+use DW::Request::Standard;
+use HTTP::Request;
+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# WARNING! PELIGROSO! ACHTUNG! VNIMANIYE!
# some fools (aka mischa) try to use this library from web context,
@@ -26,7 +30,7 @@ use DBI;
use DBI;
use LJ::ModuleCheck;
our @ISA = qw(Exporter);
-our @EXPORT = qw(memcache_stress with_fake_memcache temp_user temp_comm temp_feed fake_apache);
+our @EXPORT = qw(memcache_stress with_fake_memcache temp_user temp_comm temp_feed fake_apache routing_request);
my @temp_userids; # to be destroyed later
END {
@@ -191,6 +195,30 @@ sub memcache_stress (&) {
LJ::MemCache::set_memcache($pre_mem);
}
+sub routing_request {
+ my ( $uri, %opts ) = @_;
+
+ my $method = $opts{method} || 'GET';
+ my %routing_data = %{ $opts{routing_data} || {} };
+
+ LJ::start_request();
+
+ my $req = HTTP::Request->new( $method => $uri );
+
+ $opts{setup_http_request}->($req) if $opts{setup_http_request};
+
+ # Just in case, but this shouldn't get set in a non-web context
+ DW::Request->reset;
+ my $r = DW::Request::Standard->new( $req );
+
+ $opts{setup_dw_request}->($r) if $opts{setup_dw_request};
+
+ my $rv = DW::Routing->call( %routing_data );
+ $r->status( $rv ) unless $rv eq $r->OK;
+
+ return $r;
+}
+
package LJ::Test::FakeMemCache;
# duck-typing at its finest!
# this is a fake Cache::Memcached object which implements the
@@ -283,7 +311,6 @@ sub disconnect_all {}
sub disconnect_all {}
sub forget_dead_hosts {}
-
package LJ::User;
# post a fake entry in a community journal
diff -r b0d94d3528e4 -r c6ee06c056d3 t/atom-post.t
--- a/t/atom-post.t Mon Feb 28 18:47:21 2011 +0800
+++ b/t/atom-post.t Mon Feb 28 18:50:22 2011 +0800
@@ -30,43 +30,33 @@ my $api = XML::Atom::Client->new( Versio
my $r;
sub do_request {
- my ( %opts ) = @_;
+ my ( $method, $uri, %opts ) = @_;
my $authenticate = delete $opts{authenticate};
my $data = delete $opts{data} || {};
my $remote = delete $opts{remote} || $u;
my $password = delete $opts{password} || $remote->password;
- my $req = HTTP::Request->new( %opts );
-
- $req->uri =~ m!http://([^.]+)!;
+ $uri =~ m!http://([^.]+)!;
my $user_subdomain = $1 eq "www" ? "" : $1;
-
- # caution: may be fragile
- # relies upon knowing details of the client's implementation
- # which are not in the client's public documented API
- if ( $authenticate ) {
- $api->username( $remote->username );
- $api->password( $password );
- $api->munge_request( $req );
- }
-
- # clear request caches
- DW::Request->reset;
- $r = $DW::Request::cur_req = DW::Request::Standard->new( $req );
- $DW::Request::determined = 1;
-
- LJ::Entry->reset_singletons;
- %LJ::REQ_CACHE_REL = ();
-
- # set any additional information
- $r->pnote( $_ => $data->{$_} ) foreach %$data;
my %routing_data = ();
$routing_data{username} = $user_subdomain if $user_subdomain;
- my $returned = DW::Routing->call( %routing_data );
- $r->status( $returned ) unless $returned eq $r->OK;
+ $r = routing_request( $uri,
+ method => $method,
+ setup_http_request => sub {
+ if ( $authenticate ) {
+ $api->username( $remote->username );
+ $api->password( $password );
+ $api->munge_request( $_[0] );
+ }
+ },
+ setup_dw_request => sub {
+ $_[0]->pnote( $_ => $data->{$_} ) foreach %$data;
+ },
+ routing_data => \%routing_data,
+ );
}
# check subject, etc
--------------------------------------------------------------------------------
