From b8b65937856dfc2090bdaf39c76d8c4af6310010 Mon Sep 17 00:00:00 2001 From: StefanStrobel <> Date: Fri, 12 Jul 2024 19:24:17 +0000 Subject: [PATCH] 98_HTTPMOD: extended test files git-svn-id: https://svn.fhem.de/fhem/trunk@29022 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/t/FHEM/98_HTTPMOD/10_Redirects.t | 1 + fhem/t/FHEM/98_HTTPMOD/32_SetExpr.cfg | 4 ++++ fhem/t/FHEM/98_HTTPMOD/32_SetExpr.t | 11 ++++++++-- fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg | 2 ++ fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t | 16 ++++++++++---- fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg | 25 ++++++++-------------- fhem/t/FHEM/98_HTTPMOD/50_Replacements.t | 24 +++++++++++++++------ fhem/t/FHEM/98_HTTPMOD/90_SmallFeatures.t | 8 +++++-- fhem/t/FHEM/98_HTTPMOD/91_ErrLogLvl.t | 10 ++++----- fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t | 9 +++++++- 10 files changed, 74 insertions(+), 36 deletions(-) diff --git a/fhem/t/FHEM/98_HTTPMOD/10_Redirects.t b/fhem/t/FHEM/98_HTTPMOD/10_Redirects.t index a4ed34bef..49b4a05f5 100644 --- a/fhem/t/FHEM/98_HTTPMOD/10_Redirects.t +++ b/fhem/t/FHEM/98_HTTPMOD/10_Redirects.t @@ -1,5 +1,6 @@ ############################################## # test redirects +# perl /opt/fhem/fhem.pl -t /opt/fhem/t/... ############################################## use strict; use warnings; diff --git a/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.cfg b/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.cfg index dff30e5f9..cda22dca5 100644 --- a/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.cfg +++ b/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.cfg @@ -3,6 +3,10 @@ define H1 HTTPMOD none 0 attr H1 set01Name Msg attr H1 set01TextArg 1 attr H1 set01URL https://api.testurl.local/send?text=$val + +attr H1 set02Name S2 +attr H1 set02URL https://api.testurl.local/send?text=$val&v1=$val1&v2=$val2&v3=$val3 + attr H1 verbose 5 attr H1 timeout 0.1 attr H1 queueDelay 0 diff --git a/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.t b/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.t index 674e467fc..5a364d748 100644 --- a/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.t +++ b/fhem/t/FHEM/98_HTTPMOD/32_SetExpr.t @@ -12,7 +12,7 @@ $modVersion =~ /^([0-9]+)\./; my $major = $1; if ($major && $major >= 4) { - plan tests => 2; + plan tests => 3; } else { plan skip_all => "This test only works for HTTPMOD version 4 or later, installed is $modVersion"; } @@ -23,7 +23,14 @@ FhemTestUtils_resetLogs(); fhem 'attr H1 set01IExpr $val =~ s/\s/%20/g;; $val;'; fhem 'set H1 Msg Hallo Du da'; -is(FhemTestUtils_gotLog("HandleSendQueue sends set01 with timeout.*text=Hallo%20Du%20da"), 1, "send normal request"); +is(FhemTestUtils_gotLog("HandleSendQueue sends set01 with timeout.*text=Hallo%20Du%20da"), 1, "send S1 request"); +FhemTestUtils_resetLogs(); + + +fhem 'attr H1 set02ValueSeparator ,'; +#fhem 'attr H1 set02TextArg ,'; +fhem 'set H1 S2 1,22,333'; +is(FhemTestUtils_gotLog("HandleSendQueue sends .*v1=1&v2=22&v3=333"), 1, "send S2 request"); FhemTestUtils_resetLogs(); diff --git a/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg index d934d0cba..046809603 100644 --- a/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg +++ b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.cfg @@ -6,6 +6,7 @@ attr H1 get01Header1 Content-Type: application/json attr H1 get01URLExpr $old . '.testdata' attr H1 get01HdrExpr $old . '345' attr H1 get01DatExpr $old . '567' +attr H1 get01JSON modes define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0 attr H2 requestHeader1 Content-Type: Test-Content @@ -26,3 +27,4 @@ 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 index 8ead7d78d..b7be7eb85 100644 --- a/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t +++ b/fhem/t/FHEM/98_HTTPMOD/36_JSONExpr.t @@ -4,24 +4,32 @@ use strict; use warnings; use Test::More; +use FHEM::Modbus::TestUtils qw(:all); 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; + plan tests => 6; } -fhem('set H2 reread'); +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'); +fhem 'set H2 TestSet1 4'; is(FhemTestUtils_gotLog("TestSet1 PostData 8"), 1, "set IExpr1 to Post Data in log"); + +fhem 'attr H1 verbose 5'; +fhem 'attr H1 get01-1Name Mode1'; +fhem 'attr H1 get01-1OExpr $val . "add"'; +fhem 'attr H1 get01-2Name Mode2'; +fhem 'get H1 TestGet'; + +CheckAndReset(); done_testing; exit(0); diff --git a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg index bbeedd23a..2d3cbd09e 100644 --- a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg +++ b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.cfg @@ -1,18 +1,11 @@ -define GeoTest HTTPMOD none 0 -attr GeoTest minSendDelay 0 -attr GeoTest verbose 5 -attr GeoTest enableControlSet 1 +define H HTTPMOD none 0 +attr H minSendDelay 0 +attr H verbose 5 +attr H enableControlSet 1 -attr GeoTest replacement01Mode expression -attr GeoTest replacement01Regex %%date%% +attr H replacement01Mode expression +attr H replacement01Regex %%date%% +attr H get01Name get01 +attr H get01Regex +attr H getURL file://JSON.testdata -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 getURL file://geocache-planer.de/CAL/anmeldung.php?date=%%date%%&CALID=XYZ -attr GeoTest get01Replacement01Value {strftime("%d.%m.%Y", localtime( time))} - -attr GeoTest get02Name Test2 -#attr GeoTest get02Replacement01Value {my $NAME = "GeoTest" ;;fhem("deletereading ".$NAME." message");;fhem("deletereading ".$NAME." auth.*");;my @chars=('a'..'z','A'..'Z','0'..'9'); my $r; foreach(1..16) {$r.=$chars[rand @chars];};; fhem("setreading ".$NAME." auth_randomString64 ".$r);; $r;;} -attr GeoTest get02Replacement01Value {my $NAME = "GeoTest" ;;fhem("deletereading ".$NAME." message");;fhem("deletereading ".$NAME." auth.*");;my @chars=('a'..'z','A'..'Z','0'..'9');; my $r;; foreach(1..16) {$r.=$chars[rand @chars];;};; fhem("setreading ".$NAME." auth_randomString64 ".$r);; $r;;} -attr GeoTest get02Poll 1 diff --git a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t index 57cd1b07d..c5f48ae5f 100644 --- a/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t +++ b/fhem/t/FHEM/98_HTTPMOD/50_Replacements.t @@ -5,14 +5,26 @@ use strict; use warnings; use Test::More; +use FHEM::Modbus::TestUtils qw(:all); -fhem('set GeoTest reread'); +NextStep(); +sub testStep1 { + fhem 'attr H verbose 5'; + #fhem 'attr H getReplacement01Value {"new"."val"}'; + fhem 'attr H get01Replacement01Value {"new"."val" err}'; + #is(FhemTestUtils_gotLog('3 : Invalid Expression'), 1, "found invalid expression"); + is(FhemTestUtils_gotLog('3\s*:\s*Invalid Expression'), 1, "found invalid expression"); + fhem 'attr H get01Replacement01Value {"new"."val"}'; + CheckAndReset(); + fhem 'attr H get01Data01 header%%date%%'; + fhem 'get H get01'; + return 0; +} -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); +sub testStep2 { + LogStep "check results"; + is(FhemTestUtils_gotLog('data: headernewval'), 1, "did replacement"); +} 1; diff --git a/fhem/t/FHEM/98_HTTPMOD/90_SmallFeatures.t b/fhem/t/FHEM/98_HTTPMOD/90_SmallFeatures.t index 0eb34d581..17a963e53 100644 --- a/fhem/t/FHEM/98_HTTPMOD/90_SmallFeatures.t +++ b/fhem/t/FHEM/98_HTTPMOD/90_SmallFeatures.t @@ -12,7 +12,7 @@ $modVersion =~ /^([0-9]+)\./; my $major = $1; if ($major && $major >= 4) { - plan tests => 11; + plan tests => 13; } else { plan skip_all => "This test only works for HTTPMOD version 4 or later, installed is $modVersion"; } @@ -28,13 +28,17 @@ SKIP: { is(FhemTestUtils_gotEvent("H2:TestReading1: \x8e\x6e"), 1, "TestReading without bodyDecode"); +CheckAndReset(); +fhem('attr H2 bodyDecode illegal123'); +fhem('attr H2 readingEncode nonsense456'); +is(FhemTestUtils_gotLog("encoding.*is not valid"), 4, "encodings validated"); + CheckAndReset(); fhem('attr H2 bodyDecode auto'); fhem('attr H2 readingEncode utf8'); fhem('set H2 reread'); is(FhemTestUtils_gotEvent("H2:TestReading1: \xc3\x84\x6e"), 1, "TestReading with body decode"); - CheckAndReset(); fhem('attr H2 bodyDecode UTF8'); fhem('attr H2 readingEncode utf8'); diff --git a/fhem/t/FHEM/98_HTTPMOD/91_ErrLogLvl.t b/fhem/t/FHEM/98_HTTPMOD/91_ErrLogLvl.t index 4c87c82d4..8f2336c76 100644 --- a/fhem/t/FHEM/98_HTTPMOD/91_ErrLogLvl.t +++ b/fhem/t/FHEM/98_HTTPMOD/91_ErrLogLvl.t @@ -24,8 +24,8 @@ sub testStep1 { } sub testStep2 { - # loginform used fot FhemTestUtils inserts a space between the level and : - is(FhemTestUtils_gotLog('3 : H1: Read callback: Error'), 1, "standard log level"); + # loginform used for FhemTestUtils inserts a space between the level and : + is(FhemTestUtils_gotLog('3\s*:\s*H1: Read callback: Error'), 1, "standard log level"); FhemTestUtils_resetLogs(); FhemTestUtils_resetEvents(); LogStep "set level go 4"; @@ -35,7 +35,7 @@ sub testStep2 { } sub testStep3 { - is(FhemTestUtils_gotLog('4 : H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "log level 4"); + is(FhemTestUtils_gotLog('4\s*:\s*H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "log level 4"); FhemTestUtils_resetLogs(); FhemTestUtils_resetEvents(); LogStep "set level regex to timeout only"; @@ -46,7 +46,7 @@ sub testStep3 { } sub testStep4 { - is(FhemTestUtils_gotLog('3 : H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "standard log level because regex doesnt match"); + is(FhemTestUtils_gotLog('3\s*:\s*H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "standard log level because regex doesnt match"); FhemTestUtils_resetLogs(); FhemTestUtils_resetEvents(); LogStep "set level regex to no such file"; @@ -57,7 +57,7 @@ sub testStep4 { } sub testStep5 { - is(FhemTestUtils_gotLog('4 : H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "log level 4 with match"); + is(FhemTestUtils_gotLog('4\s*:\s*H1: Read callback: Error: t/FHEM/98_HTTPMOD/NoSuchFile'), 1, "log level 4 with match"); FhemTestUtils_resetLogs(); FhemTestUtils_resetEvents(); return; diff --git a/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t index 807c2934f..adbf8562e 100644 --- a/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t +++ b/fhem/t/FHEM/98_HTTPMOD/99_evalExpr.t @@ -37,7 +37,14 @@ is $result, undef, "FormatVal with empty format and undef value"; $format = '%.2f'; $result = FormatVal($hash, {val => $val, format => $format}); -is $result, "0.00", "FormatVal with empty format and undef value"; +is $result, "0.00", "FormatVal with %.2f format and undef value"; + +# mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = -1) +my $time = mktime(0,10,14,1,2,122,0,0,-1); # 1.3.2022 14:10 +$result = FmtDateTimeNice ($time); +Log3 $name, 1, "result time formatted is $result"; +is $result, "Di 1.Mar 2022 14:10", "FmtDateTimeNice"; + done_testing; exit(0);