Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit f333fcd

Browse files
committed
compare request strings by creating a canonized json form to avoid url variances
1 parent c6d9ea1 commit f333fcd

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

t/basic.t

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use Test::InDistDir;
66
use Test::More;
77
use Capture::Tiny 'capture';
88
use Test::Fatal;
9+
use URI;
910

1011
use WebService::Plotly;
1112

@@ -18,7 +19,7 @@ exit;
1819
sub run {
1920
my %user = (
2021
un => "api_test",
21-
key => "key",
22+
key => "api_key",
2223
password => "password",
2324
email => '[email protected]',
2425
);
@@ -177,30 +178,47 @@ sub run {
177178

178179
sub fake_responses {
179180
my ( $old, $user ) = @_;
180-
my $email = $user->{email};
181-
$email =~ s/\@/\%40/;
182-
my $version = WebService::Plotly->version || '';
183-
my %pairs = (
184-
"/apimkacct / email=$email&un=$user->{un}&version=$version&platform=Perl" =>
181+
my $email = $user->{email};
182+
my $version = WebService::Plotly->version || '';
183+
my @req_vals = ( $user->{key}, $user->{un}, $version );
184+
my %pairs = (
185+
sprintf( q|/apimkacct / {"email":"%s","platform":"Perl","un":"%s","version":"%s"}|,
186+
$user->{email}, $user->{un}, $version ) =>
185187
qq[{"api_key": "$user->{key}", "message": "", "un": "$user->{un}", "tmp_pw": "$user->{password}", "error": ""}],
186-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3Anull%7D&un=$user->{un}&version=$version&origin=plot&args=%5B%5B1%2C2%2C3%2C4%5D%2C%5B10%2C15%2C13%2C17%5D%2C%5B2%2C3%2C4%2C5%5D%2C%5B16%2C5%2C11%2C9%5D%5D&platform=Perl&key=$user->{key}"
187-
188-
=> qq[{"url": "https://plot.ly/~$user->{un}/0", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/0 or inside your plot.ly account where it is named 'plot from API'", "warning": "", "filename": "plot from API", "error": ""}],
189-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3A%22plot+from+API%22%7D&un=$user->{un}&version=$version&origin=plot&args=%5B%7B%22pointpos%22%3A-1.8%2C%22y%22%3A%5B1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%2C1%5D%2C%22boxpoints%22%3A%22all%22%2C%22jitter%22%3A0.3%2C%22type%22%3A%22box%22%7D%5D&platform=Perl&key=$user->{key}"
190-
=> qq[{"url": "https://plot.ly/~$user->{un}/1", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/1 or inside your plot.ly account where it is named 'plot from API (1)'", "warning": "", "filename": "plot from API (1)", "error": ""}],
191-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3A%22plot+from+API+(1)%22%7D&un=$user->{un}&version=$version&origin=plot&args=%5B%5B0%2C1.28228271575094%2C2.56456543150187%2C3.84684814725281%2C5.12913086300374%2C6.41141357875468%2C7.69369629450562%2C8.97597901025655%2C10.2582617260075%2C11.5405444417584%2C12.8228271575094%2C14.1051098732603%2C15.3873925890112%2C16.6696753047622%2C17.9519580205131%2C19.234240736264%2C20.516523452015%2C21.7988061677659%2C23.0810888835168%2C24.3633715992678%2C25.6456543150187%2C26.9279370307697%2C28.2102197465206%2C29.4925024622715%2C30.7747851780225%2C32.0570678937734%2C33.3393506095243%2C34.6216333252753%2C35.9039160410262%2C37.1861987567771%2C38.4684814725281%2C39.750764188279%2C41.03304690403%2C42.3153296197809%2C43.5976123355318%2C44.8798950512828%2C46.1621777670337%2C47.4444604827846%2C48.7267431985356%2C50.0090259142865%2C51.2913086300374%2C52.5735913457884%2C53.8558740615393%2C55.1381567772902%2C56.4204394930412%2C57.7027222087921%2C58.9850049245431%2C60.267287640294%2C61.5495703560449%2C62.8318530717959%5D%2C%5B0%2C0.843294627805856%2C0.422128698503034%2C-0.441226470165472%2C-0.547503487089109%2C0.0673517422367614%2C0.457366285904565%2C0.176828213301626%2C-0.265389780406003%2C-0.269674043959601%2C0.0703646298416577%2C0.243893188115149%2C0.0676384677126959%2C-0.154863929056215%2C-0.129858243347091%2C0.05482869629949%2C0.127928477398816%2C0.021611443873364%2C-0.0881715414468317%2C-0.0608587917241091%2C0.0377619990293321%2C0.0659943959757905%2C0.00381506496885814%2C-0.0491560023375872%2C-0.0275581151401661%2C0.0242415664367422%2C0.0334587771159902%2C-0.00200936014698983%2C-0.0268958480722349%2C-0.0119084571919307%2C0.0148506747745368%2C0.0166484433762141%2C-0.00315755789163243%2C-0.0144629574290807%2C-0.0047964523556046%2C0.00879030769874005%2C0.00811161194115449%2C-0.00274140367899917%2C-0.00764893974873858%2C-0.00170756920936203%2C0.0050639031844756%2C0.00385613988297645%2C-0.00198811948558657%2C-0.00397903179641911%2C-0.000453403047040574%2C0.00285196918046206%2C0.0017784507207353%2C-0.00131658072954927%2C-0.00203518684842697%2C-4.57376446929804e-018%5D%5D&platform=Perl&key=$user->{key}"
192-
=> qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
193-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3A%22plot+from+API+(2)%22%7D&un=$user->{un}&version=$version&origin=style&args=%5B%7B%22line%22%3A%7B%22width%22%3A4%2C%22color%22%3A%22rgb(84%2C+39%2C+143)%22%7D%7D%5D&platform=Perl&key=$user->{key}"
194-
=> qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
195-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3A%22plot+from+API+(2)%22%7D&un=$user->{un}&version=$version&origin=layout&args=%5B%7B%22width%22%3A600%2C%22titlefont%22%3A%7B%22color%22%3A%22rgb(84%2C+39%2C+143)%22%2C%22size%22%3A25%2C%22family%22%3A%22%5C%22Avant+Garde%5C%22%2C+Avantgarde%2C+%5C%22Century+Gothic%5C%22%2C+CenturyGothic%2C+%5C%22AppleGothic%5C%22%2C+sans-serif%22%7D%2C%22showlegend%22%3Anull%2C%22font%22%3A%7B%22color%22%3A%22rgb(84%2C+39%2C+143)%22%2C%22size%22%3A20%2C%22family%22%3A%22%5C%22Avant+Garde%5C%22%2C+Avantgarde%2C+%5C%22Century+Gothic%5C%22%2C+CenturyGothic%2C+%5C%22AppleGothic%5C%22%2C+sans-serif%22%7D%2C%22height%22%3A600%2C%22autosize%22%3Anull%2C%22title%22%3A%22Damped+Sinusoid%22%2C%22margin%22%3A%7B%22pad%22%3A2%2C%22l%22%3A70%2C%22r%22%3A40%2C%22b%22%3A60%2C%22t%22%3A60%7D%2C%22paper_bgcolor%22%3A%22rgb(188%2C+189%2C+220)%22%2C%22plot_bgcolor%22%3A%22rgb(158%2C+154%2C+200)%22%7D%5D&platform=Perl&key=$user->{key}"
196-
=> qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
197-
"/clientresp / kwargs=%7B%22fileopt%22%3Anull%2C%22filename%22%3A%22plot+from+API+(2)%22%7D&un=$user->{un}&version=$version&origin=layout&args=%5B%7B%7D%5D&platform=Perl&key=$user->{key}"
198-
=> qq[{"url": "", "message": "", "warning": "", "filename": "", "error": "Traceback (most recent call last):\\n File \\"/home/jp/dj/shelly/remote/remoteviews.py\\", line 311, in clientresp\\n grph = {'layout': args[0]}\\nKeyError: 0\\n"}],
188+
sprintf(
189+
q|/clientresp / {"args":"[[1,2,3,4],[10,15,13,17],[2,3,4,5],[16,5,11,9]]","key":"%s","kwargs":"{\"filename\":null,\"fileopt\":null}","origin":"plot","platform":"Perl","un":"%s","version":"%s"}|,
190+
@req_vals ) =>
191+
qq[{"url": "https://plot.ly/~$user->{un}/0", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/0 or inside your plot.ly account where it is named 'plot from API'", "warning": "", "filename": "plot from API", "error": ""}],
192+
sprintf(
193+
q|/clientresp / {"args":"[{\"boxpoints\":\"all\",\"jitter\":0.3,\"pointpos\":-1.8,\"type\":\"box\",\"y\":[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}]","key":"%s","kwargs":"{\"filename\":\"plot from API\",\"fileopt\":null}","origin":"plot","platform":"Perl","un":"%s","version":"%s"}|,
194+
@req_vals ) =>
195+
qq[{"url": "https://plot.ly/~$user->{un}/1", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/1 or inside your plot.ly account where it is named 'plot from API (1)'", "warning": "", "filename": "plot from API (1)", "error": ""}],
196+
sprintf(
197+
q|/clientresp / {"args":"[[0,1.28228271575094,2.56456543150187,3.84684814725281,5.12913086300374,6.41141357875468,7.69369629450562,8.97597901025655,10.2582617260075,11.5405444417584,12.8228271575094,14.1051098732603,15.3873925890112,16.6696753047622,17.9519580205131,19.234240736264,20.516523452015,21.7988061677659,23.0810888835168,24.3633715992678,25.6456543150187,26.9279370307697,28.2102197465206,29.4925024622715,30.7747851780225,32.0570678937734,33.3393506095243,34.6216333252753,35.9039160410262,37.1861987567771,38.4684814725281,39.750764188279,41.03304690403,42.3153296197809,43.5976123355318,44.8798950512828,46.1621777670337,47.4444604827846,48.7267431985356,50.0090259142865,51.2913086300374,52.5735913457884,53.8558740615393,55.1381567772902,56.4204394930412,57.7027222087921,58.9850049245431,60.267287640294,61.5495703560449,62.8318530717959],[0,0.843294627805856,0.422128698503034,-0.441226470165472,-0.547503487089109,0.0673517422367614,0.457366285904565,0.176828213301626,-0.265389780406003,-0.269674043959601,0.0703646298416577,0.243893188115149,0.0676384677126959,-0.154863929056215,-0.129858243347091,0.05482869629949,0.127928477398816,0.021611443873364,-0.0881715414468317,-0.0608587917241091,0.0377619990293321,0.0659943959757905,0.00381506496885814,-0.0491560023375872,-0.0275581151401661,0.0242415664367422,0.0334587771159902,-0.00200936014698983,-0.0268958480722349,-0.0119084571919307,0.0148506747745368,0.0166484433762141,-0.00315755789163243,-0.0144629574290807,-0.0047964523556046,0.00879030769874005,0.00811161194115449,-0.00274140367899917,-0.00764893974873858,-0.00170756920936203,0.0050639031844756,0.00385613988297645,-0.00198811948558657,-0.00397903179641911,-0.000453403047040574,0.00285196918046206,0.0017784507207353,-0.00131658072954927,-0.00203518684842697,-4.57376446929804e-018]]","key":"%s","kwargs":"{\"filename\":\"plot from API (1)\",\"fileopt\":null}","origin":"plot","platform":"Perl","un":"%s","version":"%s"}|,
198+
@req_vals ) =>
199+
qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
200+
sprintf(
201+
q|/clientresp / {"args":"[{\"line\":{\"color\":\"rgb(84, 39, 143)\",\"width\":4}}]","key":"%s","kwargs":"{\"filename\":\"plot from API (2)\",\"fileopt\":null}","origin":"style","platform":"Perl","un":"%s","version":"%s"}|,
202+
@req_vals ) =>
203+
qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
204+
sprintf(
205+
q|/clientresp / {"args":"[{\"autosize\":null,\"font\":{\"color\":\"rgb(84, 39, 143)\",\"family\":\"\\\\\"Avant Garde\\\\\", Avantgarde, \\\\\"Century Gothic\\\\\", CenturyGothic, \\\\\"AppleGothic\\\\\", sans-serif\",\"size\":20},\"height\":600,\"margin\":{\"b\":60,\"l\":70,\"pad\":2,\"r\":40,\"t\":60},\"paper_bgcolor\":\"rgb(188, 189, 220)\",\"plot_bgcolor\":\"rgb(158, 154, 200)\",\"showlegend\":null,\"title\":\"Damped Sinusoid\",\"titlefont\":{\"color\":\"rgb(84, 39, 143)\",\"family\":\"\\\\\"Avant Garde\\\\\", Avantgarde, \\\\\"Century Gothic\\\\\", CenturyGothic, \\\\\"AppleGothic\\\\\", sans-serif\",\"size\":25},\"width\":600}]","key":"%s","kwargs":"{\"filename\":\"plot from API (2)\",\"fileopt\":null}","origin":"layout","platform":"Perl","un":"%s","version":"%s"}|,
206+
@req_vals ) =>
207+
qq[{"url": "https://plot.ly/~$user->{un}/2", "message": "High five! You successfuly sent some data to your account on plotly. View your plot in your browser at https://plot.ly/~$user->{un}/2 or inside your plot.ly account where it is named 'plot from API (2)'", "warning": "", "filename": "plot from API (2)", "error": ""}],
208+
sprintf(
209+
q|/clientresp / {"args":"[{}]","key":"%s","kwargs":"{\"filename\":\"plot from API (2)\",\"fileopt\":null}","origin":"layout","platform":"Perl","un":"%s","version":"%s"}|,
210+
@req_vals ) =>
211+
qq[{"url": "", "message": "", "warning": "", "filename": "", "error": "Traceback (most recent call last):\\n File \\"/home/jp/dj/shelly/remote/remoteviews.py\\", line 311, in clientresp\\n grph = {'layout': args[0]}\\nKeyError: 0\\n"}],
199212
);
200213
return sub {
201214
my ( $self, $request ) = @_;
202215

203-
my $req_string = $request->uri->path . " / " . $request->content;
216+
my $url = URI->new( 'http:' );
217+
$url->query( $request->content );
218+
my %form = $url->query_form;
219+
my $content = JSON->new->utf8->convert_blessed( 1 )->canonical( 1 )->encode( \%form );
220+
221+
my $req_string = $request->uri->path . " / " . $content;
204222

205223
die "unknown request: " . $req_string if !$pairs{$req_string};
206224

0 commit comments

Comments
 (0)