Skip to content

Commit

Permalink
added is_xhr method to Mojo::Message::Request
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 10, 2010
1 parent 13cf7f0 commit 2d2a48a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -11,6 +11,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added finished callback support for HTTP transactions.
- Added relative path support to Mojo::URL. (marcus)
- Added simple iterator support to Mojo::DOM.
- Added is_xhr method to Mojo::Message::Request.
- Improved Mojo::Client error logging.
- Improved Mojo::Template error messages.
- Improved generated multipart messages to be 2 bytes shorter.
Expand Down
19 changes: 19 additions & 0 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -98,6 +98,19 @@ sub is_secure {
return;
}

sub is_xhr {
my $self = shift;

# No ajax
return unless my $with = $self->headers->header('X-Requested-With');

# Ajax
return 1 if $with =~ /XMLHttpRequest/i;

# No ajax
return;
}

sub param {
my $self = shift;
$self->{_params} = $self->params unless $self->{_params};
Expand Down Expand Up @@ -487,6 +500,12 @@ Make sure message has all required headers for the current HTTP version.
Check if connection is secure.
=head2 C<is_xhr>
my $xhr = $req->is_xhr;
Check C<X-Requested-With> header for C<XMLHttpRequest> value.
=head2 C<param>
my $param = $req->param('foo');
Expand Down
19 changes: 18 additions & 1 deletion t/mojolicious/lite_app.t
Expand Up @@ -16,7 +16,7 @@ use Test::More;
# Make sure sockets are working
plan skip_all => 'working sockets required for this test!'
unless Mojo::IOLoop->new->generate_port;
plan tests => 419;
plan tests => 429;

# Pollution
123 =~ m/(\d+)/;
Expand Down Expand Up @@ -58,6 +58,13 @@ get '/null/:null' => sub {
$self->render(text => $self->param('null'), layout => 'layout');
};

# GET /maybe/ajax
get '/maybe/ajax' => sub {
my $self = shift;
return $self->render(text => 'is ajax') if $self->req->is_xhr;
$self->render(text => 'not ajax');
};

# GET /stream
get '/stream' => sub {
my $self = shift;
Expand Down Expand Up @@ -487,6 +494,16 @@ $t->get_ok("http://sri:foo\@localhost:$port/stream")->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_like(qr/^foobarsri\:foohttp:\/\/localhost\:\d+\/stream$/);

# GET /maybe/ajax (not ajax)
$t->get_ok('/maybe/ajax')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('not ajax');

# GET /maybe/ajax (is ajax)
$t->get_ok('/maybe/ajax', {'X-Requested-With' => 'XMLHttpRequest'})
->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')->content_is('is ajax');

# GET /finished (with finished callback)
$t->get_ok('/finished')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
Expand Down

0 comments on commit 2d2a48a

Please sign in to comment.