From 8c99f81cb5acd9923b7c71171d5050eb319b6262 Mon Sep 17 00:00:00 2001 From: Ryan Kereliuk Date: Wed, 2 Nov 2022 08:41:50 -0700 Subject: Add missing unit test coverage and fix SIP URI to pass tests. Mainly fixing the params() setter but make the code more readable in general. --- lib/URI/sip.pm | 40 ++++++++++++++++++---------------------- t/sip.t | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 37 deletions(-) diff --git a/lib/URI/sip.pm b/lib/URI/sip.pm index 4aa2cfa..250b905 100644 --- a/lib/URI/sip.pm +++ b/lib/URI/sip.pm @@ -22,37 +22,33 @@ sub authority { my $self = shift; $$self =~ m,^($URI::scheme_re:)?([^;?]*)(.*)$,os or die; - my $old = $2; + my $start = $1; + my $authoritystr = $2; + my $rest = $3; if (@_) { - my $auth = shift; - $$self = defined($1) ? $1 : ""; - my $rest = $3; - if (defined $auth) { - $auth =~ s/([^$URI::uric])/ URI::Escape::escape_char($1)/ego; - $$self .= "$auth"; - } - $$self .= $rest; + $authoritystr = shift; + $authoritystr =~ s/([^$URI::uric])/ URI::Escape::escape_char($1)/ego; + $$self = $start . $authoritystr . $rest; } - $old; + return $authoritystr; } sub params_form { my $self = shift; $$self =~ m,^((?:$URI::scheme_re:)?)(?:([^;?]*))?(;[^?]*)?(.*)$,os or die; + my $start = $1 . $2; my $paramstr = $3; + my $rest = $4; if (@_) { - my @args = @_; - $$self = $1 . $2; - my $rest = $4; - my @new; - for (my $i=0; $i < @args; $i += 2) { - push(@new, "$args[$i]=$args[$i+1]"); + my @paramarr; + for (my $i = 0; $i < @_; $i += 2) { + push(@paramarr, "$_[$i]=$_[$i+1]"); } - $paramstr = join(";", @new); - $$self .= ";" . $paramstr . $rest; + $paramstr = join(";", @paramarr); + $$self = $start . ";" . $paramstr . $rest; } $paramstr =~ s/^;//o; return split(/[;=]/, $paramstr); @@ -62,13 +58,13 @@ sub params { my $self = shift; $$self =~ m,^((?:$URI::scheme_re:)?)(?:([^;?]*))?(;[^?]*)?(.*)$,os or die; + my $start = $1 . $2; my $paramstr = $3; + my $rest = $4; if (@_) { - my $new = shift; - $$self = $1 . $2; - my $rest = $4; - $$self .= $paramstr . $rest; + $paramstr = shift; + $$self = $start . ";" . $paramstr . $rest; } $paramstr =~ s/^;//o; return $paramstr; diff --git a/t/sip.t b/t/sip.t index a12f41c..dca67e4 100644 --- a/t/sip.t +++ b/t/sip.t @@ -1,50 +1,64 @@ use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 13; use URI (); my $u = URI->new('sip:phone@domain.ext'); ok($u->user eq 'phone' && - $u->host eq 'domain.ext' && - $u->port eq '5060' && - $u eq 'sip:phone@domain.ext'); + $u->host eq 'domain.ext' && + $u->port eq '5060' && + $u->host_port eq 'domain.ext:5060' && + $u->authority eq 'phone@domain.ext' && + $u eq 'sip:phone@domain.ext'); $u->host_port('otherdomain.int:9999'); ok($u->host eq 'otherdomain.int' && $u->port eq '9999' && + $u->host_port eq 'otherdomain.int:9999' && + $u->authority eq 'phone@otherdomain.int:9999' && $u eq 'sip:phone@otherdomain.int:9999'); $u->port('5060'); $u = $u->canonical; -ok($u->host eq 'otherdomain.int' && - $u->port eq '5060' && +ok($u->port eq '5060' && + $u->host_port eq 'otherdomain.int:5060' && + $u->authority eq 'phone@otherdomain.int' && $u eq 'sip:phone@otherdomain.int'); $u->user('voicemail'); ok($u->user eq 'voicemail' && + $u->authority eq 'voicemail@otherdomain.int' && $u eq 'sip:voicemail@otherdomain.int'); +$u->authority('fax@gateway.ext'); +ok($u->user eq 'fax' && + $u->host eq 'gateway.ext' && + $u->host_port eq 'gateway.ext:5060' && + $u->authority eq 'fax@gateway.ext' && + $u eq 'sip:fax@gateway.ext'); + $u = URI->new('sip:phone@domain.ext?Subject=Meeting&Priority=Urgent'); -ok($u->host eq 'domain.ext' && - $u->query eq 'Subject=Meeting&Priority=Urgent'); +ok($u->query eq 'Subject=Meeting&Priority=Urgent'); $u->query_form(Subject => 'Lunch', Priority => 'Low'); my @q = $u->query_form; -ok($u->host eq 'domain.ext' && - $u->query eq 'Subject=Lunch&Priority=Low' && - @q == 4 && "@q" eq "Subject Lunch Priority Low"); +ok($u->query eq 'Subject=Lunch&Priority=Low' && + @q == 4 && "@q" eq 'Subject Lunch Priority Low' && + $u eq 'sip:phone@domain.ext?Subject=Lunch&Priority=Low'); $u = URI->new('sip:phone@domain.ext;maddr=127.0.0.1;ttl=16'); -ok($u->host eq 'domain.ext' && - $u->params eq 'maddr=127.0.0.1;ttl=16'); +ok($u->params eq 'maddr=127.0.0.1;ttl=16'); + +$u->params('maddr=127.0.0.1;ttl=16;x-addedparam=1'); +ok($u->params eq 'maddr=127.0.0.1;ttl=16;x-addedparam=1' && + $u eq 'sip:phone@domain.ext;maddr=127.0.0.1;ttl=16;x-addedparam=1'); $u = URI->new('sip:phone@domain.ext?Subject=Meeting&Priority=Urgent'); $u->params_form(maddr => '127.0.0.1', ttl => '16'); my @p = $u->params_form; -ok($u->host eq 'domain.ext' && - $u->query eq 'Subject=Meeting&Priority=Urgent' && +ok($u->query eq 'Subject=Meeting&Priority=Urgent' && $u->params eq 'maddr=127.0.0.1;ttl=16' && @p == 4 && "@p" eq "maddr 127.0.0.1 ttl 16"); -- cgit v1.2.1