view release on metacpan or search on metacpan
Makefile.PL view on Meta::CPAN
202122232425262728293031
license
=>
repository
=> {
type
=>
'git'
,
},
},
},
PREREQ_PM
=> {
'Mojolicious'
=>
'7.00'
},
test
=> {
TESTS
=>
't/*.t'
},
);
t/lib/API/MikroTik/Mockup.pm view on Meta::CPAN
8990919293949596979899100101102103104105106107108109}
sub
cmd_login {
my
(
undef
,
$attr
) =
@_
;
my
$tag
=
$attr
->{
'.tag'
};
return
_done(
$tag
, {
ret
=>
'098f6bcd4621d373cade4e832627b4f6'
})
unless
$attr
->{name};
return
_done(
$tag
)
if
$attr
->{name} eq
'test'
&&
$attr
->{response} eq
'00119ce7e093e33497053e73f37a5d3e15'
;
return
[
'!fatal'
, {
message
=>
'cannot log in'
},
undef
,
$tag
];
}
sub
cmd_nocmd {
return
();
}
sub
cmd_resp {
t/mikrotik-online.t view on Meta::CPAN
123456789101112131415161718192021#!/usr/bin/env perl
use
warnings;
use
strict;
use
Test::More;
plan
skip_all
=>
'On-line tests. Set API_MIKROTIK_ONLINE to "host:user:pass:tls" to run.'
unless
$ENV
{API_MIKROTIK_ONLINE};
use
API::MikroTik;
my
(
$h
,
$u
,
$p
,
$tls
) =
split
':'
, (
$ENV
{API_MIKROTIK_ONLINE} ||
''
);
my
$a
= API::MikroTik->new(
user
=> (
$u
//
'admin'
),
password
=> (
$p
//
''
),
t/mikrotik-online.t view on Meta::CPAN
2526272829303132333435my
$res
;
$res
=
$a
->cmd(
'/interface/print'
,
{
'.proplist'
=>
'.id,name,type,running'
},
);
ok !
$a
->error,
'no error'
;
my
@keys
=
sort
keys
%{
$res
->[0]};
is_deeply [
@keys
], [
qw(.id name running type)
],
'right result'
;
done_testing();
t/mikrotik.t view on Meta::CPAN
181920212223242526272829303132333435363738use
Mojo::IOLoop;
# blocking
my
$loop
= Mojo::IOLoop->new();
my
$mockup
= API::MikroTik::Mockup->new()->ioloop(
$loop
);
my
$port
=
$loop
->acceptor(
$mockup
->server)->port;
my
$api
= API::MikroTik->new(
user
=>
'test'
,
password
=>
'tset'
,
host
=>
'127.0.0.1'
,
port
=>
$port
,
tls
=> 1,
ioloop
=>
$loop
,
);
# check connection
$api
->tls(1);
my
$res
=
$api
->cmd(
'/resp'
);
t/mikrotik.t view on Meta::CPAN
5455565758596061626364656667686970717273$ctime
= steady_time();
$res
=
$api
->cmd(
'/nocmd'
);
ok((steady_time() -
$ctime
) < 0.6,
'timeout ok'
);
$api
->timeout(1);
# close connection prematurely, next command should succeed
$res
=
$api
->cmd(
'/close/premature'
);
ok !
$res
,
'no result'
;
is
$api
->error,
'closed prematurely'
,
'right error'
;
# also check previous test case on errors
$res
=
$api
->cmd(
'/resp'
);
isa_ok
$res
,
'Mojo::Collection'
,
'right result type'
;
is_deeply
$res
, _gen_result(),
'right result'
;
$res
=
$api
->cmd(
'/resp'
, {
'.proplist'
=>
'prop0,prop2'
});
is_deeply
$res
, _gen_result(
'prop0,prop2'
),
'right result'
;
$res
=
$api
->cmd(
'/resp'
, {
'.proplist'
=>
'prop0,prop2'
,
count
=> 3});
is_deeply
$res
, _gen_result(
'prop0,prop2'
, 3),
'right result'
;
t/mikrotik.t view on Meta::CPAN
105106107108109110111112113114115116117118119120$api
->cmd(
'/err'
=>
sub
{
$err2
=
$_
[1] .
'2'
});
Mojo::IOLoop->timer(1.
3
=>
sub
{ Mojo::IOLoop->stop() });
Mojo::IOLoop->start();
is_deeply
$res
, {
key
=>
'nnn'
},
'right result'
;
is
$err
,
'interrupted'
,
'right error'
;
is
$err1
,
'random error1'
,
'right error'
;
is
$err2
,
'random error2'
,
'right error'
;
done_testing();
sub
_gen_result {
my
$attr
= API::MikroTik::Mockup::_gen_attr(
@_
);
return
[
$attr
,
$attr
];
}
12345678910use
Mojo::Base -strict;
use
Test::More;
plan
skip_all
=>
'set TEST_POD to enable this test (developer only!)'
unless
$ENV
{TEST_POD};
plan
skip_all
=>
'Test::Pod 1.14+ required for this test!'
unless
eval
'use Test::Pod 1.14; 1'
;
all_pod_files_ok();
t/pod_coverage.t view on Meta::CPAN
1234567891011use
Mojo::Base -strict;
use
Test::More;
plan
skip_all
=>
'set TEST_POD to enable this test (developer only!)'
unless
$ENV
{TEST_POD};
plan
skip_all
=>
'Test::Pod::Coverage 1.04+ required for this test!'
unless
eval
'use Test::Pod::Coverage 1.04; 1'
;
# DEPRECATED!
all_pod_coverage_ok({
also_private
=> [
'data'
,
'remaining'
]});
t/promises.t view on Meta::CPAN
101112131415161718192021222324252627282930313233343536use
FindBin;
use
API::MikroTik;
use
Mojo::IOLoop;
use
Test::More;
plan
skip_all
=>
'Mojolicious v7.54+ required for this test.'
unless
API::MikroTik->PROMISES;
my
$mockup
= API::MikroTik::Mockup->new();
my
$port
= Mojo::IOLoop->acceptor(
$mockup
->server)->port;
my
$api
= API::MikroTik->new(
user
=>
'test'
,
password
=>
'tset'
,
host
=>
'127.0.0.1'
,
port
=>
$port
,
tls
=> 1,
);
my
$p
=
$api
->cmd_p(
'/resp'
);
isa_ok
$p
,
'Mojo::Promise'
,
'right result type'
;
# connection errors
t/promises.t view on Meta::CPAN
48495051525354555657585960616263is
$err
,
'random error'
,
'right error'
;
is_deeply
$res
, [{
message
=>
'random error'
,
category
=> 0}],
'right error attributes'
;
# request
$api
->cmd_p(
'/resp'
)->then(
sub
{
$res
=
$_
[0] })
->
finally
(
sub
{ Mojo::IOLoop->stop() });
Mojo::IOLoop->start();
is_deeply
$res
, _gen_result(),
'right result'
;
done_testing();
sub
_gen_result {
my
$attr
= API::MikroTik::Mockup::_gen_attr(
@_
);
return
[
$attr
,
$attr
];
}
8081828384858687888990$r
= build_query([
a
=> [{
'='
, []}, 2, {}]]);
is_deeply
$r
, [
'?a=2'
],
'ignore empty structs'
;
my
$err
;
$SIG
{__WARN__} =
sub
{
$err
=
$_
[0] };
$r
= build_query([
a
=>
undef
,
b
=> [1,
undef
, 2],
c
=> {
'='
,
undef
}]);
ok !
$err
,
'no warning'
;
is_deeply
$r
, [
'?a='
,
'?b=1'
,
'?b='
,
'?b=2'
,
'?#||'
,
'?c='
,
'?#||'
],
'right result'
;
done_testing();
t/response.t view on Meta::CPAN
4142434445464748495051$w
=
$r
->parse(\
$parts
[1]);
is_deeply
$w
, [],
'right result'
;
ok
$r
->sentence->is_incomplete,
'incomplete is set'
;
$w
=
$r
->parse(\
$parts
[2]);
is_deeply
$w
, [(
$attr
) x 2],
'right result'
;
ok
$r
->sentence->is_incomplete,
'incomplete is set'
;
$w
=
$r
->parse(\
$parts
[3]);
is_deeply
$w
, [
$attr
],
'right result'
;
ok !
$r
->sentence->is_incomplete,
'incomplete is not set'
;
done_testing();
t/sentence.t view on Meta::CPAN
313233343536373839404142434445464748495051
.= encode_sentence(
'/cmd/2'
, {
c
=>
'foo'
,
d
=>
'bar'
}, {
e
=>
'baz'
}, 11);
my
$words
=
$s
->fetch(\
$packed
);
is
shift
@$words
,
'/cmd/1'
,
'right command'
;
is_deeply [
sort
@$words
], [
'=a=1'
,
'=b=2'
],
'right attributes'
;
$words
=
$s
->fetch(\
$packed
);
is
shift
@$words
,
'/cmd/2'
,
'right command'
;
is_deeply [
sort
@$words
], [
'.tag=11'
,
'=c=foo'
,
'=d=bar'
,
'?e=baz'
],
'right attributes'
;
# buffer ends in the middle of a word
$packed
= encode_sentence(
'/one/two/three'
, {
test
=> 1,
another
=> 2});
substr
$packed
, 20, 20,
''
;
$words
=
$s
->fetch(\
$packed
);
is_deeply
$words
, [
'/one/two/three'
],
'right results'
;
ok
$s
->is_incomplete,
'incomplete is set'
;
# reset
$s
->
reset
;
ok !
$s
->is_incomplete,
'incomplete is not longer set'
;
# buffer ends at the end of the word, before an empty closing word
t/sentence.t view on Meta::CPAN
5657585960616263646566is_deeply
$words
, [
'/one/two'
,
'=three=four'
],
'right results'
;
ok
$s
->is_incomplete,
'incomplete is set'
;
my
$err
;
$SIG
{__WARN__} =
sub
{
$err
=
$_
[0] };
$packed
= encode_sentence(
'/cmd'
, {
argv
=>
undef
});
ok !
$err
,
'no warning'
;
$words
=
$s
->
reset
->fetch(\
$packed
);
is_deeply
$words
, [
'/cmd'
,
'=argv='
],
'right results'
;
done_testing();