|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Overwatch\ServiceBundle\Expectation; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
6
|
|
|
use Overwatch\ExpectationBundle\Exception as Result; |
|
7
|
|
|
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* ToRespondWithMimeTypeExpectation |
|
11
|
|
|
* Expectation that a URL will respond to a HTTP request with the given mime type. |
|
12
|
|
|
*/ |
|
13
|
|
|
class ToRespondWithMimeTypeExpectation implements ExpectationInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $expectationConfig; |
|
16
|
|
|
|
|
17
|
|
|
private $httpClient; |
|
18
|
|
|
|
|
19
|
32 |
|
public function __construct(array $expectationConfig, array $httpClientConfig = []) |
|
20
|
|
|
{ |
|
21
|
32 |
|
$this->expectationConfig = $expectationConfig; |
|
22
|
32 |
|
$this->httpClient = new HttpClient($httpClientConfig); |
|
23
|
32 |
|
} |
|
24
|
|
|
|
|
25
|
5 |
|
public function run($actual, $expected = null) |
|
26
|
|
|
{ |
|
27
|
5 |
|
$actual = filter_var($actual, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); |
|
28
|
|
|
|
|
29
|
5 |
|
if ($actual === false) { |
|
30
|
1 |
|
throw new \InvalidArgumentException('The actual value provided is not a valid URL'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
$response = $this->httpClient->get( |
|
34
|
4 |
|
$actual, |
|
35
|
|
|
[ |
|
36
|
4 |
|
'allow_redirects' => false, |
|
37
|
4 |
|
'http_errors' => !$this->expectationConfig['allow_errors'], |
|
38
|
4 |
|
'timeout' => $this->expectationConfig['timeout'] |
|
39
|
4 |
|
] |
|
40
|
4 |
|
); |
|
41
|
|
|
|
|
42
|
3 |
|
$result = $response->getHeader('Content-Type')[0]; |
|
43
|
|
|
|
|
44
|
3 |
|
if ($expected !== $result) { |
|
45
|
1 |
|
throw new Result\ExpectationFailedException(sprintf( |
|
46
|
1 |
|
'Expected %s to respond with mime type "%s", actually responded "%s"', |
|
47
|
1 |
|
$actual, |
|
48
|
1 |
|
$expected, |
|
49
|
|
|
$result |
|
50
|
1 |
|
)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
return sprintf('Responded with mime type: "%s"', $result); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|