forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpClientTest.php
More file actions
104 lines (81 loc) · 2.6 KB
/
Copy pathHttpClientTest.php
File metadata and controls
104 lines (81 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Cloudinary;
use Cloudinary;
use PHPUnit\Framework\TestCase;
/**
* Class HelpersTest
* @package Cloudinary
*/
class HttpClientTest extends TestCase
{
protected static $http_client_test_id;
/**
* @var HttpClient
*/
protected $httpClient;
public static function setUpBeforeClass()
{
\Cloudinary::reset_config();
if (!Cloudinary::config_get("api_secret")) {
self::markTestSkipped('Please setup environment for Helpers test to run');
}
self::$http_client_test_id = "httpclient_test_" . UNIQUE_TEST_ID;
Uploader::upload(TEST_IMG, ["public_id" => self::$http_client_test_id]);
}
public static function tearDownAfterClass()
{
if (!Cloudinary::config_get("api_secret")) {
self::fail("You need to configure the cloudinary api for the tests to work.");
}
$api = new Cloudinary\Api();
try {
$api->delete_resources([self::$http_client_test_id]);
} catch (\Exception $e) {
}
}
public function setUp()
{
$this->httpClient = new HttpClient();
}
/**
* @throws Error
*/
public function testHttpClientGetJSON()
{
$json_options = ['width'=> 'auto:breakpoints:json'];
$json_url = Cloudinary::cloudinary_url(self::$http_client_test_id, $json_options);
$json = $this->httpClient->getJSON($json_url);
$this->assertArrayHasKey('breakpoints', $json);
$this->assertTrue(is_array($json['breakpoints']));
}
/**
* Should throw Cloudinary\Error on invalid or non JSON reponse
*/
public function testHttpClientGetJSONNonJSON()
{
$url = Cloudinary::cloudinary_url(self::$http_client_test_id);
$message = "";
try {
$this->httpClient->getJSON($url);
$this->fail("Cloudinary\Error expected");
} catch (Cloudinary\Error $e) {
$message = $e->getMessage();
}
self::assertStringStartsWith("Error parsing server response", $message);
}
/**
* Should throw Cloudinary\Error on invalid or non existing URL
*/
public function testHttpClientGetJSONInvalidURL()
{
$url = Cloudinary::cloudinary_url(self::$http_client_test_id . '_non_existing');
$message = "";
try {
$this->httpClient->getJSON($url);
$this->fail("Cloudinary\Error expected");
} catch (Cloudinary\Error $e) {
$message = $e->getMessage();
}
self::assertStringStartsWith("Server returned unexpected status code", $message);
}
}