Skip to content

Commit

Permalink
fixed a few small bugs in Mojo::UserAgent::Transactor and tweaked doc…
Browse files Browse the repository at this point in the history
…umentation
  • Loading branch information
kraih committed Apr 22, 2013
1 parent 7861cc0 commit c27ad6c
Show file tree
Hide file tree
Showing 17 changed files with 461 additions and 432 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@
- Updated jQuery to version 2.0.
- Updated prettify.js to version 4-Mar-2013.
- Improved Mojo::URL to be able to contain scheme data for unknown schemes.
- Improved form content generator for GET and HEAD requests. (jberger)
- Improved default descriptions in Test::Mojo.
- Improved documentation.
- Improved tests.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/URL.pm
Expand Up @@ -248,7 +248,7 @@ Mojo::URL - Uniform Resource Locator
# Parse
my $url
= Mojo::URL->new('http://sri:foobar@kraih.com:3000/foo/bar?foo=bar#23');
= Mojo::URL->new('http://sri:foobar@example.com:3000/foo/bar?foo=bar#23');
say $url->scheme;
say $url->userinfo;
say $url->host;
Expand All @@ -261,7 +261,7 @@ Mojo::URL - Uniform Resource Locator
my $url = Mojo::URL->new;
$url->scheme('http');
$url->userinfo('sri:foobar');
$url->host('kraih.com');
$url->host('example.com');
$url->port(3000);
$url->path('/foo/bar');
$url->path('baz');
Expand Down Expand Up @@ -437,14 +437,14 @@ appended, defaults to a L<Mojo::Parameters> object.
=head2 to_abs
my $abs = $url->to_abs;
my $abs = $url->to_abs(Mojo::URL->new('http://kraih.com/foo'));
my $abs = $url->to_abs(Mojo::URL->new('http://example.com/foo'));
Clone relative URL and turn it into an absolute one.
=head2 to_rel
my $rel = $url->to_rel;
my $rel = $url->to_rel(Mojo::URL->new('http://kraih.com/foo'));
my $rel = $url->to_rel(Mojo::URL->new('http://example.com/foo'));
Clone absolute URL and turn it into a relative one.
Expand Down
88 changes: 47 additions & 41 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -814,18 +814,18 @@ Get absolute L<Mojo::URL> object for C<app> and switch protocol if necessary.
=head2 build_tx
my $tx = $ua->build_tx(GET => 'kraih.com');
my $tx = $ua->build_tx(PUT => 'http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->build_tx(GET => 'example.com');
my $tx = $ua->build_tx(PUT => 'http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->build_tx(
PUT => 'http://kraih.com' => {DNT => 1} => form => {a => 'b'});
PUT => 'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->build_tx(
PUT => 'http://kraih.com' => {DNT => 1} => json => {a => 'b'});
PUT => 'http://example.com' => {DNT => 1} => json => {a => 'b'});
Generate L<Mojo::Transaction::HTTP> object with
L<Mojo::UserAgent::Transactor/"tx">.
# Request with cookie
my $tx = $ua->build_tx(GET => 'kraih.com');
my $tx = $ua->build_tx(GET => 'example.com');
$tx->req->cookies({name => 'foo', value => 'bar'});
$ua->start($tx);
Expand All @@ -839,19 +839,19 @@ L<Mojo::UserAgent::Transactor/"websocket">.
=head2 delete
my $tx = $ua->delete('kraih.com');
my $tx = $ua->delete('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->delete('example.com');
my $tx = $ua->delete('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->delete(
'http://kraih.com' => {DNT => 1} => form => {a => 'b'});
'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->delete(
'http://kraih.com' => {DNT => 1} => json => {a => 'b'});
'http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<DELETE> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->delete('http://kraih.com' => sub {
$ua->delete('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Expand All @@ -867,35 +867,37 @@ enabled with the MOJO_PROXY environment variable.
=head2 get
my $tx = $ua->get('kraih.com');
my $tx = $ua->get('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->get('http://kraih.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->get('http://kraih.com' => {DNT => 1} => json => {a => 'b'});
my $tx = $ua->get('example.com');
my $tx = $ua->get('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->get('http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->get('http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<GET> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->get('http://kraih.com' => sub {
$ua->get('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 head
my $tx = $ua->head('kraih.com');
my $tx = $ua->head('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->head('http://kraih.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->head('http://kraih.com' => {DNT => 1} => json => {a => 'b'});
my $tx = $ua->head('example.com');
my $tx = $ua->head('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->head(
'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->head(
'http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<HEAD> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->head('http://kraih.com' => sub {
$ua->head('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Expand All @@ -909,73 +911,77 @@ Check if request for domain would use a proxy server.
=head2 options
my $tx = $ua->options('kraih.com');
my $tx = $ua->options('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->options('example.com');
my $tx = $ua->options('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->options(
'http://kraih.com' => {DNT => 1} => form => {a => 'b'});
'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->options(
'http://kraih.com' => {DNT => 1} => json => {a => 'b'});
'http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<OPTIONS> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the 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 {
$ua->options('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 patch
my $tx = $ua->patch('kraih.com');
my $tx = $ua->patch('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->patch('http://kraih.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->patch('http://kraih.com' => {DNT => 1} => json => {a => 'b'});
my $tx = $ua->patch('example.com');
my $tx = $ua->patch('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->patch(
'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->patch(
'http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<PATCH> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->patch('http://kraih.com' => sub {
$ua->patch('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 post
my $tx = $ua->post('kraih.com');
my $tx = $ua->post('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->post('http://kraih.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->post('http://kraih.com' => {DNT => 1} => json => {a => 'b'});
my $tx = $ua->post('example.com');
my $tx = $ua->post('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->post(
'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->post(
'http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<POST> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->post('http://kraih.com' => sub {
$ua->post('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 put
my $tx = $ua->put('kraih.com');
my $tx = $ua->put('http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->put('http://kraih.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->put('http://kraih.com' => {DNT => 1} => json => {a => 'b'});
my $tx = $ua->put('example.com');
my $tx = $ua->put('http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $ua->put('http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $ua->put('http://example.com' => {DNT => 1} => json => {a => 'b'});
Perform blocking HTTP C<PUT> request and return resulting
L<Mojo::Transaction::HTTP> object, takes the same arguments as
L<Mojo::UserAgent::Transactor/"tx"> (except for the method). You can also
append a callback to perform requests non-blocking.
$ua->put('http://kraih.com' => sub {
$ua->put('http://example.com' => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
});
Expand All @@ -988,7 +994,7 @@ append a callback to perform requests non-blocking.
Perform blocking request. You can also append a callback to perform requests
non-blocking.
my $tx = $ua->build_tx(GET => 'http://kraih.com');
my $tx = $ua->build_tx(GET => 'http://example.com');
$ua->start($tx => sub {
my ($ua, $tx) = @_;
say $tx->res->body;
Expand Down
56 changes: 31 additions & 25 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -185,13 +185,15 @@ sub _form {
return $tx;
}

# Urlencoded
$headers->content_type('application/x-www-form-urlencoded');
# Query parameters or urlencoded
my $p = Mojo::Parameters->new(map { $_ => $form->{$_} } sort keys %$form);
$p->charset($options{charset}) if defined $options{charset};
my $method = uc $req->method;
if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query->merge($p) }
else { $req->body($p->to_string) }
if ($method eq 'GET' || $method eq 'HEAD') { $req->url->query->merge($p) }
else {
$req->body($p->to_string);
$headers->content_type('application/x-www-form-urlencoded');
}
return $tx;
}

Expand Down Expand Up @@ -284,10 +286,10 @@ Mojo::UserAgent::Transactor - User agent transactor
say $t->tx(PATCH => 'mojolicio.us' => {DNT => 1} => 'Hi!')->req->to_string;
# POST request with form data
say $t->tx(POST => 'kraih.com' => form => {a => 'b'})->req->to_string;
say $t->tx(POST => 'example.com' => form => {a => 'b'})->req->to_string;
# PUT request with JSON data
say $t->tx(PUT => 'http://kraih.com' => json => {a => 'b'})->req->to_string;
say $t->tx(PUT => 'example.com' => json => {a => 'b'})->req->to_string;
=head1 DESCRIPTION
Expand Down Expand Up @@ -351,17 +353,17 @@ C<307> or C<308> redirect response if possible.
=head2 tx
my $tx = $t->tx(GET => 'kraih.com');
my $tx = $t->tx(POST => 'http://kraih.com');
my $tx = $t->tx(GET => 'http://kraih.com' => {DNT => 1});
my $tx = $t->tx(PUT => 'http://kraih.com' => 'Hi!');
my $tx = $t->tx(PUT => 'http://kraih.com' => form => {a => 'b'});
my $tx = $t->tx(PUT => 'http://kraih.com' => json => {a => 'b'});
my $tx = $t->tx(POST => 'http://kraih.com' => {DNT => 1} => 'Hi!');
my $tx = $t->tx(GET => 'example.com');
my $tx = $t->tx(POST => 'http://example.com');
my $tx = $t->tx(GET => 'http://example.com' => {DNT => 1});
my $tx = $t->tx(PUT => 'http://example.com' => 'Hi!');
my $tx = $t->tx(PUT => 'http://example.com' => form => {a => 'b'});
my $tx = $t->tx(PUT => 'http://example.com' => json => {a => 'b'});
my $tx = $t->tx(POST => 'http://example.com' => {DNT => 1} => 'Hi!');
my $tx = $t->tx(
PUT => 'http://kraih.com' => {DNT => 1} => form => {a => 'b'});
PUT => 'http://example.com' => {DNT => 1} => form => {a => 'b'});
my $tx = $t->tx(
PUT => 'http://kraih.com' => {DNT => 1} => json => {a => 'b'});
PUT => 'http://example.com' => {DNT => 1} => json => {a => 'b'});
Versatile general purpose L<Mojo::Transaction::HTTP> transaction builder for
requests, with support for content generators.
Expand All @@ -377,38 +379,42 @@ requests, with support for content generators.
my $tx = $t->tx(GET => 'http://mojolicio.us');
$tx->connection($sock);
# Generate query parameters
my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});
# Use form generator with custom charset
my $tx = $t->tx(
PUT => 'http://kraih.com' => form => {a => 'b'} => charset => 'UTF-8');
PUT => 'http://example.com' => form => {a => 'b'} => charset => 'UTF-8');
# Multiple form values with the same name
my $tx = $t->tx(PUT => 'http://kraih.com' => form => {a => [qw(b c d)]});
my $tx = $t->tx(PUT => 'http://example.com' => form => {a => [qw(b c d)]});
# Multipart upload streamed from file
my $tx = $t->tx(
PUT => 'http://kraih.com' => form => {mytext => {file => '/foo.txt'}});
PUT => 'http://example.com' => form => {mytext => {file => '/foo.txt'}});
# Multipart upload with in-memory content
my $tx = $t->tx(
POST => 'http://kraih.com' => form => {mytext => {content => 'lalala'}});
POST => 'http://example.com' => form => {mytext => {content => 'lala'}});
# Upload multiple files
my $tx = $t->tx(POST => 'http://kraih.com' =>
my $tx = $t->tx(POST => 'http://example.com' =>
form => {mytext => [{content => 'first'}, {content => 'second'}]});
# Customized upload with filename and header
my $tx = $t->tx(POST => 'http://kraih.com' => form => {
my $tx = $t->tx(POST => 'http://example.com' => form => {
myzip => {
file => Mojo::Asset::Memory->new->add_chunk('lalala'),
filename => 'foo.zip',
DNT => 1
}
});
While the "multipart/form-data" content type will be automatically used
instead of "application/x-www-form-urlencoded" when necessary, you can also
enforce it by setting the header manually. GET and HEAD transactions merge
form paramters into the url query parameters.
The C<form> content generator will automatically use query parameters for
C<GET>/C<HEAD> requests and the "application/x-www-form-urlencoded" content
type for everything else. Both get upgraded automatically to using the
"multipart/form-data" content type when necessary or when the header has been
set manually.
=head2 upgrade
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -919,7 +919,7 @@ Get L<Mojo::UserAgent> object from L<Mojo/"ua">.
# Blocking
my $tx = $c->ua->get('http://mojolicio.us');
my $tx = $c->ua->post('http://kraih.com/login' => form => {user => 'mojo'});
my $tx = $c->ua->post('example.com/login' => form => {user => 'mojo'});
# Non-blocking
$c->ua->get('http://mojolicio.us' => sub {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -634,7 +634,7 @@ Same for monitoring tasks.
$self->hook(after_dispatch => sub {
my $self = shift;
return unless my $e = $self->stash('exception');
$self->ua->post('https://kraih.com/bugs' => form => {exception => $e});
$self->ua->post('https://example.com/bugs' => form => {exception => $e});
});

For a full list of available hooks see L<Mojolicious/"hook">.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -353,7 +353,7 @@ Generate file input element.
%= text_field 'first_name'
%= submit_button
% end
%= form_for 'http://kraih.com/login' => (method => 'POST') => begin
%= form_for 'http://example.com/login' => (method => 'POST') => begin
%= text_field 'first_name'
%= submit_button
% end
Expand All @@ -373,7 +373,7 @@ but not C<GET>, a C<method> attribute will be automatically added.
<input name="first_name" />
<input value="Ok" type="submit" />
</form>
<form action="http://kraih.com/login" method="POST">
<form action="http://example.com/login" method="POST">
<input name="first_name" />
<input value="Ok" type="submit" />
</form>
Expand Down

0 comments on commit c27ad6c

Please sign in to comment.