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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog