Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions t/getups.t
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ subtest 'getUPS defaults country to US' => sub {
like( $captured_url, qr/22_destCountry=US/, 'defaults to US' );
};

subtest 'getUPS sends to correct UPS endpoint with required params' => sub {
my $captured_url;
{
no warnings 'redefine';
*LWP::UserAgent::get = sub {
my ( $self, $url ) = @_;
$captured_url = $url;
return MockResponse->new( success => 1, content => $success_response );
};
}

getUPS(qw/GNDCOM 23606 23607 50/);

like( $captured_url, qr{^https://www\.ups\.com/using/services/rave/qcostcgi\.cgi\?},
'uses correct UPS CGI base URL' );
like( $captured_url, qr/accept_UPS_license_agreement=yes/, 'includes license agreement' );
like( $captured_url, qr/13_product=GNDCOM/, 'product code in URL' );
like( $captured_url, qr/15_origPostal=23606/, 'origin zip in URL' );
like( $captured_url, qr/19_destPostal=23607/, 'dest zip in URL' );
like( $captured_url, qr/23_weight=50/, 'weight in URL' );
};

subtest 'getUPS includes oversized and COD flags' => sub {
my $captured_url;
{
Expand Down
70 changes: 70 additions & 0 deletions t/upstrack.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use strict;
use warnings;
use Test::More;
use JSON::PP qw(decode_json);
use Business::UPS;

# Mock LWP::UserAgent to avoid real HTTP requests
Expand Down Expand Up @@ -384,4 +385,73 @@ subtest 'UPStrack dies on undef tracking number' => sub {
like( $@, qr/tracking/i, 'dies on undef tracking number' );
};

# Request format validation — verify outgoing HTTP request is correct
subtest 'UPStrack sends correct request format' => sub {
my ( $captured_url, @captured_args );
{
no warnings 'redefine';
*LWP::UserAgent::post = sub {
my ( $self, $url, @args ) = @_;
$captured_url = $url;
@captured_args = @args;
return MockResponse->new( success => 1, content => $delivered_json );
};
}

UPStrack("1Z999AA10123456784");

# Verify URL
is( $captured_url,
'https://www.ups.com/track/api/Track/GetStatus?loc=en_US',
'posts to correct UPS tracking API endpoint' );

# Verify Content-Type header
my %headers;
for ( my $i = 0; $i < $#captured_args; $i += 2 ) {
$headers{ $captured_args[$i] } = $captured_args[ $i + 1 ];
}
is( $headers{'Content-Type'}, 'application/json', 'Content-Type is application/json' );

# Verify JSON payload structure
ok( defined $headers{'Content'}, 'request body is present' );
my $payload = decode_json( $headers{'Content'} );
is( $payload->{Locale}, 'en_US', 'payload Locale is en_US' );
is( ref $payload->{TrackingNumber}, 'ARRAY', 'TrackingNumber is an array' );
is_deeply( $payload->{TrackingNumber}, ['1Z999AA10123456784'],
'TrackingNumber contains the input tracking number' );

# Restore normal mock
no warnings 'redefine';
*LWP::UserAgent::post = sub {
my ( $self, $url, @args ) = @_;
return shift @mock_responses;
};
};

subtest 'UPStrack passes different tracking numbers correctly' => sub {
my $captured_body;
{
no warnings 'redefine';
*LWP::UserAgent::post = sub {
my ( $self, $url, @args ) = @_;
my %h = @args;
$captured_body = $h{'Content'};
return MockResponse->new( success => 1, content => $minimal_json );
};
}

UPStrack("1ZABC123XYZ");

my $payload = decode_json($captured_body);
is_deeply( $payload->{TrackingNumber}, ['1ZABC123XYZ'],
'tracking number passed through to payload' );

# Restore normal mock
no warnings 'redefine';
*LWP::UserAgent::post = sub {
my ( $self, $url, @args ) = @_;
return shift @mock_responses;
};
};

done_testing();
Loading