CustomHostnamesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 18/03/2018
  6. * Time: 22:23
  7. */
  8. use Cloudflare\API\Endpoints\CustomHostnames;
  9. class CustomHostnamesTest extends TestCase
  10. {
  11. public function testAddHostname()
  12. {
  13. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.json');
  14. $customSsl = $this->getCustomSsl();
  15. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  16. $mock->method('post')->willReturn($response);
  17. $mock->expects($this->once())
  18. ->method('post')
  19. ->with(
  20. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
  21. $this->equalTo([
  22. 'hostname' => 'app.example.com',
  23. 'custom_origin_server' => 'origin.example.com',
  24. 'ssl' => [
  25. 'method' => 'http',
  26. 'type' => 'dv',
  27. 'settings' => [
  28. 'http2' => 'on',
  29. 'http3' => 'on',
  30. 'min_tls_version' => '1.2',
  31. ],
  32. 'bundle_method' => 'optimal',
  33. 'custom_key' => $customSsl['key'],
  34. 'custom_certificate' => $customSsl['certificate'],
  35. 'wildcard' => true,
  36. ],
  37. ])
  38. );
  39. $hostname = new CustomHostnames($mock);
  40. $sslSettings = [
  41. 'http2' => 'on',
  42. 'http3' => 'on',
  43. 'min_tls_version' => '1.2'
  44. ];
  45. $hostname->addHostname(
  46. '023e105f4ecef8ad9ca31a8372d0c353',
  47. 'app.example.com',
  48. 'http',
  49. 'dv',
  50. $sslSettings,
  51. 'origin.example.com',
  52. true,
  53. 'optimal',
  54. $customSsl
  55. );
  56. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id);
  57. }
  58. public function testListHostnames()
  59. {
  60. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json');
  61. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  62. $mock->method('get')->willReturn($response);
  63. $mock->expects($this->once())
  64. ->method('get')
  65. ->with(
  66. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'),
  67. $this->equalTo([
  68. 'hostname' => 'app.example.com',
  69. 'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9',
  70. 'page' => 1,
  71. 'per_page' => 20,
  72. 'order' => 'ssl',
  73. 'direction' => 'desc',
  74. 'ssl' => 0
  75. ])
  76. );
  77. $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
  78. $result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0);
  79. $this->assertObjectHasAttribute('result', $result);
  80. $this->assertObjectHasAttribute('result_info', $result);
  81. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id);
  82. $this->assertEquals(1, $result->result_info->page);
  83. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result[0]->id);
  84. }
  85. public function testGetHostname()
  86. {
  87. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json');
  88. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  89. $mock->method('get')->willReturn($response);
  90. $mock->expects($this->once())
  91. ->method('get')
  92. ->with(
  93. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9')
  94. );
  95. $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
  96. $result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
  97. $this->assertObjectHasAttribute('id', $result);
  98. $this->assertObjectHasAttribute('hostname', $result);
  99. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id);
  100. }
  101. public function testUpdateHostname()
  102. {
  103. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json');
  104. $customSsl = $this->getCustomSsl();
  105. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  106. $mock->method('patch')->willReturn($response);
  107. $mock->expects($this->once())
  108. ->method('patch')
  109. ->with(
  110. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'),
  111. $this->equalTo([
  112. 'custom_origin_server' => 'origin.example.com',
  113. 'ssl' => [
  114. 'method' => 'http',
  115. 'type' => 'dv',
  116. 'settings' => [
  117. 'http2' => 'on',
  118. 'http3' => 'on',
  119. 'min_tls_version' => '1.2'
  120. ],
  121. 'bundle_method' => 'optimal',
  122. 'custom_key' => $customSsl['key'],
  123. 'custom_certificate' => $customSsl['certificate'],
  124. 'wildcard' => true,
  125. ]
  126. ])
  127. );
  128. $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
  129. $sslSettings = [
  130. 'http2' => 'on',
  131. 'http3' => 'on',
  132. 'min_tls_version' => '1.2'
  133. ];
  134. $result = $zones->updateHostname(
  135. '023e105f4ecef8ad9ca31a8372d0c353',
  136. '0d89c70d-ad9f-4843-b99f-6cc0252067e9',
  137. 'http',
  138. 'dv',
  139. $sslSettings,
  140. 'origin.example.com',
  141. true,
  142. 'optimal',
  143. [
  144. 'key' => $customSsl['key'],
  145. 'certificate' => $customSsl['certificate'],
  146. ]
  147. );
  148. $this->assertObjectHasAttribute('id', $result);
  149. $this->assertObjectHasAttribute('hostname', $result);
  150. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id);
  151. }
  152. public function testDeleteHostname()
  153. {
  154. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.json');
  155. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  156. $mock->method('delete')->willReturn($response);
  157. $mock->expects($this->once())
  158. ->method('delete')
  159. ->with(
  160. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9')
  161. );
  162. $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
  163. $result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9');
  164. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id);
  165. $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->id);
  166. }
  167. public function testGetHostnameFallbackOrigin()
  168. {
  169. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getCustomHostnameFallbackOrigin.json');
  170. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  171. $mock->method('get')->willReturn($response);
  172. $mock->expects($this->once())
  173. ->method('get')
  174. ->with(
  175. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/fallback_origin')
  176. );
  177. $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock);
  178. $result = $zones->getFallbackOrigin('023e105f4ecef8ad9ca31a8372d0c353');
  179. $this->assertObjectHasAttribute('origin', $result);
  180. $this->assertObjectHasAttribute('status', $result);
  181. }
  182. private function getCustomSsl(): array
  183. {
  184. $customKey = <<<KEY
  185. -----BEGIN PRIVATE KEY-----
  186. MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDZfoCUkzkZLCzo
  187. OFTtlXU9OYqNFx06J/GOKCwDCyfkY5RY1x6BVrVpTqf/JaU42DZmCjIiEugBg4bu
  188. eu9/w21prIWgRKEe8mjrw83+3QSIyQrs+78rqwDptUfL+IyhYln6SBjqPQ569Y0w
  189. x6A896PDMYPHgnWtclGwsxDNKJ2eWsH+C4UkLUeVM4BILEJ00YUjayowL/0sflTJ
  190. yY58c9fVV27aGBJ4znreYkBojPQ0fzVZ3HJfYD+DgYUUkuzN/WohOLTNTxvzt/i2
  191. GNxP8tZzi0E/t4KtGTsIVmROKaCXnmozQyv0VES5TNZL1nxLvVuPca9DKXwVst2o
  192. v5czEM8fAgMBAAECggEBANgG/aIVpWYqaaRyp3CgviWE7Oh9J+um1xgzMJwJTaNd
  193. gXDIoyUmweQKW3Vjp/uRTl8GC4uqqcUvJivj8dU+gIOw970bzcmWT7616vsV/rX6
  194. sp524wh1vt9jzx97DfwSW3rsd8rZwHNDSO1FqxRDiOaNXO4i183iud8/zRVqHTy1
  195. 5girngsGl7ebTt3LDHDQQ86kND2nVr8xZuFaqs8Td41AsF6DGbB709wMUqoM/obO
  196. iUtXCZ5Rrm2a78OUi0cqWsuxdhJjtOW0PBvrPTlSq+1EuQWAWV8HN1JI58YnLcLy
  197. SKZpsu5wxWdKMgX0NCkfLjDZCAPlBaZLPPp986GHavECgYEA8hM6tIfGBnXuxBvI
  198. y2lJG3sHGs83pnCqYg9dDrr+m3JOPQu6l9MEPEtsrOiI0Ktu/L+kV5uyBDRvB6ff
  199. BD6BJ2CiG86UvMpKojBeAlZBLXr1SnWzIPC+3fBzkVSo1MiRs3nTNRfeblkRxC3e
  200. LWtl96obA1GOgpifrh6ZB2RfvrcCgYEA5gFL4+oDUDcRtc1Pw+AFwPTey+3rkVU+
  201. FHvRGeU+m6dtxXF+BYFpDs/ONfmHzsdBSwkYxta/x8rKP5uyjl9p0QSdhysrJibO
  202. sWsoux35QxEZiyplCV2+zMK/79EhS2CuiudAidF6NxK+/g9EwXRlGDDlnFDB2epe
  203. kyL97K4zCtkCgYEA68Bgbsq/xzD5XFG2xqr9wN6a97gQ+W5F8QQHW74vEZJLsdYH
  204. Xa7rNBE8gFRiUd5zU4EL+yotPz0VWH5bilWZEJFirvQMFKRp9PRnyZzZEwLpeh+Q
  205. WSc8qwZudn3dgoTmqMSfNdjODed+jvEgrFkoz/8BGcVGpdcfw8IWxIUzXZcCgYAY
  206. /OsRx8q0XEdASR3xWdVGMVRDM4X0NB6aexkshwtWPcpfOQVH89dGFK2Cj6mBfYRK
  207. cqKOd6Y+Pnnajz/G1/bXDnlOxhHaAz1RaSLzsT3zW1g7FlADxHuGI2JW25GSbt6H
  208. mLgaQPfWI+M8FsyRd+PDzQwk/2EQG7ZKpfKQVByXgQKBgQDkKciB6Wb2hLNTKzK8
  209. Kr42U70H++QT8AqZX2F79PjgYcRFZqGXLuq/hEuiOhXfl8DFur3fC5JN8AeLC5/j
  210. bsrBsljYfVvtLQzilugs1oEe94LTrYjR2oQt0W24bqpGQHuv1ILuUBuodERkxSFL
  211. /cMkj3wSfC341hFaJEuG1+PcxA==
  212. -----END PRIVATE KEY-----
  213. KEY;
  214. $customCertificate = <<<CERTIFICATE
  215. -----BEGIN CERTIFICATE-----
  216. MIIDmTCCAoGgAwIBAgIULyaeNqp0tOut/wvuxNyKmUxOGYEwDQYJKoZIhvcNAQEL
  217. BQAwXDELMAkGA1UEBhMCWFgxFTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoGA1UE
  218. CgwTRGVmYXVsdCBDb21wYW55IEx0ZDEYMBYGA1UEAwwPYXBwLmV4YW1wbGUuY29t
  219. MB4XDTIxMDYxNDIzMzU0MVoXDTIyMDYxNDIzMzU0MVowXDELMAkGA1UEBhMCWFgx
  220. FTATBgNVBAcMDERlZmF1bHQgQ2l0eTEcMBoGA1UECgwTRGVmYXVsdCBDb21wYW55
  221. IEx0ZDEYMBYGA1UEAwwPYXBwLmV4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEF
  222. AAOCAQ8AMIIBCgKCAQEA2X6AlJM5GSws6DhU7ZV1PTmKjRcdOifxjigsAwsn5GOU
  223. WNcegVa1aU6n/yWlONg2ZgoyIhLoAYOG7nrvf8NtaayFoEShHvJo68PN/t0EiMkK
  224. 7Pu/K6sA6bVHy/iMoWJZ+kgY6j0OevWNMMegPPejwzGDx4J1rXJRsLMQzSidnlrB
  225. /guFJC1HlTOASCxCdNGFI2sqMC/9LH5UycmOfHPX1Vdu2hgSeM563mJAaIz0NH81
  226. WdxyX2A/g4GFFJLszf1qITi0zU8b87f4thjcT/LWc4tBP7eCrRk7CFZkTimgl55q
  227. M0Mr9FREuUzWS9Z8S71bj3GvQyl8FbLdqL+XMxDPHwIDAQABo1MwUTAdBgNVHQ4E
  228. FgQUbAfyBm0wpM7FqUb1yqeaF4voY/gwHwYDVR0jBBgwFoAUbAfyBm0wpM7FqUb1
  229. yqeaF4voY/gwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAO2Dd
  230. k/seFjp83caYE/NVdDy5B7l5JeVtruaUdlGbb0xtVhiIdoY43ukhHFw8zuWMW9RX
  231. SUbrzwacfKLDBikcefk9go6cMimqYIRF8Hntph1gjjqB0papUm2WVYbsBRv2okys
  232. ej0dGSeUEsWjKRTSMkJsbbiEv6oveeSki069zl+tln0UhbHedkIY3rJsFIyoddSu
  233. g96r5HPHksnObm1JCym0xd09+msliDkBmq87mxok9m5aEqWX4XvdGfYERV/eD5vC
  234. KcW4DoM1KZd8E6tlniglc1jC0pzKfho7Uoe6UtObgHZGNwRYwYy+BHvHYY46ctSI
  235. NdZ7G/lUyrBFhsRrhw==
  236. -----END CERTIFICATE-----
  237. CERTIFICATE;
  238. return [
  239. 'key' => $customKey,
  240. 'certificate' => $customCertificate,
  241. ];
  242. }
  243. }