|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Adapter; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use GuzzleHttp\Exception\RequestException; |
| 7 | +use Reactmore\SupportAdapter\Adapter\Guzzle; |
| 8 | +use Reactmore\SupportAdapter\Adapter\Auth\BearerToken; |
| 9 | + |
| 10 | +class GuzzleTest extends TestCase |
| 11 | +{ |
| 12 | + private $client; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + // Menggunakan BearerToken untuk autentikasi |
| 19 | + $auth = new BearerToken('test-token'); |
| 20 | + $this->client = new Guzzle($auth, 'https://httpbin.org/'); |
| 21 | + } |
| 22 | + |
| 23 | + public function testGet() |
| 24 | + { |
| 25 | + $response = $this->client->get('https://httpbin.org/get', [], ['X-Testing' => 'Test']); |
| 26 | + $headers = $response->getHeaders(); |
| 27 | + $this->assertEquals('application/json', $headers['Content-Type'][0]); |
| 28 | + |
| 29 | + $body = json_decode($response->getBody()); |
| 30 | + $this->assertEquals('Test', $body->headers->{'X-Testing'}); |
| 31 | + $this->assertEquals('Bearer test-token', $body->headers->Authorization); |
| 32 | + |
| 33 | + $response = $this->client->get('https://httpbin.org/get', [], ['X-Another-Test' => 'Test2']); |
| 34 | + $body = json_decode($response->getBody()); |
| 35 | + $this->assertEquals('Test2', $body->headers->{'X-Another-Test'}); |
| 36 | + } |
| 37 | + |
| 38 | + public function testPost() |
| 39 | + { |
| 40 | + $response = $this->client->post('https://httpbin.org/post', ['X-Post-Test' => 'Testing a POST request.']); |
| 41 | + $headers = $response->getHeaders(); |
| 42 | + $this->assertEquals('application/json', $headers['Content-Type'][0]); |
| 43 | + $body = json_decode($response->getBody()->getContents()); |
| 44 | + $this->assertEquals('Testing a POST request.', $body->form->{'X-Post-Test'}); |
| 45 | + } |
| 46 | + |
| 47 | + public function testPut() |
| 48 | + { |
| 49 | + $response = $this->client->put('https://httpbin.org/put', ['X-Put-Test' => 'Testing a PUT request.']); |
| 50 | + $headers = $response->getHeaders(); |
| 51 | + $this->assertEquals('application/json', $headers['Content-Type'][0]); |
| 52 | + |
| 53 | + $body = json_decode($response->getBody()->getContents()); |
| 54 | + $this->assertEquals('Testing a PUT request.', $body->form->{'X-Put-Test'}); |
| 55 | + } |
| 56 | + |
| 57 | + public function testPatch() |
| 58 | + { |
| 59 | + $response = $this->client->patch( |
| 60 | + 'https://httpbin.org/patch', |
| 61 | + ['X-Patch-Test' => 'Testing a PATCH request.'] |
| 62 | + ); |
| 63 | + |
| 64 | + $headers = $response->getHeaders(); |
| 65 | + $this->assertEquals('application/json', $headers['Content-Type'][0]); |
| 66 | + |
| 67 | + $body = json_decode($response->getBody()->getContents()); |
| 68 | + $this->assertEquals('Testing a PATCH request.', $body->form->{'X-Patch-Test'}); |
| 69 | + } |
| 70 | + |
| 71 | + public function testDelete() |
| 72 | + { |
| 73 | + $response = $this->client->delete( |
| 74 | + 'https://httpbin.org/delete', |
| 75 | + ['X-Delete-Test' => 'Testing a DELETE request.'] |
| 76 | + ); |
| 77 | + |
| 78 | + $headers = $response->getHeaders(); |
| 79 | + $this->assertEquals('application/json', $headers['Content-Type'][0]); |
| 80 | + |
| 81 | + $body = json_decode($response->getBody()->getContents()); |
| 82 | + $this->assertEquals('Testing a DELETE request.', $body->form->{'X-Delete-Test'}); |
| 83 | + } |
| 84 | + |
| 85 | + public function testNotFound() |
| 86 | + { |
| 87 | + $this->expectException(RequestException::class); |
| 88 | + $this->client->get('https://httpbin.org/status/404'); |
| 89 | + } |
| 90 | + |
| 91 | + public function testServerError() |
| 92 | + { |
| 93 | + $this->expectException(RequestException::class); |
| 94 | + $this->client->get('https://httpbin.org/status/500'); |
| 95 | + } |
| 96 | +} |
0 commit comments