From: Matt S Trout Date: Wed, 12 Jan 2011 21:09:11 +0000 (+0000) Subject: cleanup test code and support non-GET requests X-Git-Tag: release_0.006~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=4ba6d891dd8c3b7ea92540466329b40de2e6ff80 cleanup test code and support non-GET requests --- diff --git a/lib/Web/Simple/Application.pm b/lib/Web/Simple/Application.pm index 92fbca0..2782c7c 100644 --- a/lib/Web/Simple/Application.pm +++ b/lib/Web/Simple/Application.pm @@ -67,19 +67,39 @@ sub run { } elsif ($ENV{GATEWAY_INTERFACE}) { return $self->_run_cgi; } - unless (@ARGV && $ARGV[0] =~ m{^/}) { + unless (@ARGV && $ARGV[0] =~ m{^[A-Z/]}) { return $self->_run_cli(@ARGV); } - my $path = shift @ARGV; + my @args = @ARGV; - require HTTP::Request::Common; + unshift(@args, 'GET') if $args[0] =~ m{^/}; + + $self->_run_test_request(@args); +} + +sub _run_test_request { + my ($self, $method, $path, @rest) = @_; + + require HTTP::Request; require Plack::Test; - local *GET = \&HTTP::Request::Common::GET; - my $request = GET($path); + my $request = HTTP::Request->new($method => $path); + if ($method eq 'POST' or $method eq 'PUT' and @rest) { + my $content = do { + require URI; + my $url = URI->new('http:'); + $url->query_form(@rest); + $url->query; + }; + $request->header('Content-Type' => 'application/x-www-form-urlencoded'); + $request->header('Content-Length' => length($content)); + $request->content($content); + } my $response; - Plack::Test::test_psgi($self->to_psgi_app, sub { $response = shift->($request) }); + Plack::Test::test_psgi( + $self->to_psgi_app, sub { $response = shift->($request) } + ); print $response->as_string; }