2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-09 20:57:11 +00:00

98_HTTPMOD: added tests

git-svn-id: https://svn.fhem.de/fhem/trunk@22636 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
StefanStrobel 2020-08-20 15:51:14 +00:00
parent 5bf0cf2c72
commit 48227c3fc4
19 changed files with 424 additions and 0 deletions

View File

@ -0,0 +1,5 @@
define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/10_Redirects.testdata 0
attr H2 verbose 5
attr H2 fileHeaderSplit --end-of-http-header--
attr H2 queueDelay 2
attr H2 minSendDelay 2

View File

@ -0,0 +1,15 @@
##############################################
# test redirects
##############################################
use strict;
use warnings;
use Test::More;
fhem('set H2 reread');
is(FhemTestUtils_gotLog("AddToQueue prepends type update to URL http://test.url/"), 1, "Match redirected url");
done_testing;
exit(0);
1;

View File

@ -0,0 +1,13 @@
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Content-Length: 162
Connection: close
Location: http://test.url/
--end-of-http-header--
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

View File

@ -0,0 +1,3 @@
define H1 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0
attr H1 extractAllJSON 1
attr H1 verbose 5

View File

@ -0,0 +1,19 @@
##############################################
# test extractAllReadings
##############################################
use strict;
use warnings;
use Test::More;
fhem('set H1 reread');
InternalTimer(time()+1, sub() {
is(FhemTestUtils_gotLog("Read response matched 24"), 1, "Match 24 Readings log");
is(FhemTestUtils_gotEvent("H1:MQTT_ip_1: 168"), 1, "Reading creation 1");
is(FhemTestUtils_gotEvent("H1:modes_2: RainbowChase"), 1, "Reading creation 2");
done_testing;
exit(0);
}, 0);
1;

View File

@ -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

View File

@ -0,0 +1,55 @@
##############################################
# test Expressions in config
##############################################
use strict;
use warnings;
use Test::More;
use FHEM::HTTPMOD::Utils qw(:all);
fhem('attr H1 verbose 5');
fhem('attr H1 get02IExpr $vale');
is(FhemTestUtils_gotLog('attr H1 get02IExpr \$vale : Invalid Expression'), 1, "perl syntax check in attrs");
fhem('get H1 TestGet');
is(FhemTestUtils_gotLog("with timeout 2 to file://t/FHEM/98_HTTPMOD/JSON.testdata"), 1, "URL Expression log");
is(FhemTestUtils_gotLog("header Content-Type: application/json345"), 1, "Header expression in log");
is(FhemTestUtils_gotLog(", data Post Data for Test567"), 1, "Post Data expression in log");
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('set H2 TestSet1 4');
is(FhemTestUtils_gotLog("TestSet1 PostData 8"), 1, "set IExpr1 to Post Data in log");
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;

View File

@ -0,0 +1,67 @@
define H1 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0
attr H1 verbose 5
attr H1 minSendDelay 0
attr H1 regexDecode none
attr H1 regexCompile 1
attr H1 reading01Name TestReading1
attr H1 reading01Regex \"SimpleColor\",\"([^\"]+)\"
attr H1 reading02Name TestReading2
attr H1 reading02Regex \" SimpleColor \" ,\" ([^\"]+) \"
attr H1 reading02RegOpt xms
attr H1 reading03Name TestReading3
attr H1 reading03Regex \" SimpleColor \" ,\" ([^\"]+) \"
attr H1 reading10Name TestReading4
attr H1 reading10Regex code . \" : (\d+)
attr H1 reading10RegOpt gxms
attr H1 reading10RecombineExpr join ' ', @matchlist
define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0
attr H2 verbose 5
attr H2 minSendDelay 0
attr H2 regexDecode none
attr H2 regexCompile 0
attr H2 reading01Name TestReading1
attr H2 reading01Regex \"SimpleColor\",\"([^\"]+)\"
attr H2 reading02Name TestReading2
attr H2 reading02Regex \" SimpleColor \" ,\" ([^\"]+) \"
attr H2 reading02RegOpt xms
attr H2 reading03Name TestReading3
attr H2 reading03Regex \" SimpleColor \" ,\" ([^\"]+) \"
attr H2 reading10Name TestReading4
attr H2 reading10Regex code . \" : (\d+)
attr H2 reading10RegOpt gxms
attr H2 reading10RecombineExpr join ' ', @matchlist
define H3 HTTPMOD file://t/FHEM/98_HTTPMOD/JSON.testdata 0
attr H3 verbose 3
attr H3 reading01Name TestReading
attr H3 reading01JSON MQTT_ip_1
attr H3 preProcessRegex s/168/466/g
define H4 HTTPMOD file://t/FHEM/98_HTTPMOD/Login.testdata 0
attr H4 verbose 3
attr H4 minSendDelay 0
attr H4 queueDelay 0
attr H4 showMatched 1
attr H4 authRetries 1
attr H4 reAuthRegex Login required
attr H4 sid01URL file://t/FHEM/98_HTTPMOD/JSON.testdata
attr H4 sid02URL file://t/FHEM/98_HTTPMOD/JSON3.testdata
attr H4 idRegex session ([\d]+)
#attr H4 dontRequeueAfterAuth 1
attr H4 reading01Name TestReading
attr H4 reading01JSON MQTT_ip_1

