1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HtaccessCapabilityTester\Testers\Helpers; |
4
|
|
|
|
5
|
|
|
use \HtaccessCapabilityTester\HttpResponse; |
6
|
|
|
use \HtaccessCapabilityTester\TestResult; |
7
|
|
|
use \HtaccessCapabilityTester\Testers\AbstractTester; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class for interpreting responses using a defined interpretation table. |
11
|
|
|
* |
12
|
|
|
* @package HtaccessCapabilityTester |
13
|
|
|
* @author Bjørn Rosell <[email protected]> |
14
|
|
|
* @since Class available since 0.7 |
15
|
|
|
*/ |
16
|
|
|
class ResponseInterpreter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Interpret a response using an interpretation table. |
21
|
|
|
* |
22
|
|
|
* @param HttpResponse $response |
23
|
|
|
* @param array $interpretationTable |
24
|
|
|
* |
25
|
|
|
* @return TestResult If there is no match, the test result will have status = false and |
26
|
|
|
* info = "no-match". |
27
|
|
|
*/ |
28
|
52 |
|
public static function interpret($response, $interpretationTable) |
29
|
|
|
{ |
30
|
52 |
|
foreach ($interpretationTable as $i => $entry) { |
31
|
|
|
// ie: |
32
|
|
|
// ['inconclusive', 'body', 'is-empty'], |
33
|
|
|
// ['failure', 'statusCode', 'equals', '500'] |
34
|
|
|
// ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'], |
35
|
|
|
|
36
|
52 |
|
$statusStr = $entry[0]; |
37
|
|
|
|
38
|
52 |
|
$status = null; |
39
|
52 |
|
switch ($statusStr) { |
40
|
52 |
|
case 'failure': |
41
|
31 |
|
$status = false; |
42
|
31 |
|
break; |
43
|
49 |
|
case 'inconclusive': |
44
|
21 |
|
$status = null; |
45
|
21 |
|
break; |
46
|
48 |
|
case 'success': |
47
|
48 |
|
$status = true; |
48
|
48 |
|
break; |
49
|
|
|
} |
50
|
|
|
|
51
|
52 |
|
if (!isset($entry[1])) { |
52
|
12 |
|
return new TestResult($status, ''); |
53
|
|
|
} |
54
|
|
|
|
55
|
52 |
|
$propertyToExamine = $entry[1]; |
56
|
52 |
|
$operator = $entry[2]; |
57
|
52 |
|
$arg1 = (isset($entry[3]) ? $entry[3] : ''); |
58
|
52 |
|
$arg2 = (isset($entry[4]) ? $entry[4] : ''); |
59
|
|
|
|
60
|
52 |
|
$val = ''; |
61
|
52 |
|
switch ($propertyToExamine) { |
62
|
52 |
|
case 'status-code': |
63
|
31 |
|
$val = $response->statusCode; |
64
|
31 |
|
break; |
65
|
35 |
|
case 'body': |
66
|
25 |
|
$val = $response->body; |
67
|
25 |
|
break; |
68
|
15 |
|
case 'headers': |
69
|
15 |
|
$val = $response->getHeadersHash(); |
70
|
15 |
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
52 |
|
$reason = $propertyToExamine . ' ' . $operator; |
74
|
52 |
|
if (isset($entry[3])) { |
75
|
52 |
|
$reason .= ' "' . implode('" "', array_slice($entry, 3)) . '"'; |
76
|
|
|
} |
77
|
52 |
|
if (($propertyToExamine == 'status-code') && ($operator == 'not-equals')) { |
78
|
19 |
|
$reason .= ' - it was: ' . $val; |
79
|
|
|
} |
80
|
52 |
|
$result = new TestResult($status, $reason); |
81
|
|
|
|
82
|
52 |
|
switch ($operator) { |
83
|
52 |
|
case 'is-empty': |
84
|
|
|
if ($val == '') { |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
break; |
88
|
52 |
|
case 'equals': |
89
|
41 |
|
if ($val == $arg1) { |
90
|
37 |
|
return $result; |
91
|
|
|
} |
92
|
23 |
|
break; |
93
|
29 |
|
case 'not-equals': |
94
|
19 |
|
if ($val != $arg1) { |
95
|
4 |
|
return $result; |
96
|
|
|
} |
97
|
17 |
|
break; |
98
|
22 |
|
case 'begins-with': |
99
|
3 |
|
if (strpos($val, $arg1) === 0) { |
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
3 |
|
break; |
103
|
19 |
|
case 'contains-key': |
104
|
2 |
|
if (isset($val[$arg1])) { |
105
|
1 |
|
return $result; |
106
|
|
|
} |
107
|
1 |
|
break; |
108
|
19 |
|
case 'not-contains-key': |
109
|
9 |
|
if (!isset($val[$arg1])) { |
110
|
7 |
|
return $result; |
111
|
|
|
} |
112
|
2 |
|
break; |
113
|
15 |
|
case 'contains-key-value': |
114
|
11 |
|
if (isset($val[$arg1]) && ($val[$arg1] == $arg2)) { |
115
|
3 |
|
return $result; |
116
|
|
|
} |
117
|
9 |
|
break; |
118
|
11 |
|
case 'not-contains-key-value': |
119
|
7 |
|
if (!isset($val[$arg1]) || ($val[$arg1] != $arg2)) { |
120
|
7 |
|
return $result; |
121
|
|
|
} |
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
} |
125
|
15 |
|
return new TestResult(null, 'no-match'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|