Skip to content

Commit

Permalink
added OPTIONS support
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 16, 2012
1 parent a2a5aea commit a05148a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Changes
@@ -1,6 +1,11 @@
This file documents the revision history for Perl extension Mojolicious.

2.62 2012-03-16 00:00:00
- Added options function to Mojolicious::Lite.
- Added options method to Mojolicious::Routes.
- Added options method to Mojo::UserAgent.
- Added options_ok method to Test::Mojo.
- Added o function to ojo.
- Removed experimental status from Mojo::Cache.
- Removed experimental status from Mojo::DOM::CSS.
- Removed experimental status from Mojo::DOM::HTML.
Expand Down
18 changes: 17 additions & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -30,7 +30,7 @@ has transactor => sub { Mojo::UserAgent::Transactor->new };
# Common HTTP methods
{
no strict 'refs';
for my $name (qw/DELETE GET HEAD PATCH POST PUT/) {
for my $name (qw/DELETE GET HEAD OPTIONS PATCH POST PUT/) {
*{__PACKAGE__ . '::' . lc($name)} = sub {
my $self = shift;
$self->start($self->build_tx($name, @_));
Expand Down Expand Up @@ -917,6 +917,22 @@ append a callback to perform requests non-blocking.
Check if request for domain would use a proxy server.
=head2 C<options>
my $tx = $ua->options('http://kraih.com');
Perform blocking HTTP C<OPTIONS> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the exact same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->options('http://kraih.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Mojo::IOLoop->stop;
});
Mojo::IOLoop->start;
=head2 C<patch>
my $tx = $ua->patch('http://kraih.com');
Expand Down
9 changes: 8 additions & 1 deletion lib/Mojolicious/Lite.pm
Expand Up @@ -35,7 +35,7 @@ sub import {
# Export
no warnings 'redefine';
my $root = $routes;
for my $name (qw/any get patch post put websocket/) {
for my $name (qw/any get options patch post put websocket/) {
*{"${caller}::$name"} = sub { $routes->$name(@_) };
}
*{"${caller}::new"} = *{"${caller}::app"} = sub {$app};
Expand Down Expand Up @@ -855,6 +855,13 @@ Alias for L<Mojolicious/"helper">.
Alias for L<Mojolicious/"hook">.
=head2 C<options>
my $route = options '/:foo' => sub {...};
Generate route matching only C<OPTIONS> requests. See also the tutorial above
for more argument variations.
=head2 C<patch>
my $route = patch '/:foo' => sub {...};
Expand Down
9 changes: 9 additions & 0 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -166,6 +166,8 @@ sub name {
return $self->{name};
}

sub options { shift->_generate_route(options => @_) }

sub over {
my $self = shift;
my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
Expand Down Expand Up @@ -775,6 +777,13 @@ The name of this route, defaults to an automatically generated name based on
the route pattern. Note that the name C<current> is reserved for refering to
the current route.
=head2 C<options>
my $route = $route->options('/:foo' => sub {...});
Generate route matching only C<OPTIONS> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
=head2 C<over>
$r = $r->over(foo => qr/\w+/);
Expand Down
12 changes: 10 additions & 2 deletions lib/Test/Mojo.pm
Expand Up @@ -238,8 +238,9 @@ sub message_unlike {
}

# "God bless those pagans."
sub patch_ok { shift->_request_ok(patch => @_) }
sub post_ok { shift->_request_ok(post => @_) }
sub options_ok { shift->_request_ok(options => @_) }
sub patch_ok { shift->_request_ok(patch => @_) }
sub post_ok { shift->_request_ok(post => @_) }

sub post_form_ok {
my ($self, $url) = (shift, shift);
Expand Down Expand Up @@ -639,6 +640,13 @@ Check WebSocket message for similar match.
Opposite of C<message_like>.
=head2 C<options_ok>
$t = $t->options_ok('/foo');
Perform a C<OPTIONS> request and check for transport errors, takes the exact
same arguments as L<Mojo::UserAgent/"options">.
=head2 C<patch_ok>
$t = $t->patch_ok('/foo');
Expand Down
22 changes: 15 additions & 7 deletions lib/ojo.pm
Expand Up @@ -41,13 +41,14 @@ sub import {
*{"${caller}::oO"} = sub { _request(@_) };
*{"${caller}::a"} =
sub { *{"${caller}::any"}->(@_) and return *{"${caller}::app"}->() };
*{"${caller}::d"} = sub { _request(DELETE => @_) };
*{"${caller}::f"} = sub { _request(FORM => @_) };
*{"${caller}::g"} = sub { _request(GET => @_) };
*{"${caller}::h"} = sub { _request(HEAD => @_) };
*{"${caller}::p"} = sub { _request(POST => @_) };
*{"${caller}::t"} = sub { _request(PATCH => @_) };
*{"${caller}::u"} = sub { _request(PUT => @_) };
*{"${caller}::d"} = sub { _request(DELETE => @_) };
*{"${caller}::f"} = sub { _request(FORM => @_) };
*{"${caller}::g"} = sub { _request(GET => @_) };
*{"${caller}::h"} = sub { _request(HEAD => @_) };
*{"${caller}::o"} = sub { _request(OPTIONS => @_) };
*{"${caller}::p"} = sub { _request(POST => @_) };
*{"${caller}::t"} = sub { _request(PATCH => @_) };
*{"${caller}::u"} = sub { _request(PUT => @_) };
*{"${caller}::x"} = sub { Mojo::DOM->new(@_) };
}

Expand Down Expand Up @@ -145,6 +146,13 @@ variable.
Perform C<HEAD> request with L<Mojo::UserAgent/"head"> and return resulting
L<Mojo::Message::Response> object.
=head2 C<o>
my $res = o('http://mojolicio.us');
Perform C<OPTIONS> request with L<Mojo::UserAgent/"options"> and return
resulting L<Mojo::Message::Response> object.
=head2 C<p>
my $res = p('http://mojolicio.us');
Expand Down
5 changes: 4 additions & 1 deletion t/mojolicious/ojo.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'development';
}

use Test::More tests => 10;
use Test::More tests => 11;

# "What do you mean 'we', flesh-tube?"
use ojo;
Expand All @@ -27,6 +27,9 @@ is g('/')->body, 'GET', 'right content';
# HEAD /
is h('/')->body, '', 'no content';

# OPTIONS /
is o('/')->body, 'OPTIONS', 'right content';

# PATCH /
is t('/')->body, 'PATCH', 'right content';

Expand Down
8 changes: 4 additions & 4 deletions t/mojolicious/tag_helper_lite_app.t
Expand Up @@ -14,8 +14,8 @@ use Test::More tests => 63;
use Mojolicious::Lite;
use Test::Mojo;

# GET /tags
get 'tags';
# OPTIONS /tags
options 'tags';

# GET /more_tags
get 'more_tags';
Expand Down Expand Up @@ -49,8 +49,8 @@ patch '/☃' => 'snowman';

my $t = Test::Mojo->new;

# GET /tags
$t->get_ok('/tags')->status_is(200)->content_is(<<EOF);
# OPTIONS /tags
$t->options_ok('/tags')->status_is(200)->content_is(<<EOF);
<foo />
<foo bar="baz" />
<foo one="t&lt;wo" three="four">Hello</foo>
Expand Down

0 comments on commit a05148a

Please sign in to comment.