Skip to content

Commit

Permalink
Merge pull request #96 from cego/mawo/add-check-for-image-gif-content…
Browse files Browse the repository at this point in the history
…-type

mawo/add-check-for-image-gif-content-type
  • Loading branch information
mawocego authored Jan 2, 2024
2 parents 8b1b213 + f2d80a1 commit bd28773
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/RequestInsurance/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public function isInconsistent(): bool
return ! isset($this->response);
}

/**
* Returns true if the request has content-type image
*
* @return bool
*/
public function isImageResponse(): bool
{
return collect($this->getHeaders()
->get('content-type', []))
->filter(fn ($type) => str_starts_with($type, 'image/'))
->isNotEmpty();
}

/**
* Returns true when the request timed out
*
Expand Down Expand Up @@ -140,6 +153,10 @@ public function getBody(): ?string
return '<REQUEST_INCONSISTENT : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>';
}

if ($this->isImageResponse()) {
return '<REQUEST_IMAGE_GIF_RESPONSE : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>';
}

return $this->response->getBody()->getContents();
}

Expand Down
2 changes: 1 addition & 1 deletion src/RequestInsurance/Models/RequestInsurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public function handleResponse(HttpResponse $response): void
}
} elseif ($response->wasSuccessful()) {
$this->setState(State::COMPLETED);
// If response code is 408, we take it as a timeout and set the request to retry again later
// If response code is 408, we take it as a timeout and set the request to retry again later
} elseif ($response->isRetryable() || ($response->getCode() == 408 && $this->retry_inconsistent)) {
$this->retryLater(false);
} else {
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/RequestInsuranceWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,30 @@ public function it_can_retry_inconsistent_jobs()
// Assert
$this->assertEquals(State::WAITING, RequestInsurance::first()->state);
}

/** @test */
public function it_can_process_image_response()
{
// Arrange
RequestInsuranceClient::fake(fn () => Http::response([], 200, ['content-type' => 'image/gif', 'some-header' => 'some-value']));

$requestInsurance = RequestInsurance::getBuilder()
->url('https://test.lupinsdev.dk')
->method('get')
->create();

$requestInsurance->refresh();

$this->assertTrue($requestInsurance->hasState(State::READY));

// Act
$worker = new RequestInsuranceWorker();
$worker->run(true);

// Assert
$requestInsurance->refresh();

$this->assertTrue($requestInsurance->hasState(State::COMPLETED));
$this->assertEquals('<REQUEST_IMAGE_GIF_RESPONSE : THIS MESSAGE WAS ADDED BY REQUEST INSURANCE>', $requestInsurance->response_body);
}
}

0 comments on commit bd28773

Please sign in to comment.