dprc88435 2019-06-23 16:20
浏览 178
已采纳

Laravel - 在中间件中测试独特的cookie创建

I have a middleware that creates cookies for guest users and I'm struggling to test it. This is my middleware's handle function:


public function handle($request, Closure $next)
{
  $guestCookieKey = 'guest';

  if ($request->cookie($guestCookieKey)) {
    return $next($request);
  }

  return $next($request)->cookie($guestCookieKey, createGuestCookieId());
}

Testing this on the browser works perfectly: when request hasn't this cookie, it creates a new one. When it does have it, we just forward the request chain.

The problem is while testing this. Here's what I'm doing:

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestResponse;

class GuestCookieTest extends TestCase
{
  public function testItCreatesOnlyOneCookiePerResponse()
  {
    $firstResponse = $this->get('/');
    $secondResponse = $this->get('/');

    $this->assertEquals(
      $firstResponse->headers->getCookies()[0],
      $secondResponse->headers->getCookies()[0]
    );
  }
}

The error shows diffs between the two values:

Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 Symfony\Component\HttpFoundation\Cookie Object (
     'name' => 'guest'
-    'value' => 'eyJpdiI6IkttV2I2dldNelNkWXpyQXRWRUFlRUE9PSIsInZhbHVlIjoiQnBFYVlZNTNtYThjOWFxTTdLRXh4Zz09IiwibWFjIjoiZjY0NmEyNTMyMmM2MjJkNThmNmM5NWMxMDc2ZThmOTZjNDJhZTJjMmJhMGM0YTY0N2Q4NDg5YWEwNjI1ODEwZiJ9'
+    'value' => 'eyJpdiI6InRSMUVOSEZESm5xblwvOUU3aHQweGZ3PT0iLCJ2YWx1ZSI6ImhjU1lcL2pJUU1VbGxTN1BJQTdPWXBBPT0iLCJtYWMiOiJmM2QyYjQ3NzU5NWU5Nzk2Yjg0Yzg4MmFlNGFmYTdkNThlNjZhNzVhMjE3YjUxODhlNzRkMjA0MWQzZmEyODM2In0='
     'domain' => null
     'expire' => 0
     'path' => '/'

It's like $this->get was not executed in the same environment (?), not preserving cookies set previously and creating unique calls and data for each call. This does make sense, but how would you test the creation of a guest cookie if no other with the same name is set?

  • 写回答

1条回答 默认 最新

  • duanhan5388 2019-06-25 22:56
    关注

    Despite of knowing about E2E test and tools, I had no idea there was something like Laravel Dusk, which solved my problem.

    After installing Laravel Dusk, it creates a tests/Browser folder.

    I just had to create a test file like GuestCookieTest.php with the following content:

    <?php
    
    namespace Tests\Browser;
    
    use Tests\DuskTestCase;
    use Laravel\Dusk\Browser;
    use Illuminate\Foundation\Testing\DatabaseMigrations;
    
    class GuestCookieTest extends DuskTestCase
    {
        public function testItDoesNotOverwriteGuestCookieValue()
        {
            $this->browse(function (Browser $browser) {
                $firstCookieVal = $browser->visit('/')->cookie(config('guest'));
                $secondCookieVal = $browser->visit('/')->cookie(config('guest'));
                $this->assertEquals($firstCookieVal, $secondCookieVal);
            });
        }
    }
    

    Absolutely simple!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 设计一个光控计数器,全部用ttl芯片
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开