Skip to content

Commit

Permalink
Merge pull request #111 from cego/lejo/fix-response-headers-being-enu…
Browse files Browse the repository at this point in the history
…merable-in-laravel-11

Fix not being able to handle enumerable response headers
  • Loading branch information
LauJosefsen authored Mar 26, 2024
2 parents d7b1623 + 78ffe5b commit a99f8ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/RequestInsurance/Models/RequestInsurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Request;
use GuzzleHttp\TransferStats;
use Cego\RequestInsurance\Events;
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Crypt;
Expand Down Expand Up @@ -40,7 +41,7 @@
* @property int|null $timeout_ms
* @property string|null $trace_id
* @property string|array|null $encrypted_fields
* @property string|array $response_headers
* @property string|array|Enumerable $response_headers
* @property string $response_body
* @property int $response_code
* @property int $retry_count
Expand Down Expand Up @@ -229,6 +230,11 @@ protected static function booted(): void
if (is_array($request->response_headers)) {
$request->response_headers = json_encode($request->response_headers, JSON_THROW_ON_ERROR);
}

// We make sure to json encode response headers to json if passed as an Enumerable
if ($request->response_headers instanceof Enumerable) {
$request->response_headers = json_encode($request->response_headers->all(), JSON_THROW_ON_ERROR);
}
});

static::saved(function (RequestInsurance $request) {
Expand Down
9 changes: 3 additions & 6 deletions tests/Unit/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@

class HttpResponseTest extends TestCase
{
/** @test */
public function it_can_initialize_with_request_exception()
public function test_it_can_initialize_with_request_exception()
{
$httpResponse = new HttpResponse(new RequestException('F', new Request('GET', 'www.notaplaceforsho.dk')));

$this->assertTrue($httpResponse->isRequestException());
}

/** @test */
public function it_can_initialize_with_bad_response_exception()
public function test_it_can_initialize_with_bad_response_exception()
{
$httpResponse = new HttpResponse(new BadResponseException('F', new Request('GET', 'www.notaplaceforsho.dk'), new Response()));

$this->assertTrue($httpResponse->isRequestException());
}

/** @test */
public function it_can_initialize_with_too_many_redirects_exception()
public function test_it_can_initialize_with_too_many_redirects_exception()
{
$httpResponse = new HttpResponse(new TooManyRedirectsException('F', new Request('GET', 'www.notaplaceforsho.dk')));

Expand Down

0 comments on commit a99f8ec

Please sign in to comment.