|
| 1 | +<?php |
| 2 | + |
| 3 | +use GuzzleHttp\Exception\RequestException; |
| 4 | +use GuzzleHttp\Handler\MockHandler; |
| 5 | +use GuzzleHttp\HandlerStack; |
| 6 | +use GuzzleHttp\Middleware; |
| 7 | +use GuzzleHttp\Psr7\Request as GuzzleRequest; |
| 8 | +use GuzzleHttp\Psr7\Response as GuzzleResponse; |
| 9 | +use Illuminate\Contracts\Events\Dispatcher as DispatcherContract; |
| 10 | +use Illuminate\Events\Dispatcher; |
| 11 | +use OpenAI\Laravel\Http\Adapters\GuzzleHttpAdapter; |
| 12 | + |
| 13 | +beforeEach(function () { |
| 14 | + app()->instance(DispatcherContract::class, new Dispatcher(app())); |
| 15 | + app()->alias(DispatcherContract::class, 'events'); // Important |
| 16 | +}); |
| 17 | + |
| 18 | +it('create GuzzleHttp client instance', function () { |
| 19 | + expect((new GuzzleHttpAdapter)->make())->toBeInstanceOf(GuzzleHttp\Client::class); |
| 20 | + expect((new GuzzleHttpAdapter)->client())->toBeInstanceOf(GuzzleHttp\Client::class); |
| 21 | + expect((new GuzzleHttpAdapter)->timeout(3))->toBeInstanceOf(GuzzleHttp\Client::class); |
| 22 | +}); |
| 23 | + |
| 24 | +it('create GuzzleHttp client instance with options timeout and handler', function () { |
| 25 | + $timeout = 0; |
| 26 | + $handler = HandlerStack::create(new MockHandler([new GuzzleResponse(200, [], 'OK')])); |
| 27 | + $handler->push(Middleware::tap( |
| 28 | + after: function ($req, array $options) use (&$timeout) { |
| 29 | + $timeout = $options['timeout']; |
| 30 | + expect($options['handler'])->toBeInstanceOf(HandlerStack::class); |
| 31 | + } |
| 32 | + )); |
| 33 | + $client = GuzzleHttpAdapter::make(['timeout' => 3], $handler); |
| 34 | + $response = $client->request('GET', 'https://example.com'); |
| 35 | + |
| 36 | + expect($client)->toBeInstanceOf(\GuzzleHttp\Client::class); |
| 37 | + expect($response)->toBeInstanceOf(\Psr\Http\Message\ResponseInterface::class); |
| 38 | + expect($response->getBody()->getContents())->toBe('OK'); |
| 39 | + expect($response->getStatusCode())->toBe(200); |
| 40 | + expect($timeout)->toBe(3); |
| 41 | +}); |
| 42 | + |
| 43 | +it('runs middleware before and after callbacks to callable events', function () { |
| 44 | + $calledBefore = false; |
| 45 | + $calledAfter = false; |
| 46 | + |
| 47 | + $mock = new MockHandler([new GuzzleResponse(200, [], 'OK')]); |
| 48 | + $stack = HandlerStack::create($mock); |
| 49 | + |
| 50 | + $stack->push(Middleware::tap( |
| 51 | + before: function () use (&$calledBefore) { |
| 52 | + $calledBefore = true; |
| 53 | + }, |
| 54 | + after: function ($req, array $options, $promise) use (&$calledAfter) { |
| 55 | + $calledAfter = true; |
| 56 | + } |
| 57 | + )); |
| 58 | + |
| 59 | + $client = (new GuzzleHttpAdapter)->client([], $stack); |
| 60 | + |
| 61 | + $response = $client->request('GET', 'https://example.com'); |
| 62 | + |
| 63 | + // Force the response to complete |
| 64 | + expect($response->getBody()->getContents())->toBe('OK'); |
| 65 | + expect($calledBefore)->toBeTrue(); |
| 66 | + expect($calledAfter)->toBeTrue(); |
| 67 | + expect($response->getStatusCode())->toBe(200); |
| 68 | +}); |
| 69 | + |
| 70 | +it('handles rejected promise without Laravel events', function () { |
| 71 | + |
| 72 | + $mock = new MockHandler([new RequestException('Error', new GuzzleRequest('GET', 'test'))]); |
| 73 | + $handler = HandlerStack::create($mock); |
| 74 | + $client = (new GuzzleHttpAdapter)->client([], $handler); |
| 75 | + |
| 76 | + $promise = $client->getAsync('https://example.com'); |
| 77 | + |
| 78 | + $rejection = null; |
| 79 | + $promise->then(null, function ($reason) use (&$rejection) { |
| 80 | + $rejection = $reason; |
| 81 | + })->wait(); |
| 82 | + |
| 83 | + expect($rejection)->toBeInstanceOf(RequestException::class); |
| 84 | +}); |
| 85 | + |
| 86 | +it('handles rejected promise and normalizes ConnectionException', function () { |
| 87 | + $calledAfter = false; |
| 88 | + $reason = null; |
| 89 | + |
| 90 | + // Mock a rejected promise |
| 91 | + $request = new GuzzleRequest('GET', 'test'); |
| 92 | + $mock = new MockHandler([new RequestException('Connection failed', $request)]); |
| 93 | + $stack = HandlerStack::create($mock); |
| 94 | + |
| 95 | + // Push tap middleware to observe "after" |
| 96 | + $stack->push(Middleware::tap( |
| 97 | + after: function ($req, array $o, $promise) use (&$calledAfter, &$reason) { |
| 98 | + $promise->otherwise(function ($responseReason) use (&$calledAfter, &$reason) { |
| 99 | + $reason = $responseReason; |
| 100 | + $calledAfter = true; |
| 101 | + }); |
| 102 | + } |
| 103 | + )); |
| 104 | + |
| 105 | + try { |
| 106 | + GuzzleHttpAdapter::make([], $stack)->request('GET', 'https://example.com'); |
| 107 | + } catch (\GuzzleHttp\Exception\RequestException $e) { |
| 108 | + // expected |
| 109 | + } |
| 110 | + |
| 111 | + expect($calledAfter)->toBeTrue(); // ensure the "after" handler ran |
| 112 | + expect($reason)->toBeInstanceOf(RequestException::class); |
| 113 | +}); |
0 commit comments