From 5555f28bd33fb1b0e2ad9a5253e91f519892ca66 Mon Sep 17 00:00:00 2001 From: StefanStrobel <> Date: Sat, 29 Aug 2020 16:17:28 +0000 Subject: [PATCH] 98_HTTPMOD: updated tests git-svn-id: https://svn.fhem.de/fhem/trunk@22691 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg | 28 +++++++++++++++++ fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t | 28 +++++++++++++++++ fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg | 11 +++++++ fhem/t/FHEM/98_HTTPMOD/50_Replacements.t | 18 +++++++++++ fhem/t/FHEM/98_HTTPMOD/99_evalExpr.cfg | 4 +++ fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t | 36 ++++++++++++++++++++++ 6 files changed, 125 insertions(+) create mode 100644 fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg create mode 100644 fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t create mode 100644 fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg create mode 100644 fhem/t/FHEM/98_HTTPMOD/50_Replacements.t create mode 100644 fhem/t/FHEM/98_HTTPMOD/99_evalExpr.cfg create mode 100644 fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t diff --git a/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg new file mode 100644 index 000000000..3a03a1658 --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg @@ -0,0 +1,28 @@ +define H1 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON 0 +attr H1 verbose 3 +attr H1 get01Name TestGet +attr H1 get01Data Post Data for Test +attr H1 get01Header1 Content-Type: application/json +attr H1 get01URLExpr $old . '.testdata' +attr H1 get01HdrExpr $old . '345' +attr H1 get01DatExpr $old . '567' + +define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0 +attr H2 requestHeader1 Content-Type: Test-Content +attr H2 requestHeader2 TestHeader: T1E2S3T +attr H2 verbose 5 +attr H2 minSendDelay 0 +attr H2 reading01Name TestReading +attr H2 reading01JSON MQTT_ip_1 +attr H2 reading01OExpr $val * 2 + +attr H2 reading02Name TestReading2 +attr H2 reading02JSON modes + +attr H2 reading03Name CombReading +attr H2 reading03JSON modes +attr H2 reading03RecombineExpr join ' ', @matchlist + +attr H2 set01Name TestSet1 +attr H2 set01Data TestSet1 PostData $val +attr H2 set01IExpr $val * 2 diff --git a/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t new file mode 100644 index 000000000..8ead7d78d --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t @@ -0,0 +1,28 @@ +################################################ +# test Expressions with JSON readings in config +################################################ +use strict; +use warnings; +use Test::More; + +eval "use JSON"; +if ($@) { + plan skip_all => "This test checks an optional JSON-Feature of HTTPMOD and can only be run with the JSON library installed. Please install JSON Library (apt-get install libjson-perl)"; +} else { + plan tests => 5; +} + +fhem('set H2 reread'); +is(FhemTestUtils_gotEvent(qr/H2:TestReading:\s336/xms), 1, "JSON Reading creation with OExpr Expression"); +is(FhemTestUtils_gotEvent("H2:TestReading2-8: UDP"), 1, "JSON multiple Reading creation"); +is(FhemTestUtils_gotEvent("H2:CombReading: Off SimpleColor RainbowChase"), 1, "Reading recombine expresion"); +is(FhemTestUtils_gotLog(qr/HandleSendQueue\ssends\supdate.*header:\sContent-Type:\sTest-Content.*TestHeader:\sT1E2S3T/xms), 1, "requestHeader"); + +fhem('attr H1 verbose 5'); +fhem('set H2 TestSet1 4'); +is(FhemTestUtils_gotLog("TestSet1 PostData 8"), 1, "set IExpr1 to Post Data in log"); + +done_testing; +exit(0); + +1; diff --git a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg new file mode 100644 index 000000000..ce596513b --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg @@ -0,0 +1,11 @@ +defmod GeoTest HTTPMOD none 0 +attr GeoTest verbose 3 +attr GeoTest enableControlSet 1 +attr GeoTest get01Name Tag01 +attr GeoTest get01Poll 1 +attr GeoTest get01Regex (?s)Zeit auswählen
(?[A-Za-z]+).-.(?[0-9\.]{10}).*?09:15<\/td>(?.*?)<\/td>.*?12:15<\/td>(?.*?)<\/td> +attr GeoTest get01Replacement01Value {strftime("%d.%m.%Y", localtime( time))} +attr GeoTest getURL file://geocache-planer.de/CAL/anmeldung.php?date=%%date%%&CALID=XYZ +attr GeoTest replacement01Mode expression +attr GeoTest replacement01Regex %%date%% + diff --git a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t new file mode 100644 index 000000000..57cd1b07d --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t @@ -0,0 +1,18 @@ +############################################## +# test replacements +# +############################################## +use strict; +use warnings; +use Test::More; + +fhem('set GeoTest reread'); + + +InternalTimer(time()+1, sub() { + is(FhemTestUtils_gotLog(qr/Read callback: Error: geocache\-planer.*date=[\d]+\.\d+\.\d+/), 1, "Expr replacement in URL"); + done_testing; + exit(0); +}, 0); + +1; diff --git a/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.cfg b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.cfg new file mode 100644 index 000000000..1d099cc00 --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.cfg @@ -0,0 +1,4 @@ +define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON 0 +attr H2 verbose 3 +attr H2 get01Name TestGet +attr H2 get01Data Post Data for Test diff --git a/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t new file mode 100644 index 000000000..556b03b98 --- /dev/null +++ b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t @@ -0,0 +1,36 @@ +############################################## +# test evalExpr Util function +############################################## +use strict; +use warnings; +use Test::More; + +use_ok ('FHEM::HTTPMOD::Utils', qw(:all)); + +my $hash = $defs{'H2'}; +my $name = 'H2'; +my $val = 5; +my @array = (1,2,3); +my %tHash = (a => 10, b => 20); +my $exp = '$val * 2'; + +my $result = EvalExpr($hash, $exp, {'$val' => $val, '@array' => \@array}); +#Log3 $name, 3, "$name: result of EvalExpr test 1 = $result"; +is $result, 10, "simple expression with one scalar in list"; + +$exp = '$array[1] * 2'; +$result = EvalExpr($hash, $exp, {'$val' => $val, '@array' => \@array}); +is $result, 4, "simple expression with array ref in hash"; + +$exp = '$hash{a} * 2'; +$result = EvalExpr($hash, $exp, {'$val' => $val, '%hash' => \%tHash}); +is $result, 20, "simple expression with hash ref in hash"; + +$exp = '$hash->{a} * 2'; +$result = EvalExpr($hash, $exp, {'$val' => $val, '$hash' => \%tHash}); +is $result, 20, "simple expression with hash ref as ref in hash"; + +done_testing; +exit(0); + +1;