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
|
|
|
* Interpret headers line |
44
|
|
|
* |
45
|
|
|
* @param HttpResponse $response |
46
|
|
|
* @param string $operator (has-key | ) |
47
|
|
|
* @param string $fieldName field name of the header |
48
|
|
|
* @param string $fieldValue (optional) field value to look for. Only required when |
49
|
|
|
* operator is "contains-key-value" or "not-contains-key-value" |
50
|
|
|
* @return bool true if the condition matches, false otherwise |
51
|
|
|
*/ |
52
|
28 |
|
private static function evaluateHeadersLine($response, $operator, $fieldName, $fieldValue) |
53
|
|
|
{ |
54
|
28 |
|
switch ($operator) { |
55
|
28 |
|
case 'contains-key': |
56
|
4 |
|
return $response->hasHeader($fieldName); |
57
|
28 |
|
case 'not-contains-key': |
58
|
21 |
|
return (!($response->hasHeader($fieldName))); |
59
|
22 |
|
case 'contains-key-value': |
60
|
22 |
|
return $response->hasHeaderValue($fieldName, $fieldValue); |
61
|
15 |
|
case 'not-contains-key-value': |
62
|
15 |
|
return (!($response->hasHeaderValue($fieldName, $fieldValue))); |
63
|
|
|
} |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Interpret string line (body or status-code) |
69
|
|
|
* |
70
|
|
|
* @param HttpResponse $response |
71
|
|
|
* @param string $property ("body" or "status-code") |
72
|
|
|
* @param string $operator (is-empty | equals | not-equals | begins-with) |
73
|
|
|
* @param string $arg1 (only required for some operators) |
74
|
|
|
* |
75
|
|
|
* @return bool true if the condition matches, false otherwise |
76
|
|
|
*/ |
77
|
63 |
|
private static function evaluateStringLine($response, $property, $operator, $arg1) |
78
|
|
|
{ |
79
|
63 |
|
$val = ''; |
80
|
63 |
|
switch ($property) { |
81
|
63 |
|
case 'status-code': |
82
|
45 |
|
$val = $response->statusCode; |
83
|
45 |
|
break; |
84
|
43 |
|
case 'body': |
85
|
43 |
|
$val = $response->body; |
86
|
43 |
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
63 |
|
switch ($operator) { |
90
|
63 |
|
case 'is-empty': |
91
|
|
|
return ($val == ''); |
92
|
63 |
|
case 'equals': |
93
|
59 |
|
return ($val == $arg1); |
94
|
36 |
|
case 'not-equals': |
95
|
33 |
|
return ($val != $arg1); |
96
|
26 |
|
case 'begins-with': |
97
|
3 |
|
return (strpos($val, $arg1) === 0); |
98
|
|
|
} |
99
|
23 |
|
return false; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Interpret line. |
106
|
|
|
* |
107
|
|
|
* @param HttpResponse $response |
108
|
|
|
* @param array $line |
109
|
|
|
* |
110
|
|
|
* @return TestResult|null If the condition matches, a TestResult is returned, otherwise null |
111
|
|
|
*/ |
112
|
71 |
|
private static function interpretLine($response, $line) |
113
|
|
|
{ |
114
|
|
|
// ie: |
115
|
|
|
// ['inconclusive', 'body', 'is-empty'], |
116
|
|
|
// ['failure', 'statusCode', 'equals', '500'] |
117
|
|
|
// ['success', 'headers', 'contains-key-value', 'X-Response-Header-Test', 'test'], |
118
|
|
|
|
119
|
71 |
|
$status = self::parseStatusString($line[0]); |
120
|
|
|
|
121
|
71 |
|
if (!isset($line[1])) { |
122
|
24 |
|
return new TestResult($status, ''); |
123
|
|
|
} |
124
|
|
|
|
125
|
71 |
|
$propertyToExamine = $line[1]; |
126
|
71 |
|
$operator = $line[2]; |
127
|
71 |
|
$arg1 = (isset($line[3]) ? $line[3] : ''); |
128
|
71 |
|
$arg2 = (isset($line[4]) ? $line[4] : ''); |
129
|
|
|
|
130
|
71 |
|
if ($propertyToExamine == 'headers') { |
131
|
28 |
|
$match = self::evaluateHeadersLine($response, $operator, $arg1, $arg2); |
132
|
|
|
} else { |
133
|
63 |
|
$match = self::evaluateStringLine($response, $propertyToExamine, $operator, $arg1); |
134
|
|
|
} |
135
|
71 |
|
if ($match) { |
136
|
65 |
|
$reason = $propertyToExamine . ' ' . $operator; |
137
|
65 |
|
if (isset($line[3])) { |
138
|
65 |
|
$reason .= ' "' . implode('" "', array_slice($line, 3)) . '"'; |
139
|
|
|
} |
140
|
|
|
/* |
141
|
|
|
if (($propertyToExamine == 'status-code') && ($operator == 'not-equals') && (gettype($val) == 'string')) { |
142
|
|
|
$reason .= ' - it was: ' . $val; |
143
|
|
|
}*/ |
144
|
65 |
|
return new TestResult($status, $reason); |
145
|
|
|
} |
146
|
|
|
|
147
|
47 |
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Interpret a response using an interpretation table. |
152
|
|
|
* |
153
|
|
|
* @param HttpResponse $response |
154
|
|
|
* @param array $interpretationTable |
155
|
|
|
* |
156
|
|
|
* @return TestResult If there is no match, the test result will have status = false and |
157
|
|
|
* info = "no-match". |
158
|
|
|
*/ |
159
|
71 |
|
public static function interpret($response, $interpretationTable) |
160
|
|
|
{ |
161
|
71 |
|
foreach ($interpretationTable as $i => $line) { |
162
|
71 |
|
$testResult = self::interpretLine($response, $line); |
163
|
71 |
|
if (!is_null($testResult)) { |
164
|
70 |
|
return $testResult; |
165
|
|
|
} |
166
|
|
|
} |
167
|
29 |
|
return new TestResult(null, 'no-match'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|