Skip to content

Commit

Permalink
Add tests for useragent_string() and useragent_timeout()
Browse files Browse the repository at this point in the history
  • Loading branch information
stevieb9 committed Apr 19, 2022
1 parent f345b14 commit 4ae0327
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Tesla/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ sub useragent_timeout {
my ($self, $timeout) = @_;

if (defined $timeout) {
if ($timeout !~ /^\d+?(?:\.\d+)?$/) {
croak "useragent_timeout() requires an integer or float";
}
$self->{useragent_timeout} = $timeout;
}

Expand Down
22 changes: 22 additions & 0 deletions t/205-useragent_string.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use warnings;
use strict;
use feature 'say';

use Tesla::API;
use Test::More;

my $constant_ua_string = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0';

my $t = Tesla::API->new(unauthenticated => 1);

is
$t->useragent_string,
$constant_ua_string,
"useragent_string() returns proper default ok";

is
$t->useragent_string('Test Browser'),
'Test Browser',
"Setting user agent works ok";

done_testing();
29 changes: 29 additions & 0 deletions t/210-useragent_timeout.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use warnings;
use strict;
use feature 'say';

use Tesla::API;
use Test::More;

my $constant_ua_timeout = 180;

my $t = Tesla::API->new(unauthenticated => 1);

is
$t->useragent_timeout,
$constant_ua_timeout,
"useragent_timeout() returns proper default ok";

for ('*', '?', 'aa', '99a', 'aa9', '0.a', '-1', '-1.0', '1.') {
my $ok = eval { $t->useragent_timeout($_); 1; };
is $ok, undef, "useragent_timeout() with '$_' param croaks ok";
}

for (999, .2, 2.2, 10.10, 0.1) {
is
$t->useragent_timeout($_),
$_,
"Setting user agent to '$_' works ok";
}

done_testing();

0 comments on commit 4ae0327

Please sign in to comment.