In Laravel 4 unit-test, how to set cookie in request?

Related searches

In my Laravel 4 app, I wish to test that a particular endpoint detects and responds correctly to cookies in the request. So, in my unit-test, I wish to set a cookie into the request and then perform the standard assertions on the response.

My call in the unit-test is of the form:

$response = $this->client->request($method, $endpoint, $params, $files, $server);

where I have specified my correct values for $method, $endpoint, $params, and empty $files and $server.

I have tried many variations, placing the cookie values inside $params or $server under various keys, but so far, no luck seeing the cookies show up in the controller under Cookie:get('myname).

How do I set a cookie with name 'myname' and value 'myvalue' in the request in a unit-test?

Update:

I imagine I could simply add a header to the request:

Cookie: myname=myvalue

but I gotta believe that there is a more Laravel-like way of doing it.

Update 2:

The above did not work.

Looks like we can do the following in a test that extends Illuminate\Foundation\Testing\TestCase:

$this->client->getCookieJar()->set(
    new \Symfony\Component\BrowserKit\Cookie('myname', 'myvalue')
);

The cookie will then be exposed in the controller via the standard:

$cookie = Cookie::get('myname'); // $cookie gets 'myvalue'

(via Laravel.io)

PHPUnit Test Setting Cookies, For other tests that require that cookie to be present should I mock the cookie. If so, what is the best method of doing so? Thanks in advance for� Cookies play an important role while dealing a user’s session on a web application. In this chapter, you will learn about working with cookies in Laravel based web applications. Creating a Cookie. Cookie can be created by global cookie helper of Laravel. It is an instance of Symfony\Component\HttpFoundation\Cookie. The cookie can be attached

EDIT: THIS ONLY WORKS FOR LARAVEL 5.2 OR GREATER

You can set Cookies with the call() method shown in the Documentation for TestCase

I used something similar recently. Here is an example:

$cookies = ['cookie_name' => 'cookie_value'];
$response = $this->call("get", 'target/url/for/request', [], $cookies);

Here is an additional issue thread on the Laravel Framework Github where someone had some confusion on how to call this. This further cemented my knowledge. Hope this helps!

EDIT:

There seems to be an issue with laravel dropping cookies it cannot decrypt. Be wary of this. Here is a Link to someone encountering this issue.

Their suggested solution (in case this link dies):

It seems that the problem is cookie encryption. Laravel will delete the cookies that it can't decrypt. As test cookies is not created by laravel (I simply use an array to test cookies as seen in the previous post), they are not encrypted and they will be deleted. I can think of two solutions for this problem.

  1. Simply remove cookie middleware from the list in app/Http/Kernel.php. All cookies will be sent to and received from the client as they are. If you don't need encryption or you want to read some cookies in the client side this is a nice solution.

  2. Edit middleware so that it won't encrypt or decrypt cookies during testing. Just add a code to handle function that checks the environment and continues without doing anything if environment is testing. This middleware is in Illuminate/Cookie/Middleware/EncryptCookie.php. So a framework update will undo your editing in this file. If you need encrpytion this is your only solution.

How to Set Cookies During Unit Test?, In a unit test, I'm trying to set cookies when calling a route. Test code is: $ response = $this->call('GET', 'api/user/logout', [], ['key' => 'abc123',� At its core, Laravel's authentication facilities are made up of "guards" and "providers". Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.

I did a test for a functionality that needed something very similar. I did my test using the $this->call() function:

$this->call($method, $url, $parameters = [], $cookies = ["name"=> Illuminate\Support\Facades\Crypt::encryptString("Leonardo")], $files = [], $server = [], $content = null);

If you need to see more or less how it works, just look for this file:

Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

HTTP Tests - Laravel, Basically, I'm unable to test if controller accepts cookie no matter what i try. Here's my test: public laravel / framework � Watch 1.1k � Star as per my answer on the Laracasts thread, the cookies need to be set like: public function [6.x] Add methods for sending cookies with test requests #30101. Merged. If you would like to obtain an instance of a PSR-7 request instead of a Laravel request, you will first need to install a few libraries. Laravel uses the Symfony HTTP Message Bridge component to convert typical Laravel requests and responses into PSR-7 compatible implementations:

you have to put cookie variable with return response then it will work

code will look like

$response->withCookie(cookie('name', 'value', $minutes));

return $response;

[5.2] Cookies sent in test are not present in request. � Issue #12032 , Laravel Unit Testing - cookie isn't attached with request Have you enabled cookie related middleware for api routes? check app/Http/Kernel.php to start developing now, head to the Larapass repository and install it in your application. Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. An ExampleTest.php file is provided in both the Feature and Unit test directories. After installing a new Laravel application, run phpunit on the command line to run your tests. Environment

Laravel Unit Testing, There are two primary ways of working with session data in Laravel: the global session helper and via a Request instance. First, let's look at accessing the session via a Request instance, which can be type-hinted on a controller method.

We create a user (this time persist in the database), set a specific password (say, ‘i-love-laravel’), make a request with user’s email and password, and assert he was logged in. Easily said

There is not much to see here as this is just a default page for a newly installed Laravel project. Setting Up the Database. To get started with testing, you need to set up your testing database. In this tutorial, we will keep things simple by using an in-memory SQLite database. It gives the advantage of improved speed for our test scripts.

Comments
  • I get this error: ErrorException: Undefined property: ExampleTest::$client
  • Does your ExampleTest extend Illuminate\Foundation\Testing\TestCase ?
  • Yes, it does extend that class.
  • Thanks for the reference and the detailed explanation. :D Sadly, it appears that the method you cite doesn't appear in Laravel 4, making its first appearance in v5.2.0 via the trait Concerns\MakesHttpRequests. Glad they added it, but in Laravel 4, looks like we're still stuck with the getCookieJar() workaround.
  • @DavidWeinraub You're right. I will edit to make this clear. But I will leave the answer up for people who are running more recent versions of laravel.
  • Thanks for the reference and the detailed explanation. :D Sadly, it appears that the method you cite doesn't appear in Laravel 4, making its first appearance in v5.2.0 via the trait Concerns\MakesHttpRequests. Glad they added it, but in Laravel 4, looks like we're still stuck with the getCookieJar() workaround.
  • That's how you send a cookie back to the client with his response. I need a way to set a cookie in the request so my code can read/process it.
  • no , my knowledge says that laravel sets cookie only if it is attached with return . in your case you can use that variable in your code and then send variable with cookie in response if it is to be set (according to your code flow / conditions )