Skip to content

Commit

Permalink
Fix the turbo-stream checking
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Jan 29, 2021
1 parent b79a073 commit 61d19b1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Http/Middleware/TurboMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tonysm\TurboLaravel\NamesResolver;
use Tonysm\TurboLaravel\Turbo;
use Tonysm\TurboLaravel\TurboFacade;

class TurboMiddleware
Expand Down Expand Up @@ -72,7 +73,7 @@ private function turboResponse($response, Request $request)
*/
private function turboVisit($request)
{
return Str::contains($request->header('Accept', ''), 'turbo-stream');
return Str::contains($request->header('Accept', ''), Turbo::TURBO_STREAM_FORMAT);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Models/Broadcasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Tonysm\TurboLaravel\Jobs\BroadcastModelCreated;
use Tonysm\TurboLaravel\Jobs\BroadcastModelUpdated;
use Tonysm\TurboLaravel\LaravelBroadcaster;
use Tonysm\TurboLaravel\NamesResolver;
use Tonysm\TurboLaravel\TurboFacade;

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/Http/Middleware/TurboMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tonysm\TurboLaravel\Http\Middleware\TurboMiddleware;
use Tonysm\TurboLaravel\Tests\TestCase;
use Tonysm\TurboLaravel\Tests\TestModel;
use Tonysm\TurboLaravel\Turbo;
use Tonysm\TurboLaravel\TurboFacade;

class TurboMiddlewareTest extends TestCase
Expand Down Expand Up @@ -36,7 +37,7 @@ public function handles_redirect_responses()
{
$request = Request::create('/source');
$request->headers->add([
'Accept' => 'text/html; turbo-stream, text/html, application/xhtml+xml',
'Accept' => sprintf('%s, text/html, application/xhtml+xml', Turbo::TURBO_STREAM_FORMAT),
]);
$response = new RedirectResponse('/destination');
$next = function () use ($response) {
Expand Down Expand Up @@ -82,7 +83,7 @@ public function respects_the_redirects_to_property_of_the_validation_failed_exce
$request = Request::create('/test-models', 'POST');

$request->headers->add([
'Accept' => 'text/html; turbo-stream, text/html, application/xhtml+xml',
'Accept' => sprintf('%s, text/html, application/xhtml+xml', Turbo::TURBO_STREAM_FORMAT),
]);

$next = function () {
Expand Down Expand Up @@ -115,7 +116,7 @@ public function redirects_back_to_resource_create_routes_on_failed_validation_fo
})->name('test-models.store')->middleware(TurboMiddleware::class);

$response = $this->from('/source')->post(route('test-models.store'), [], [
'Accept' => 'text/html; turbo-stream, text/html, application/xhtml+xml',
'Accept' => sprintf('%s, text/html, application/xhtml+xml', Turbo::TURBO_STREAM_FORMAT),
]);

$response->assertRedirect(route('test-models.create'));
Expand All @@ -136,7 +137,7 @@ public function redirects_back_to_resource_edit_routes_on_failed_validation_foll
$testModel = TestModel::create(['name' => 'Dummy model']);

$response = $this->from('/source')->put(route('test-models.update', $testModel), [], [
'Accept' => 'text/html; turbo-stream, text/html, application/xhtml+xml',
'Accept' => sprintf('%s, text/html, application/xhtml+xml', Turbo::TURBO_STREAM_FORMAT),
]);

$response->assertRedirect(route('test-models.edit', $testModel));
Expand All @@ -153,7 +154,7 @@ public function lets_it_crash_when_redirect_route_does_not_exist()
$testModel = TestModel::create(['name' => 'Dummy model']);

$response = $this->from('/source')->put(route('test-models.update', $testModel), [], [
'Accept' => 'text/html; turbo-stream, text/html, application/xhtml+xml',
'Accept' => sprintf('%s, text/html, application/xhtml+xml', Turbo::TURBO_STREAM_FORMAT),
]);

$response->assertRedirect('/source');
Expand Down

0 comments on commit 61d19b1

Please sign in to comment.