Skip to content

Commit

Permalink
Added t/30-basics.t
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Dec 10, 2024
1 parent 4282cf5 commit 1b3aba5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Revision history for CGI-Info
0.87
Test warnings()
Allow the logger to be a ref to code
Fix is_mobile() default return code
Added t/30-basics.t

0.86 Mon Nov 25 09:17:30 EST 2024
Ensure correct message is logged on SQL injection attempt
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ README.md
t/00-load.t
t/10-compile.t
t/20-new.t
t/30-basics.t
t/allow.t
t/apoc.t
t/as_string.t
Expand Down
5 changes: 4 additions & 1 deletion lib/CGI/Info.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ sub is_mobile {
return 1;
}
}

if($ENV{'HTTP_X_WAP_PROFILE'}) {
# E.g. Blackberry
# TODO: Check the sanity of this variable
Expand Down Expand Up @@ -1043,9 +1044,11 @@ sub is_mobile {
$self->{browser_detect} = HTTP::BrowserDetect->new($agent);
}
}

if($self->{browser_detect}) {
my $device = $self->{browser_detect}->device();
my $is_mobile = (defined($device) && ($device =~ /blackberry|webos|iphone|ipod|ipad|android/i));
# Without the ?1:0 it will set to the empty string not 0
my $is_mobile = (defined($device) && ($device =~ /blackberry|webos|iphone|ipod|ipad|android/i)) ? 1 : 0;
if($is_mobile && $self->{cache} && defined($remote)) {
$self->{cache}->set("$remote/$agent", 'mobile', '1 day');
}
Expand Down
29 changes: 29 additions & 0 deletions t/30-basics.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::Most tests => 8;

# Load the module being tested
BEGIN { use_ok('CGI::Info') }

local %ENV;
$ENV{'SCRIPT_NAME'} = 'test_script';
$ENV{'HTTP_USER_AGENT'} = 'Mozilla/5.0';

# Test object creation
my $cgi_info = CGI::Info->new();
ok($cgi_info, 'CGI::Info object created');

# Test script_name method
can_ok($cgi_info, 'script_name');
my $script_name = $cgi_info->script_name();
is($script_name, $ENV{'SCRIPT_NAME'}, 'script_name matches the environment variable');

# Test host_name method
can_ok($cgi_info, 'host_name');
like($cgi_info->host_name(), qr/\w+/, 'host_name returns a valid string');

# Test is_mobile method
can_ok($cgi_info, 'is_mobile');
is($cgi_info->is_mobile(), 0, 'is_mobile returns false by default (not a mobile device)');

0 comments on commit 1b3aba5

Please sign in to comment.