Aion-Surf
view release on metacpan or search on metacpan
t/aion/surf.t view on Meta::CPAN
$response->content("put") when $_ eq "PUT http://example/ex";
$response->content("patch") when $_ eq "PATCH http://example/ex";
$response->content("delete") when $_ eq "DELETE http://example/ex";
$response->content('{"a":10}') when $_ eq "PATCH http://example/json";
default {
$response = HTTP::Response->new(404, "Not Found");
$response->content("nf");
}
}
$response
};
::is scalar do {get "http://example/ex"}, "get", 'get "http://example/ex" # => get';
::is scalar do {surf "http://example/ex"}, "get", 'surf "http://example/ex" # => get';
::is scalar do {head "http://example/ex"}, scalar do{1}, 'head "http://example/ex" # -> 1';
::is scalar do {head "http://example/not-found"}, scalar do{""}, 'head "http://example/not-found" # -> ""';
::is scalar do {surf HEAD => "http://example/ex"}, scalar do{1}, 'surf HEAD => "http://example/ex" # -> 1';
::is scalar do {surf HEAD => "http://example/not-found"}, scalar do{""}, 'surf HEAD => "http://example/not-found" # -> ""';
::is_deeply scalar do {[map { surf $_ => "http://example/ex" } qw/GET HEAD POST PUT PATCH DELETE/]}, scalar do {[qw/get 1 post put patch delete/]}, '[map { surf $_ => "http://example/ex" } qw/GET HEAD POST PUT PATCH DELETE/] # --> [qw/get 1 post put ...
::is_deeply scalar do {patch "http://example/json"}, scalar do {{a => 10}}, 'patch "http://example/json" # --> {a => 10}';
::is_deeply scalar do {[map patch, qw! http://example/ex http://example/json !]}, scalar do {["patch", {a => 10}]}, '[map patch, qw! http://example/ex http://example/json !] # --> ["patch", {a => 10}]';
::is scalar do {get ["http://example/ex", headers => {Accept => "*/*"}]}, "get", 'get ["http://example/ex", headers => {Accept => "*/*"}] # => get';
::is scalar do {surf "http://example/ex", headers => [Accept => "*/*"]}, "get", 'surf "http://example/ex", headers => [Accept => "*/*"] # => get';
#
# # DESCRIPTION
#
# Aion::Surf contains a minimal set of functions for surfing the Internet. The purpose of the module is to make surfing as easy as possible, without specifying many additional settings.
#
# # SUBROUTINES
#
# ## surf (\[$method], $url, \[$data], %params)
#
# Send request by LWP::UserAgent and adapt response.
#
# `@params` maybe:
#
# * `query` - add query params to `$url`.
# * `json` - body request set in json format. Add header `Content-Type: application/json; charset=utf-8`.
# * `form` - body request set in url params format. Add header `Content-Type: application/x-www-form-urlencoded`.
# * `headers` - add headers. If `header` is array ref, then add in the order specified. If `header` is hash ref, then add in the alphabet order.
# * `cookies` - add cookies. Same as: `cookies => {go => "xyz", session => ["abcd", path => "/page"]}`.
# * `response` - returns response (as HTTP::Response) by this reference.
#
done_testing; }; subtest 'surf (\[$method], $url, \[$data], %params)' => sub {
my $req = "MAYBE_ANY_METHOD https://ya.ru/page?z=30&x=10&y=%1F9E8
Accept: */*,image/*
Content-Type: application/x-www-form-urlencoded
x&y=2
";
my $req_cookies = 'Set-Cookie3: go=""; path="/"; domain=ya.ru; version=0
Set-Cookie3: session=%1F9E8; path="/page"; domain=ya.ru; version=0
';
# mock
*LWP::UserAgent::request = sub {
my ($ua, $request) = @_;
::is scalar do {$request->as_string}, scalar do{$req}, ' $request->as_string # -> $req';
::is scalar do {$ua->cookie_jar->as_string}, scalar do{$req_cookies}, ' $ua->cookie_jar->as_string # -> $req_cookies';
my $response = HTTP::Response->new(200, "OK");
$response->content(3.14);
$response
};
my $res = surf MAYBE_ANY_METHOD => "https://ya.ru/page?z=30", [x => 1, y => 2, z => undef],
headers => [
'Accept' => '*/*,image/*',
],
query => [x => 10, y => "ð§¨"],
response => \my $response,
cookies => {
go => "",
session => ["ð§¨", path => "/page"],
},
;
::is scalar do {$res}, scalar do{3.14}, '$res # -> 3.14';
::is scalar do {ref $response}, "HTTP::Response", 'ref $response # => HTTP::Response';
#
# ## head (;$url)
#
# Check resurce in internet. Returns `1` if exists resurce in internet, otherwice returns `""`.
#
# ## get (;$url)
#
# Get content from resurce in internet.
#
# ## post (;$url, \[$headers_href], %params)
#
# Add content resurce in internet.
#
# ## put (;$url, \[$headers_href], %params)
#
# Create or update resurce in internet.
#
# ## patch (;$url, \[$headers_href], %params)
#
# Set attributes on resurce in internet.
#
# ## del (;$url)
#
# Delete resurce in internet.
#
# ## chat_message ($chat_id, $message)
#
# Sends a message to a telegram chat.
#
done_testing; }; subtest 'chat_message ($chat_id, $message)' => sub {
# mock
use Aion::Format::Json;
( run in 3.275 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )