HTTP-AnyUA
view release on metacpan or search on metacpan
lib/HTTP/AnyUA.pm view on Meta::CPAN
or _usage(q{$any_ua->post_form($url, $formdata, \%options)});
my $headers = HTTP::AnyUA::Util::normalize_headers($args->{headers});
delete $args->{headers};
return $self->request(POST => $url, {
%$args,
content => HTTP::AnyUA::Util::www_form_urlencode($data),
headers => {
%$headers,
'content-type' => 'application/x-www-form-urlencoded',
},
});
}
# adapted from HTTP/Tiny.pm
sub mirror {
my ($self, $url, $file, $args) = @_;
@_ == 3 || (@_ == 4 && ref $args eq 'HASH')
or _usage(q{$any_ua->mirror($url, $filepath, \%options)});
lib/HTTP/AnyUA/Util.pm view on Meta::CPAN
$e = "$e";
$resp->{headers}{'client-original-status'} = $resp->{status} if $resp->{status};
$resp->{headers}{'client-original-reason'} = $resp->{reason} if $resp->{reason};
$resp->{success} = '';
$resp->{status} = 599;
$resp->{reason} = 'Internal Exception';
$resp->{content} = $e;
$resp->{headers}{'content-type'} = 'text/plain';
$resp->{headers}{'content-length'} = length $e;
return $resp;
}
# adapted from HTTP/Tiny.pm
sub split_url {
my $url = shift or _usage(q{split_url($url)});
t/03-post_form.t view on Meta::CPAN
foo => 'bar',
baz => 42,
};
my $resp = $any_ua->post_form($url, $form);
my $request = ($backend->requests)[-1];
is $request->[0], 'POST', 'post_form request method is POST';
is $request->[1], $url, 'post_form request URL is correct';
is $request->[2]{content}, 'baz=42&foo=bar', 'post_form request body is correct';
is $request->[2]{headers}{'content-type'}, 'application/x-www-form-urlencoded', 'post_form request content-type header is correct';
is($env->{content}, '', 'no body sent');
is($env->{HTTP_X_TEST_CUSTOM}, 'whatever', 'custom header sent');
like($env->{HTTP_X_TEST_MULTI}, qr/foo,\s*bar,\s*baz/, 'multi-value header sent');
}
is_response_content($resp, 'this is a document');
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 18);
is_response_header($resp, 'x-foo', 'bar');
response_protocol_ok($resp);
});
return $future;
};
# test:
# X custom headers
t/11-post.t view on Meta::CPAN
test_all_user_agents {
plan tests => 10;
my $ua = shift;
my $any_ua = HTTP::AnyUA->new(ua => $ua, response_is_future => 1);
my $path = '/create-document';
my $url = $server->url . $path;
my $future = $any_ua->post($url, {
headers => {'content-type' => 'text/plain'},
content => 'some document',
});
$future->on_ready(sub {
my $self = shift;
my $resp = $self->is_done ? $self->get : $self->failure;
my $env = $server->read_env;
note explain 'RESPONSE: ', $resp;
note explain 'ENV: ', $env;
t/11-post.t view on Meta::CPAN
is($env->{REQUEST_METHOD}, 'POST', 'correct method sent');
is($env->{REQUEST_URI}, $path, 'correct url sent');
is($env->{content}, 'some document', 'correct body sent');
}
is_response_content($resp, 'created document');
is_response_reason($resp, 'Created');
is_response_status($resp, 201);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
response_protocol_ok($resp);
});
return $future;
};
test_all_user_agents {
plan tests => 9;
my $ua = shift;
my $any_ua = HTTP::AnyUA->new(ua => $ua, response_is_future => 1);
my $path = '/modify-document';
my $url = $server->url . $path;
my $future = $any_ua->put($url, {
headers => {'content-type' => 'text/plain'},
content => 'some document',
});
$future->on_ready(sub {
my $self = shift;
my $resp = $self->is_done ? $self->get : $self->failure;
my $env = $server->read_env;
note explain 'RESPONSE: ', $resp;
note explain 'ENV: ', $env;
t/13-head.t view on Meta::CPAN
SKIP: {
skip 'unexpected env', 2 if ref($env) ne 'HASH';
is($env->{REQUEST_METHOD}, 'HEAD', 'correct method sent');
is($env->{REQUEST_URI}, $path, 'correct url sent');
}
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 18);
response_protocol_ok($resp);
my $body = ref($resp) eq 'HASH' && $resp->{content};
ok(!$body, 'response body is empty');
});
return $future;
};
t/15-custom-method.t view on Meta::CPAN
is($env->{REQUEST_URI}, $path, 'correct url sent');
is($env->{content}, '', 'no body sent');
is($env->{HTTP_X_TEST_CUSTOM}, 'whatever', 'custom header sent');
}
is_response_content($resp, 'this is a document');
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 18);
is_response_header($resp, 'x-foo', 'bar');
response_protocol_ok($resp);
});
return $future;
};
t/20-data_callback.t view on Meta::CPAN
is($env->{content}, '', 'no body sent');
}
is($body, 'this is a document', 'streamed response content matches');
ok($resp && !$resp->{content}, 'content in response structure is empty');
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 18);
is_response_header($resp, 'x-foo', 'bar');
response_protocol_ok($resp);
});
return $future;
};
t/21-basic-auth.t view on Meta::CPAN
is_response_content($resp, 'this is a document');
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
TODO: {
local $TODO = 'some user agents strip the auth from the URL';
# Mojo::UserAgent strips the auth from the URL
is_response_url($resp, $url);
};
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 18);
response_protocol_ok($resp);
});
return $future;
};
t/22-redirects.t view on Meta::CPAN
is_response_content($resp, 'you found it');
is_response_reason($resp, 'OK');
is_response_status($resp, 200);
is_response_success($resp, 1);
TODO: {
local $TODO = 'some user agents do not support this correctly';
# Furl has the URL from the original request, not the last request
is_response_url($resp, $server->url . '/baz');
};
is_response_header($resp, 'content-type', 'text/plain');
is_response_header($resp, 'content-length', 12);
response_protocol_ok($resp);
SKIP: {
skip 'no redirect chain', 18 if !$resp || !$resp->{redirects};
my $chain = $resp->{redirects};
isa_ok($chain, 'ARRAY', 'redirect chain');
is(scalar @$chain, 2, 'redirect chain has two redirections');
my $r1 = $chain->[0];
is_response_content($r1, 'the thing you seek is not here');
is_response_reason($r1, 'Found');
is_response_status($r1, 302);
is_response_success($r1, 0);
is_response_url($r1, $server->url . '/foo');
is_response_header($r1, 'content-type', 'text/plain');
is_response_header($r1, 'content-length', 30);
response_protocol_ok($r1);
my $r2 = $chain->[1];
is_response_content($r2, 'not here either');
is_response_reason($r2, 'Moved Permanently');
is_response_status($r2, 301);
is_response_success($r2, 0);
is_response_url($r2, $server->url . '/bar');
is_response_header($r2, 'content-type', 'text/plain');
is_response_header($r2, 'content-length', 15);
response_protocol_ok($r2);
}
});
return $future;
};
t/23-content-coderef.t view on Meta::CPAN
$ua->max_connections(0);
}
my $chunk = 0;
my @chunk = ('some ', 'document');
my $code = sub { return $chunk[$chunk++] };
my $path = '/create-document';
my $url = $server->url . $path;
my $future = $any_ua->post($url, {
headers => {'content-type' => 'text/plain'},
content => $code,
});
$future->on_ready(sub {
my $self = shift;
my $resp = $self->is_done ? $self->get : $self->failure;
my $env = $server->read_env;
note explain 'RESPONSE: ', $resp;
note explain 'ENV: ', $env;
t/23-content-coderef.t view on Meta::CPAN
is($env->{REQUEST_METHOD}, 'POST', 'correct method sent');
is($env->{REQUEST_URI}, $path, 'correct url sent');
is($env->{content}, 'some document', 'correct body sent');
}
is_response_content($resp, 'created document');
is_response_reason($resp, 'Created');
is_response_status($resp, 201);
is_response_success($resp, 1);
is_response_url($resp, $url);
is_response_header($resp, 'content-type', 'text/plain');
response_protocol_ok($resp);
});
return $future;
};
( run in 2.135 seconds using v1.01-cache-2.11-cpan-524268b4103 )