如何为http假冒的饼干伪造响应?

发布于 2025-01-23 10:06:20 字数 667 浏览 3 评论 0原文

我想与cookie嘲笑Laravel的HTTP响应。我尝试了:

Http::fake([
    'my-mocked-route' => Http::response(['foo' => 'bar'], 200)->withCookie('a', 10),
]);

但是我收到

致电未定义的方法guzzlehttp \ promise \ eachillpromise :: withcookie()

如果我只使用cookie而不是with cookie。另外,我会添加cookie属性。

我还尝试了

Http::fake([
     
        'qnnect' => Http::response(['foo' => 'bar'], 200, ['Cookie' => 'a=10; Expiration=Wed, 21 Oct 2015 07:28:00 GMT']),
    ]);

,尽管响应在标题中包含正确的cookie,但$ response-> cookie()返回一个空的cookiejar。

是否有可能在响应中嘲笑饼干?

I want to mock a HTTP response in Laravel with cookie. I tried this:

Http::fake([
    'my-mocked-route' => Http::response(['foo' => 'bar'], 200)->withCookie('a', 10),
]);

but I receive

Call to undefined method GuzzleHttp\Promise\FulfilledPromise::withCookie()

same if I just use cookie instead of withCookie. Also, I would to add Cookie attributes aswell.

I also tried

Http::fake([
     
        'qnnect' => Http::response(['foo' => 'bar'], 200, ['Cookie' => 'a=10; Expiration=Wed, 21 Oct 2015 07:28:00 GMT']),
    ]);

And although the response contains correct cookies in the header, $response->cookies() returns an empty CookieJar.

Is there a possibility to mock cookies in the respond?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2025-01-30 10:06:20

正确的标头是set-cookie而不是cookie。还需要域。

  Http::fake([
    'home' => Http::response('Ok', 200, ['Set-Cookie' => 'foo=bar; domain=.'])
  ]);

  $response = Http::get('home');
  dump($response->cookies()->getCookieByName('foo')->getValue());

输出将为bar

The correct header is Set-Cookie and not Cookie. Domain is also required.

  Http::fake([
    'home' => Http::response('Ok', 200, ['Set-Cookie' => 'foo=bar; domain=.'])
  ]);

  $response = Http::get('home');
  dump($response->cookies()->getCookieByName('foo')->getValue());

Output will be bar

倦话 2025-01-30 10:06:20

- > with cookies是您可以在http :: fake()上使用的方法

->withCookies is a method you can use on Http::fake() and not on Http::response()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文