Skip to content

Commit

Permalink
use the term prepare instead of inject
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 25, 2015
1 parent 1d6f383 commit b998555
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -33,6 +33,8 @@
preceding_siblings and previous_sibling methods in Mojo::DOM to
descendant_nodes, child_nodes, following_nodes, next_node, preceding_nodes
and previous_node.
- Renamed inject method in Mojo::UserAgent::CookieJar to prepare.
- Renamed inject method in Mojo::UserAgent::Proxy to prepare.
- Renamed params method in Mojo::Parameters to pairs.
- Renamed -A option of prefork command to -a.
- Added names method to Mojo::Parameters.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -316,7 +316,7 @@ sub _start {
$url->scheme($base->scheme)->authority($base->authority);
}

$_ && $_->inject($tx) for $self->proxy, $self->cookie_jar;
$_ && $_->prepare($tx) for $self->proxy, $self->cookie_jar;

# Connect and add request timeout if necessary
my $id = $self->emit(start => $tx)->_connection($nb, $tx, $cb);
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -95,7 +95,7 @@ sub find {
return \@found;
}

sub inject {
sub prepare {
my ($self, $tx) = @_;
return unless keys %{$self->{jar}};
my $req = $tx->req;
Expand Down Expand Up @@ -205,11 +205,11 @@ Find L<Mojo::Cookie::Request> objects in the jar for L<Mojo::URL> object.
# Names of all cookies found
say $_->name for @{$jar->find(Mojo::URL->new('http://example.com/foo'))};
=head2 inject
=head2 prepare
$jar->inject(Mojo::Transaction::HTTP->new);
$jar->prepare(Mojo::Transaction::HTTP->new);
Inject request cookies into transaction.
Prepare request cookies for transaction.
=head1 SEE ALSO
Expand Down
22 changes: 11 additions & 11 deletions lib/Mojo/UserAgent/Proxy.pm
Expand Up @@ -10,7 +10,11 @@ sub detect {
return $self->not([split ',', $ENV{NO_PROXY} || $ENV{no_proxy} || '']);
}

sub inject {
sub is_needed {
!grep { $_[1] =~ /\Q$_\E$/ } @{$_[0]->not || []};
}

sub prepare {
my ($self, $tx) = @_;

$self->detect if $ENV{MOJO_PROXY};
Expand All @@ -28,10 +32,6 @@ sub inject {
$req->proxy($https) if $https && $proto eq 'https';
}

sub is_needed {
!grep { $_[1] =~ /\Q$_\E$/ } @{$_[0]->not || []};
}

1;

=encoding utf8
Expand Down Expand Up @@ -90,18 +90,18 @@ Check environment variables C<HTTP_PROXY>, C<http_proxy>, C<HTTPS_PROXY>,
C<https_proxy>, C<NO_PROXY> and C<no_proxy> for proxy information. Automatic
proxy detection can be enabled with the C<MOJO_PROXY> environment variable.
=head2 inject
$proxy->inject(Mojo::Transaction::HTTP->new);
Inject proxy server information into transaction.
=head2 is_needed
my $bool = $proxy->is_needed('intranet.example.com');
Check if request for domain would use a proxy server.
=head2 prepare
$proxy->prepare(Mojo::Transaction::HTTP->new);
Prepare proxy server information for transaction.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
32 changes: 16 additions & 16 deletions t/mojo/cookiejar.t
Expand Up @@ -302,7 +302,7 @@ is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'bar', 'right value';
is $cookies->[1], undef, 'no second cookie';

# Extract and inject cookies without domain and path
# Extract and prepare cookies without domain and path
$jar = Mojo::UserAgent::CookieJar->new;
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://mojolicio.us/perldoc/Mojolicious');
Expand All @@ -311,24 +311,24 @@ $tx->res->cookies(
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://mojolicio.us/perldoc');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'without', 'right value';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://mojolicio.us/perldoc');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'without', 'right value';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://www.mojolicio.us/perldoc');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo'), undef, 'no cookie';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://mojolicio.us/whatever');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo'), undef, 'no cookie';

# Extract and inject cookies with same name (with and without domain)
# Extract and prepare cookies with same name (with and without domain)
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://example.com/test');
Expand All @@ -343,7 +343,7 @@ $tx->res->cookies(
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://example.com/test');
$jar->inject($tx);
$jar->prepare($tx);
$cookies = $tx->req->every_cookie('foo');
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'without', 'right value';
Expand All @@ -352,13 +352,13 @@ is $cookies->[1]->value, 'with', 'right value';
is $cookies->[2], undef, 'no third cookie';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://www.example.com/test');
$jar->inject($tx);
$jar->prepare($tx);
$cookies = $tx->req->every_cookie('foo');
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'with', 'right value';
is $cookies->[1], undef, 'no second cookie';

# Extract and inject cookies for "localhost" (valid and invalid)
# Extract and prepare cookies for "localhost" (valid and invalid)
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://localhost:3000');
Expand All @@ -377,12 +377,12 @@ $tx->res->cookies(
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://localhost:8080');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'local', 'right value';
is $tx->req->cookie('bar'), undef, 'no cookie';

# Extract and inject cookies with domain and path
# Extract and prepare cookies with domain and path
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://LABS.bücher.Com/perldoc/Mojolicious');
Expand All @@ -409,7 +409,7 @@ $tx->res->cookies(
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://labs.bücher.COM/perldoc/Mojolicious/Lite');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'with', 'right value';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
Expand All @@ -418,20 +418,20 @@ is $tx->req->cookie('0')->name, '0', 'right name';
is $tx->req->cookie('0')->value, 'with', 'right value';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://bücher.COM/perldoc/Mojolicious/Lite');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo'), undef, 'no cookie';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
is $tx->req->cookie('bar')->value, 'with', 'right value';
is $tx->req->cookie('0')->name, '0', 'right name';
is $tx->req->cookie('0')->value, 'with', 'right value';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://labs.bücher.COM/Perldoc');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo'), undef, 'no cookie';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
is $tx->req->cookie('bar')->value, 'with', 'right value';

# Extract and inject cookies with IP address
# Extract and prepare cookies with IP address
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://213.133.102.53/perldoc/Mojolicious');
Expand All @@ -446,7 +446,7 @@ $tx->res->cookies(
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://213.133.102.53/perldoc/Mojolicious');
$jar->inject($tx);
$jar->prepare($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'valid', 'right value';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
Expand Down

0 comments on commit b998555

Please sign in to comment.