|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use GuzzleHttp\Exception\ConnectException; |
3 | 4 | use GuzzleHttp\Psr7\Request;
|
4 | 5 | use GuzzleHttp\Psr7\Response;
|
5 | 6 | use Psr\Http\Client\ClientInterface;
|
6 | 7 | use Resend\Enums\Transporter\ContentType;
|
| 8 | +use Resend\Exceptions\ErrorException; |
| 9 | +use Resend\Exceptions\TransporterException; |
| 10 | +use Resend\Exceptions\UnserializableResponse; |
7 | 11 | use Resend\Transporters\HttpTransporter;
|
8 | 12 | use Resend\ValueObjects\ApiKey;
|
9 | 13 | use Resend\ValueObjects\Transporter\BaseUri;
|
|
43 | 47 |
|
44 | 48 | $this->http->request($payload);
|
45 | 49 | });
|
| 50 | + |
| 51 | +test('request response', function () { |
| 52 | + $payload = Payload:: create( 'email', [ 'to' => '[email protected]']); |
| 53 | + $response = new Response(200, [], json_encode([ |
| 54 | + 'id' => 'test_123', |
| 55 | + |
| 56 | + ])); |
| 57 | + |
| 58 | + $this->client |
| 59 | + ->shouldReceive('sendRequest') |
| 60 | + ->once() |
| 61 | + ->andReturn($response); |
| 62 | + |
| 63 | + $response = $this->http->request($payload); |
| 64 | + |
| 65 | + expect($response); |
| 66 | +}); |
| 67 | + |
| 68 | +test('request can handle client errors', function () { |
| 69 | + $payload = Payload:: create( 'email', [ 'to' => '[email protected]']); |
| 70 | + |
| 71 | + $baseUri = BaseUri::from('api.resend.com'); |
| 72 | + $headers = Headers::withAuthorization(ApiKey::from('foo')); |
| 73 | + |
| 74 | + $this->client |
| 75 | + ->shouldReceive('sendRequest') |
| 76 | + ->once() |
| 77 | + ->andThrow(new ConnectException('Could not resolve host.', $payload->toRequest($baseUri, $headers))); |
| 78 | + |
| 79 | + expect(fn () => $this->http->request($payload))->toThrow(function (TransporterException $exception) { |
| 80 | + expect($exception->getMessage())->toBe('Could not resolve host.') |
| 81 | + ->and($exception->getCode())->toBe(0) |
| 82 | + ->and($exception->getPrevious())->toBeInstanceOf(ConnectException::class); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +test('request can handle serialization errors', function () { |
| 87 | + $payload = Payload:: create( 'email', [ 'to' => '[email protected]']); |
| 88 | + $response = new Response(200, [], 'err'); |
| 89 | + |
| 90 | + $this->client |
| 91 | + ->shouldReceive('sendRequest') |
| 92 | + ->once() |
| 93 | + ->andReturn($response); |
| 94 | + |
| 95 | + $this->http->request($payload); |
| 96 | +})->throws(UnserializableResponse::class, 'Syntax error'); |
| 97 | + |
| 98 | +test('request can handle server errors', function () { |
| 99 | + $payload = Payload:: create( 'email', [ 'to' => '[email protected]']); |
| 100 | + $response = new Response(422, [], json_encode([ |
| 101 | + 'error' => [ |
| 102 | + 'code' => 422, |
| 103 | + 'type' => 'missing_required_field', |
| 104 | + 'message' => 'Missing `to` field', |
| 105 | + ], |
| 106 | + ])); |
| 107 | + |
| 108 | + $this->client |
| 109 | + ->shouldReceive('sendRequest') |
| 110 | + ->once() |
| 111 | + ->andReturn($response); |
| 112 | + |
| 113 | + expect(fn () => $this->http->request($payload))->toThrow(function (ErrorException $exception) { |
| 114 | + expect($exception->getMessage())->toBe('Missing `to` field') |
| 115 | + ->and($exception->getErrorMessage())->toBe('Missing `to` field') |
| 116 | + ->and($exception->getErrorCode())->toBe(422) |
| 117 | + ->and($exception->getErrorType())->toBe('missing_required_field'); |
| 118 | + }); |
| 119 | +}); |
0 commit comments