2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2024-11-21 20:49:51 +00:00

98_HTTPMOD: extended test files

git-svn-id: https://svn.fhem.de/fhem/trunk@29022 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
StefanStrobel 2024-07-12 19:24:17 +00:00
parent 49e97058e7
commit b8b6593785
10 changed files with 74 additions and 36 deletions

View File

@ -1,5 +1,6 @@
############################################## ##############################################
# test redirects # test redirects
# perl /opt/fhem/fhem.pl -t /opt/fhem/t/...
############################################## ##############################################
use strict; use strict;
use warnings; use warnings;

View File

@ -3,6 +3,10 @@ define H1 HTTPMOD none 0
attr H1 set01Name Msg attr H1 set01Name Msg
attr H1 set01TextArg 1 attr H1 set01TextArg 1
attr H1 set01URL https://api.testurl.local/send?text=$val 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 verbose 5
attr H1 timeout 0.1 attr H1 timeout 0.1
attr H1 queueDelay 0 attr H1 queueDelay 0

View File

@ -12,7 +12,7 @@ $modVersion =~ /^([0-9]+)\./;
my $major = $1; my $major = $1;
if ($major && $major >= 4) { if ($major && $major >= 4) {
plan tests => 2; plan tests => 3;
} else { } else {
plan skip_all => "This test only works for HTTPMOD version 4 or later, installed is $modVersion"; 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 'attr H1 set01IExpr $val =~ s/\s/%20/g;; $val;';
fhem 'set H1 Msg Hallo Du da'; 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(); FhemTestUtils_resetLogs();

View File

@ -6,6 +6,7 @@ attr H1 get01Header1 Content-Type: application/json
attr H1 get01URLExpr $old . '.testdata' attr H1 get01URLExpr $old . '.testdata'
attr H1 get01HdrExpr $old . '345' attr H1 get01HdrExpr $old . '345'
attr H1 get01DatExpr $old . '567' attr H1 get01DatExpr $old . '567'
attr H1 get01JSON modes
define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0 define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0
attr H2 requestHeader1 Content-Type: Test-Content attr H2 requestHeader1 Content-Type: Test-Content
@ -26,3 +27,4 @@ attr H2 reading03RecombineExpr join ' ', @matchlist
attr H2 set01Name TestSet1 attr H2 set01Name TestSet1
attr H2 set01Data TestSet1 PostData $val attr H2 set01Data TestSet1 PostData $val
attr H2 set01IExpr $val * 2 attr H2 set01IExpr $val * 2

View File

@ -4,24 +4,32 @@
use strict; use strict;
use warnings; use warnings;
use Test::More; use Test::More;
use FHEM::Modbus::TestUtils qw(:all);
eval "use JSON"; eval "use JSON";
if ($@) { 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)"; 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 { } 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(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:TestReading2-8: UDP"), 1, "JSON multiple Reading creation");
is(FhemTestUtils_gotEvent("H2:CombReading: Off SimpleColor RainbowChase"), 1, "Reading recombine expresion"); 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"); 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"); 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; done_testing;
exit(0); exit(0);

View File

@ -1,18 +1,11 @@
define GeoTest HTTPMOD none 0 define H HTTPMOD none 0
attr GeoTest minSendDelay 0 attr H minSendDelay 0
attr GeoTest verbose 5 attr H verbose 5
attr GeoTest enableControlSet 1 attr H enableControlSet 1
attr GeoTest replacement01Mode expression attr H replacement01Mode expression
attr GeoTest replacement01Regex %%date%% 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<br>(?<Tag01_WochenTag>[A-Za-z]+).-.(?<Tag01_Datum>[0-9\.]{10}).*?<td>09:15<\/td><td>(?<Tag01_09Uhr>.*?)<\/td>.*?<td>12:15<\/td><td>(?<Tag01_12Uhr>.*?)<\/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

View File

@ -5,14 +5,26 @@
use strict; use strict;
use warnings; use warnings;
use Test::More; 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() { sub testStep2 {
is(FhemTestUtils_gotLog(qr/Read callback: Error: geocache\-planer.*date=[\d]+\.\d+\.\d+/), 1, "Expr replacement in URL"); LogStep "check results";
done_testing; is(FhemTestUtils_gotLog('data: headernewval'), 1, "did replacement");
exit(0); }
}, 0);
1; 1;

View File

@ -12,7 +12,7 @@ $modVersion =~ /^([0-9]+)\./;
my $major = $1; my $major = $1;
if ($major && $major >= 4) { if ($major && $major >= 4) {
plan tests => 11; plan tests => 13;
} else { } else {
plan skip_all => "This test only works for HTTPMOD version 4 or later, installed is $modVersion"; 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"); 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(); CheckAndReset();
fhem('attr H2 bodyDecode auto'); fhem('attr H2 bodyDecode auto');
fhem('attr H2 readingEncode utf8'); fhem('attr H2 readingEncode utf8');
fhem('set H2 reread'); fhem('set H2 reread');
is(FhemTestUtils_gotEvent("H2:TestReading1: \xc3\x84\x6e"), 1, "TestReading with body decode"); is(FhemTestUtils_gotEvent("H2:TestReading1: \xc3\x84\x6e"), 1, "TestReading with body decode");
CheckAndReset(); CheckAndReset();
fhem('attr H2 bodyDecode UTF8'); fhem('attr H2 bodyDecode UTF8');
fhem('attr H2 readingEncode utf8'); fhem('attr H2 readingEncode utf8');

View File

@ -24,8 +24,8 @@ sub testStep1 {
} }
sub testStep2 { sub testStep2 {
# loginform used fot FhemTestUtils inserts a space between the level and : # loginform used for FhemTestUtils inserts a space between the level and :
is(FhemTestUtils_gotLog('3 : H1: Read callback: Error'), 1, "standard log level"); is(FhemTestUtils_gotLog('3\s*:\s*H1: Read callback: Error'), 1, "standard log level");
FhemTestUtils_resetLogs(); FhemTestUtils_resetLogs();
FhemTestUtils_resetEvents(); FhemTestUtils_resetEvents();
LogStep "set level go 4"; LogStep "set level go 4";
@ -35,7 +35,7 @@ sub testStep2 {
} }
sub testStep3 { 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_resetLogs();
FhemTestUtils_resetEvents(); FhemTestUtils_resetEvents();
LogStep "set level regex to timeout only"; LogStep "set level regex to timeout only";
@ -46,7 +46,7 @@ sub testStep3 {
} }
sub testStep4 { 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_resetLogs();
FhemTestUtils_resetEvents(); FhemTestUtils_resetEvents();
LogStep "set level regex to no such file"; LogStep "set level regex to no such file";
@ -57,7 +57,7 @@ sub testStep4 {
} }
sub testStep5 { 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_resetLogs();
FhemTestUtils_resetEvents(); FhemTestUtils_resetEvents();
return; return;

View File

@ -37,7 +37,14 @@ is $result, undef, "FormatVal with empty format and undef value";
$format = '%.2f'; $format = '%.2f';
$result = FormatVal($hash, {val => $val, format => $format}); $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; done_testing;
exit(0); exit(0);