View File

@ -0,0 +1,49 @@
##############################################
# test regexes
#
# readingXRegex
# preProcess
# reAuth
# idRegex
# replacementRegex
#
# reading|get|set regOpt (gceor are not compiled in, xmsi need to be in compilation)
#
# decode and compile
##############################################
use strict;
use warnings;
use Test::More;
fhem('set H1 reread');
is(FhemTestUtils_gotEvent(qr/H1:TestReading1:\sRainbowChase/xms), 1, "match simple case with regex compilation");
is(FhemTestUtils_gotEvent(qr/H1:TestReading2:\sRainbowChase/xms), 1, "match with options xms with regex compilation");
is(FhemTestUtils_gotLog(qr/H1:.*TestReading3 did not match/), 1, "No match with wrong options with regex compilation");
is(FhemTestUtils_gotEvent(qr/H1:TestReading4:\s3\s4/xms), 1, "match with options gxms with regex compilation");
fhem('set H2 reread');
is(FhemTestUtils_gotEvent(qr/H2:TestReading1:\sRainbowChase/xms), 1, "match simple case without regex compilation");
is(FhemTestUtils_gotEvent(qr/H2:TestReading2:\sRainbowChase/xms), 1, "match with options xms without regex compilation");
is(FhemTestUtils_gotLog(qr/H2:.*TestReading3 did not match/), 1, "No match with wrong options without regex compilation");
is(FhemTestUtils_gotEvent(qr/H2:TestReading4:\s3\s4/xms), 1, "match with options gxms without regex compilation");
fhem ('attr H2 reading20Name TestReadingBad');
fhem ('attr H2 reading20Regex \"SimpleColor\",\"[^\"]+)\"');
is(FhemTestUtils_gotLog(qr/H2: reading20Regex Regex: Bad regexp/), 2, "validation of a bad regex");
fhem('set H3 reread');
is(FhemTestUtils_gotEvent(qr/H3:TestReading:\s466/xms), 1, "preProcessRegex");
fhem('set H4 reread');
InternalTimer(time()+1, sub() {
is(FhemTestUtils_gotEvent("H4:LAST_REQUEST: auth01"), 1, "Auth Step 1");
is(FhemTestUtils_gotEvent("H4:LAST_REQUEST: auth02"), 1, "Auth Step 2");
is(FhemTestUtils_gotEvent("H4:TestReading: 168"), 1, "Reading after auth");
done_testing;
exit(0);
}, 0);
1;

View File

@ -0,0 +1,32 @@
define H1 HTTPMOD none 0
attr H1 verbose 5
attr H1 minSendDelay 0
attr H1 reading01Name TestReading1
attr H1 reading01JSON MQTT_ip_1
attr H1 reading02Name TestReading2
attr H1 reading02JSON modes
attr H1 readingMaxAge 0.1
attr H1 readingMaxAgeReplacement outdated
attr H1 readingMaxAgeReplacementMode text
attr H1 reading02MaxAgeReplacement "old - was $val"
attr H1 reading02MaxAgeReplacementMode expression
attr H1 reading02-2MaxAgeReplacement tr
attr H1 reading02-2MaxAgeReplacementMode reading
attr H1 reading02-3MaxAgeReplacement NAME
attr H1 reading02-3MaxAgeReplacementMode internal
attr H1 reading02-4MaxAgeReplacement 0
attr H1 reading02-4MaxAgeReplacementMode delete
attr H1 get01URL file://t/FHEM/98_HTTPMOD/JSON.testdata
attr H1 get01Name G1
attr H1 get02URL file://t/FHEM/98_HTTPMOD/empty.testdata
attr H1 get02Name G2

View File

