Matt S Trout wrote:
> Erm. I was more thinking returning it after a prepare but with ->req 
> populated.
>
> The idea for me would be for unit testing controllers/views (and any model 
> code
> that uses ACCEPT_CONTEXT).
>
> Of course, if you wanted you could then call ->dispatch and then ->finalize
> yourself, but once you're getting as far as that you're probably better using
> the standard stuff (plus replacing the view with a mock one via
> $c->_components->{MyApp::View::Foo} = $test_view to handle testing just the
> controller ops and stash contents)
>   

Fair enough. Sounds like a plan. Looks like it would be almost the same
as Catalyst::Test::request but calling prepare() instead of handle_request.

I've made a quick patch, minimally tested.

Matt

--- lib/Catalyst/Test.pm	2007/05/18 08:57:16	1.1
+++ lib/Catalyst/Test.pm	2007/05/18 09:18:22
@@ -100,25 +100,51 @@
     my $caller = caller(0);
     *{"$caller\::request"} = $request;
     *{"$caller\::get"}     = $get;
+
+    # Context is always local
+    *{"$caller\::context"} = sub { context($class, @_) };
 }
 
 =head2 local_request
 
 =cut
 
-sub local_request {
-    my $class = shift;
-
+# Set up a CGI environment
+sub _setup_env {
     require HTTP::Request::AsCGI;
 
     my $request = Catalyst::Utils::request( shift(@_) );
-    my $cgi     = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
+    return HTTP::Request::AsCGI->new( $request, %ENV )->setup;
+}
+
+sub local_request {
+    my $class = shift;
+
+    my $cgi = _setup_env(@_);
 
     $class->handle_request;
 
     return $cgi->restore->response;
 }
 
+=head2 context
+
+    $c = context($url)
+
+Returns a context object based on the given URL. This is always local.
+
+=cut
+
+sub context {
+    my $class = shift;
+
+    my $cgi = _setup_env(@_);
+
+    return $class->prepare;
+
+    # environment restored by $cgi going out of scope here
+}
+
 my $agent;
 
 =head2 remote_request
_______________________________________________
List: [email protected]
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to