Skip to content

Commit

Permalink
Allow dancer_response to accept paramters from the query string
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rideout authored and Alexis Sukrieh committed Mar 23, 2012
1 parent f4bef86 commit fb3e25f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/Dancer/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,18 @@ sub to_string {
# helper for building a request object by hand
# with the given method, path, params, body and headers.
sub new_for_request {
my ($class, $method, $path, $params, $body, $headers) = @_;
my ($class, $method, $uri, $params, $body, $headers) = @_;
$params ||= {};
$method = uc($method);

my ( $path, $query_string ) = ( $uri =~ /([^?]*)(?:\?(.*))?/s ); #from HTTP::Server::Simple

my $req = $class->new(env => { %ENV,
PATH_INFO => $path,
QUERY_STRING => $query_string || $ENV{QUERY_STRING} || '',
REQUEST_METHOD => $method});
$req->{params} = {%{$req->{params}}, %{$params}};
$req->_build_params();
$req->{_query_params} = $req->{params};
$req->{body} = $body if defined $body;
$req->{headers} = $headers if $headers;
Expand Down
5 changes: 4 additions & 1 deletion t/02_request/04_custom.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Test::More tests => 7;
use Test::More tests => 8;

use strict;
use warnings FATAL => 'all';
Expand Down Expand Up @@ -28,3 +28,6 @@ $req = Dancer::Request->new_for_request('GET', '/stuff', {user => 'sukria'});
is_deeply scalar($req->params), {foo => 'bar', number => 42, user => 'sukria'},
'params are updated';

$req = Dancer::Request->new_for_request('GET', '/stuff?foo=baz&number=24');
is_deeply scalar($req->params), {foo => 'baz', number => 24},
'query string replace';
7 changes: 5 additions & 2 deletions t/23_dancer_tests/02_tests_functions.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use warnings;

use Test::More;

plan tests => 29;
plan tests => 30;

use Dancer qw/ :syntax :tests /;
use Dancer::Test;
Expand Down Expand Up @@ -91,7 +91,10 @@ is_deeply $r->{content}, { user => { id => 2, name => "Franck Cuny" } },
"id is correctly increased";

$r = dancer_response( GET => '/query', { params => {foo => 'bar'}});
is $r->{content} => "foo:bar";
is $r->{content}, "foo:bar", 'passed fake query params';

$r = dancer_response( GET => '/query?foo=bar' );
is $r->{content}, "foo:bar", 'passed params in query';

my $data = "She sells sea shells by the sea shore";
$r = dancer_response(
Expand Down

0 comments on commit fb3e25f

Please sign in to comment.