@ -0,0 +1,26 @@
##############################################
# test MaxAge
##############################################
use strict;
use warnings;
use Test::More;
fhem('get H1 G1');
is(FhemTestUtils_gotEvent("H1:TestReading1: 168"), 1, "Normal Reading 1");
is(FhemTestUtils_gotEvent("H1:TestReading2-1: Off"), 1, "Normal Reading 2");
sleep 0.15;
fhem('setreading H1 tr 789');
fhem('get H1 G2');
is(FhemTestUtils_gotEvent("H1:TestReading1: outdated"), 1, "Outdated Reading 1 with mode text");
is(FhemTestUtils_gotEvent("H1:TestReading2-1: old - was Off"), 1, "Outdated Reading 2 with mode expression");
is(FhemTestUtils_gotEvent("H1:TestReading2-2: 789"), 1, "Outdated Reading 3 with mode reading");
is(FhemTestUtils_gotEvent("H1:TestReading2-3: H1"), 1, "Outdated Reading 4 with mode internal");
is(FhemTestUtils_gotEvent("H1:TestReading2-4:"), 1, "Outdated Reading 5 with mode delete");
done_testing;
exit(0);
1;

View File

@ -0,0 +1,9 @@
define H2 HTTPMOD file://t/FHEM/98_HTTPMOD/Charset.testdata 0
attr H2 verbose 3
attr H2 fileHeaderSplit --end-of-http-header--
attr H2 memReading 1
attr H2 minSendDelay 0
attr H2 reading01Name TestReading1
attr H2 reading01Regex erraschung mit (.*)

View File

@ -0,0 +1,22 @@
##############################################
# test other small features
##############################################
use strict;
use warnings;
use Test::More;
fhem('attr H2 bodyDecode none');
fhem('set H2 reread');
is(FhemTestUtils_gotEvent("H2:Fhem_Mem"), 1, "memReading");
is(FhemTestUtils_gotEvent("H2:TestReading1: \x8e\x6e"), 1, "TestReading without bodyDecode");
fhem('attr H2 bodyDecode auto');
fhem('set H2 reread');
is(FhemTestUtils_gotEvent("H2:TestReading1: \xc4\x6e"), 1, "TestReading with body decode");
done_testing;
exit(0);
1;

View File

@ -0,0 +1,13 @@
HTTP/1.1 200 OK
Content-Type: text/html; charset=cp437
--end-of-http-header--
<html>
<head><title>Test</title></head>
<body>
<center><h1>Test Data</h1></center>
<hr><center>for HTTPMOD Test</center>
šberraschung mit Žnderungen ‚Š
</body>
</html>

View File

@ -0,0 +1,17 @@
{"settings":{"currentpreset":255,"effect":"Off"},
"device":"tvlights",
"heap":20864,
"power":"0",
"modes":["Off","SimpleColor","RainbowChase","BobblySquares","Blobs","CuriousCat","Adalight","UDP","DMX"],
"currentpresets":[],
"code1":3,
"code2":4,
"pixels":118,
"MQTT":
{"enable":true,
"ip":[192,168,1,24],
"port":1883
},
"timer":{"running":false}
}

View File

@ -0,0 +1,17 @@
{"settings":{"currentpreset":255,"effect":"Off"},
"device":"testdevice",
"heap":1122,
"power":"0",
"modes":["Off","SimpleColor","RainbowChase","BobblySquares","Blobs","CuriousCat","Adalight","UDP","DMX"],
"currentpresets":[],
"code1":3,
"code2":4,
"pixels":118,
"MQTT":
{"enable":true,
"ip":[192,168,1,24],
"port":1883
},
"timer":{"running":false}
}

View File

@ -0,0 +1,17 @@
{"settings":{"currentpreset":255,"effect":"Off"},
"device":"testdevice",
"heap":1122,
"power":"0",
"modes":["Off","Step2","RainbowChase","session 55","Blobs","CuriousCat","Adalight","UDP","DMX"],
"currentpresets":[],
"code1":3,
"code2":4,
"pixels":118,
"MQTT":
{"enable":true,
"ip":[192,168,1,24],
"port":1883
},
"timer":{"running":false}
}

View File

@ -0,0 +1,17 @@
{"settings":{"currentpreset":255,"effect":"Off"},
"device":"testdevice",
"heap":1122,
"power":"0",
"modes":["Off","Login required","RainbowChase","BobblySquares","Blobs","CuriousCat","Adalight","UDP","DMX"],
"currentpresets":[],
"code1":3,
"code2":4,
"pixels":118,
"MQTT":
{"enable":true,
"ip":[192,168,1,24],
"port":1883
},
"timer":{"running":false}
}

View File