GuzzleTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. use Cloudflare\API\Adapter\ResponseException;
  3. class GuzzleTest extends TestCase
  4. {
  5. private $client;
  6. public function setUp()
  7. {
  8. $auth = $this->getMockBuilder(\Cloudflare\API\Auth\Auth::class)
  9. ->setMethods(['getHeaders'])
  10. ->getMock();
  11. $auth->method('getHeaders')
  12. ->willReturn(['X-Testing' => 'Test']);
  13. $this->client = new \Cloudflare\API\Adapter\Guzzle($auth, 'https://httpbin.org/');
  14. }
  15. public function testGet()
  16. {
  17. $response = $this->client->get('https://httpbin.org/get');
  18. $headers = $response->getHeaders();
  19. $this->assertEquals('application/json', $headers['Content-Type'][0]);
  20. $body = json_decode($response->getBody());
  21. $this->assertEquals('Test', $body->headers->{'X-Testing'});
  22. $response = $this->client->get('https://httpbin.org/get', [], ['X-Another-Test' => 'Test2']);
  23. $body = json_decode($response->getBody());
  24. $this->assertEquals('Test2', $body->headers->{'X-Another-Test'});
  25. }
  26. public function testPost()
  27. {
  28. $response = $this->client->post('https://httpbin.org/post', ['X-Post-Test' => 'Testing a POST request.']);
  29. $headers = $response->getHeaders();
  30. $this->assertEquals('application/json', $headers['Content-Type'][0]);
  31. $body = json_decode($response->getBody());
  32. $this->assertEquals('Testing a POST request.', $body->json->{'X-Post-Test'});
  33. }
  34. public function testPut()
  35. {
  36. $response = $this->client->put('https://httpbin.org/put', ['X-Put-Test' => 'Testing a PUT request.']);
  37. $headers = $response->getHeaders();
  38. $this->assertEquals('application/json', $headers['Content-Type'][0]);
  39. $body = json_decode($response->getBody());
  40. $this->assertEquals('Testing a PUT request.', $body->json->{'X-Put-Test'});
  41. }
  42. public function testPatch()
  43. {
  44. $response = $this->client->patch(
  45. 'https://httpbin.org/patch',
  46. ['X-Patch-Test' => 'Testing a PATCH request.']
  47. );
  48. $headers = $response->getHeaders();
  49. $this->assertEquals('application/json', $headers['Content-Type'][0]);
  50. $body = json_decode($response->getBody());
  51. $this->assertEquals('Testing a PATCH request.', $body->json->{'X-Patch-Test'});
  52. }
  53. public function testDelete()
  54. {
  55. $response = $this->client->delete(
  56. 'https://httpbin.org/delete',
  57. ['X-Delete-Test' => 'Testing a DELETE request.']
  58. );
  59. $headers = $response->getHeaders();
  60. $this->assertEquals('application/json', $headers['Content-Type'][0]);
  61. $body = json_decode($response->getBody());
  62. $this->assertEquals('Testing a DELETE request.', $body->json->{'X-Delete-Test'});
  63. }
  64. public function testNotFound()
  65. {
  66. $this->expectException(ResponseException::class);
  67. $this->client->get('https://httpbin.org/status/404');
  68. }
  69. public function testServerError()
  70. {
  71. $this->expectException(ResponseException::class);
  72. $this->client->get('https://httpbin.org/status/500');
  73. }
  74. }