Hi How to you approach testing ajax calls? If I had: use Dancer::Plugin::Ajax; ajax '/somedata.json' => sub { return { data => "hello" }; }; The tests: route_exists [ 'GET' => '/somedata.json'], 'a route handler is defined for /somedata'; response_status_is ['GET' => '/somedata.json'], 200, 'response status is 200 for /somedata'; give: 1..2 ok 1 not ok 2 - response status is 200 for / # Failed test 'response status is 200 for /' # at t/003_api_route.t line 10. # got: '404' # expected: '200' # Looks like you failed 1 test of 2. So how do I construct the response_status_is to be: X-Requested-With: XMLHttpRequest And secondly the route_exists call would also match: get '/somedata.json' => sub { return { data => "hello" }; }; so how do I distinguish between a GET and an AJAX call with a route_exists? Thanks for any pointers.
Dancer::Request::new_for_request does not handle ajax request correctly. Below test is failed. ------------------------------------------------------------------------------------------ use Test::More; use Dancer ':syntax'; use Dancer::Plugin::Ajax; ajax '/req' => sub { return "hello"; }; use Dancer::Test; my $response = dancer_response GET => '/req', {headers => [ "x-requested-with" => "XMLHttpRequest", ] }; is $response->{content}, "hello", "response content looks good for AJAX GET /req"; $response = dancer_response POST => '/req', {headers => [ "x-requested-with" => "XMLHttpRequest", ] }; is $response->{content}, "hello", "response content looks good for AJAX GET /req"; done_testing(); ------------------------------------------------------------------------------------------ This error is fixed by below patch. But It do not seem to me proper fix. --- Request.pm.orig 2011-07-17 20:38:30.392932771 +0900 +++ Request.pm 2011-07-17 20:38:15.845590732 +0900 @@ -147,6 +147,7 @@ $req->{_query_params} = $req->{params}; $req->{body} = $body if defined $body; $req->{headers} = $headers if $headers; + $req->{ajax} = $req->is_ajax; return $req; } Best regards. -- Takeshi OKURA
participants (3)
-
sawyer x -
Stephen Fenwick-Paul -
Takeshi OKURA