X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FTest.pm;h=d0977a3769406cbc91bf62b150cde3faa798ea71;hp=f98717246d063de19fe8121dea87f5f45c07a370;hb=82e80cb1a3d0246b2e4ebc07f23599aadf9e8516;hpb=955d6da66d0a5c2f770d68d9b9e5b1bd22b21750 diff --git a/lib/Catalyst/Test.pm b/lib/Catalyst/Test.pm index f987172..d0977a3 100644 --- a/lib/Catalyst/Test.pm +++ b/lib/Catalyst/Test.pm @@ -44,7 +44,7 @@ my $build_exports = sub { ### place holder for $c after the request finishes; reset every time ### requests are done. - my $c; + my $ctx_closed_over; ### hook into 'dispatch' -- the function gets called after all plugins ### have done their work, and it's an easy place to capture $c. @@ -52,7 +52,7 @@ my $build_exports = sub { my $meta = Class::MOP::get_metaclass_by_name($class); $meta->make_mutable; $meta->add_after_method_modifier( "dispatch", sub { - $c = shift; + $ctx_closed_over = shift; }); $meta->make_immutable( replace_constructor => 1 ); Class::C3::reinitialize(); # Fixes RT#46459, I've failed to write a test for how/why, but it does. @@ -60,8 +60,18 @@ my $build_exports = sub { ### we've already stopped it from doing remote requests above. my $res = $request->( @_ ); + # Make sure not to leave a reference $ctx hanging around. + # This means that the context will go out of scope as soon as the + # caller disposes of it, rather than waiting till the next time + # that ctx_request is called. This can be important if your $ctx + # ends up with a reference to a shared resource or lock (for example) + # which you want to clean up in test teardown - if the $ctx is still + # closed over then you're stuffed... + my $ctx = $ctx_closed_over; + undef $ctx_closed_over; + ### return both values - return ( $res, $c ); + return ( $res, $ctx ); }; return { @@ -202,6 +212,9 @@ method and the L method below: is ( $uri->path , '/y'); my $content = get($uri->path); +Note also that the content is returned as raw bytes, without any attempt +to decode it into characters. + =head2 $res = request( ... ); Returns an L object. Accepts an optional hashref for request @@ -236,6 +249,21 @@ sub local_request { my $response = $cgi->restore->response; $response->request( $request ); + + # HTML head parsing based on LWP::UserAgent + + require HTML::HeadParser; + + my $parser = HTML::HeadParser->new(); + $parser->xml_mode(1) if $response->content_is_xhtml; + $parser->utf8_mode(1) if $] >= 5.008 && $HTML::Parser::VERSION >= 3.40; + + $parser->parse( $response->content ); + my $h = $parser->header; + for my $f ( $h->header_field_names ) { + $response->init_header( $f, [ $h->header($f) ] ); + } + return $response; }