|
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
|
|
|
* Parse status string (failure | success | inconclusive) to bool|null. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $statusString (failure | success | inconclusive) |
|
23
|
|
|
* @return bool|null |
|
24
|
|
|
*/ |
|
25
|
71 |
|
private static function parseStatusString($statusString) |
|
26
|
|
|
{ |
|
27
|
71 |
|
$status = null; |
|
28
|
71 |
|
switch ($statusString) { |
|
29
|
71 |
|
case 'failure': |
|
30
|
46 |
|
$status = false; |
|
31
|
46 |
|
break; |
|
32
|
68 |
|
case 'inconclusive': |
|
33
|
35 |
|
$status = null; |
|
34
|
35 |
|
break; |
|
35
|
67 |
|
case 'success': |
|
36
|
67 |
|
$status = true; |
|
37
|
67 |
|
break; |
|
38
|
|
|
} |
|
39
|
71 |
|
return $status; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Evaluate condition (string examination) |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $val |
|
46
|
|
|
* @param string $operator (is-empty | equals | not-equals | begins-with) |
|
47
|
|
|
* @param string $arg1 (only required for some operators) |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
63 |
|
private static function evaluateConditionForString($operator, $val, $arg1) |
|
51
|
|
|
{ |
|
52
|
63 |
|
switch ($operator) { |
|
53
|
63 |
|
case 'is-empty': |
|
54
|
|
|
return ($val == ''); |
|
55
|
63 |
|
case 'equals': |
|
56
|
59 |
|
return ($val == $arg1); |
|
57
|
36 |
|
case 'not-equals': |
|
58
|
33 |
|
return ($val != $arg1); |
|
59
|
26 |
|
case 'begins-with': |
|
60
|
3 |
|
return (strpos($val, $arg1) === 0); |
|
61
|
|
|
} |
|
62
|
23 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Evaluate condition (hash examination) |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $val |
|
69
|
|
|
* @param string $operator (is-empty | equals | not-equals | begins-with) |
|
70
|
|
|
* @param string $arg1 (only required for some operators) |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
28 |
|
private static function evaluateConditionForHash($operator, $val, $arg1, $arg2) |
|
74
|
|
|
{ |
|
75
|
28 |
|
switch ($operator) { |
|
76
|
28 |
|
case 'contains-key': |
|
77
|
4 |
|
return (isset($val[$arg1])); |
|
78
|
28 |
|
case 'not-contains-key': |
|
79
|
21 |
|
return (!isset($val[$arg1])); |
|
80
|
22 |
|
case 'contains-key-value': |
|
81
|
22 |
|
return (isset($val[$arg1]) && ($val[$arg1] == $arg2)); |
|
82
|
15 |
|
case 'not-contains-key-value': |
|
83
|
15 |
|
return (!isset($val[$arg1]) || ($val[$arg1] != $arg2)); |
|
84
|
|
|
} |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Evaluate condition |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $val |
|
92
|
|
|
* @param string $valType (string | hash) |
|
93
|
|
|
* @param string $arg1 (only required for some operators) |
|
94
|
|
|
* @param string $arg2 (only required for some operators) |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
71 |
|
private static function evaluateCondition($operator, $valType, $val, $arg1, $arg2) |
|
98
|
|
|
{ |
|
99
|
71 |
|
if ($valType == 'string') { |
|
100
|
63 |
|
return self::evaluateConditionForString($operator, $val, $arg1); |
|
|
|
|
|
|
101
|
28 |
|
} elseif ($valType == 'hash') { |
|
102
|
28 |
|
return self::evaluateConditionForHash($operator, $val, $arg1, $arg2); |
|
103
|
|
|
} |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get property |
|
110
|
|
|
* |
|
111
|
|
|
* @param HttpResponse $response |
|
112
|
|
|
* @param string $property (status-code | body | headers) |
|
113
|
|
|
* |
|
114
|
|
|
* @return array|null [variable type, value] or null if invalid property |
|
115
|
|
|
*/ |
|
116
|
71 |
|
private static function getPropertyOnResponse($response, $property) |
|
117
|
|
|
{ |
|
118
|
71 |
|
switch ($property) { |
|
119
|
71 |
|
case 'status-code': |
|
120
|
45 |
|
return ['string', $response->statusCode]; |
|
121
|
54 |
|
case 'body': |
|
122
|
43 |
|
return ['string', $response->body]; |
|
123
|
28 |
|
case 'headers': |
|
124
|
28 |
|
return ['hash', $response->getHeadersHash()]; |
|
125
|
|
|
} |
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Interpret line. |
|
131
|
|
|
* |
|
132
|
|
|
* @param HttpResponse $response |
|
133
|
|
|
* @param array $line |
|
134
|
|
|
* |
|
135
|
|
|
* @return TestResult|null If the condition matches, a TestResult is returned, otherwise null |
|
136
|
|
|
*/ |
|
137
|
71 |
|
private static function interpretLine($response, $line) |
|
138
|
|
|
{ |
|
139
|
|
|
// ie: |
|
140
|
|
|
// ['inconclusive', 'body', 'is-empty'], |
|
141
|
|
|
// ['failure', 'statusCode', 'equals', '500'] |
|
142
|
|
|
// ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'], |
|
143
|
|
|
|
|
144
|
71 |
|
$status = self::parseStatusString($line[0]); |
|
145
|
|
|
|
|
146
|
71 |
|
if (!isset($line[1])) { |
|
147
|
24 |
|
return new TestResult($status, ''); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
71 |
|
$propertyToExamine = $line[1]; |
|
151
|
71 |
|
$operator = $line[2]; |
|
152
|
71 |
|
$arg1 = (isset($line[3]) ? $line[3] : ''); |
|
153
|
71 |
|
$arg2 = (isset($line[4]) ? $line[4] : ''); |
|
154
|
|
|
|
|
155
|
71 |
|
$valType = ''; |
|
|
|
|
|
|
156
|
71 |
|
list($valType, $val) = self::getPropertyOnResponse($response, $propertyToExamine); |
|
157
|
|
|
|
|
158
|
71 |
|
$reason = $propertyToExamine . ' ' . $operator; |
|
159
|
71 |
|
if (isset($line[3])) { |
|
160
|
71 |
|
$reason .= ' "' . implode('" "', array_slice($line, 3)) . '"'; |
|
161
|
|
|
} |
|
162
|
71 |
|
if (($propertyToExamine == 'status-code') && ($operator == 'not-equals')) { |
|
163
|
33 |
|
$reason .= ' - it was: ' . $val; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
71 |
|
if (self::evaluateCondition($operator, $valType, $val, $arg1, $arg2)) { |
|
167
|
65 |
|
return new TestResult($status, $reason); |
|
168
|
|
|
} |
|
169
|
47 |
|
return null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Interpret a response using an interpretation table. |
|
174
|
|
|
* |
|
175
|
|
|
* @param HttpResponse $response |
|
176
|
|
|
* @param array $interpretationTable |
|
177
|
|
|
* |
|
178
|
|
|
* @return TestResult If there is no match, the test result will have status = false and |
|
179
|
|
|
* info = "no-match". |
|
180
|
|
|
*/ |
|
181
|
71 |
|
public static function interpret($response, $interpretationTable) |
|
182
|
|
|
{ |
|
183
|
71 |
|
foreach ($interpretationTable as $i => $line) { |
|
184
|
71 |
|
$testResult = self::interpretLine($response, $line); |
|
185
|
71 |
|
if (!is_null($testResult)) { |
|
186
|
70 |
|
return $testResult; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
29 |
|
return new TestResult(null, 'no-match'); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|