Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:39:15+00:00 2026-06-05T23:39:15+00:00

In my test, I’d like to specify a cookie to go along with the

  • 0

In my test, I’d like to specify a cookie to go along with the request. I traced the code back to see how a cookie jar is used in the Client’s __construct. Though a var_dump here and a var_dump on the server side show no cookie being sent with the request. I also tried sending a simpler string with HTTP_COOKIE as shown.

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
class DefaultControllerTest extends WebTestCase {
    public function test() {
        $jar = new CookieJar();
        $cookie = new Cookie('locale2', 'fr', time() + 3600 * 24 * 7, '/', null, false, false);
        $jar->set($cookie);
        $client = static::createClient(array(), array(), $jar);  //this doesn't seem to attach cookies as expected!
        $crawler = $client->request(
            'GET', //method
            '/', //uri
            array(), //parameters
            array(), //files
            array(
                'HTTP_ACCEPT_LANGUAGE' => 'en_US',
                //'HTTP_COOKIE' => 'locale2=fr' //this doesn't work either!
            ) //server
        );

        var_dump($client->getRequest());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:39:16+00:00Added an answer on June 5, 2026 at 11:39 pm

    You have an error in code:

    $client = static::createClient(array(), array(), $jar); // Third parameter ?
    

    Method createClient is defined as following (for Symfony 2.0.0):

    static protected function createClient(array $options = array(), array $server = array())
    

    So, it takes only two parameters and there is no place for cookie, because createClient method takes an instance of client from the test container:

    $client = static::$kernel->getContainer()->get('test.client');
    $client->setServerParameters($server);
    
    return $client;
    

    Here is a definition for the test.client service:

    <service id="test.client" class="%test.client.class%" scope="prototype">
        <argument type="service" id="kernel" />
        <argument>%test.client.parameters%</argument>
        <argument type="service" id="test.client.history" />
        <argument type="service" id="test.client.cookiejar" />
    </service>
    
    <service id="test.client.cookiejar" class="%test.client.cookiejar.class%" scope="prototype" />
    

    Now we see, that cookie jar service is injected into test.client and have a scope prototype which means that new object will be created on each access to that service.

    However Client class has a method getCookieJar() and you can use it to set specific cookies for request (not tested, but expected to work):

    $client = static::createClient();
    $cookie = new Cookie('locale2', 'fr', time() + 3600 * 24 * 7, '/', null, false, false);
    $client->getCookieJar()->set($cookie);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

TEST SITE Look here to see code in action I have two arrays which
#test code: #!/bin/bash #~/test/test.sh trap echo 'testmessage' DEBUG while : do echo abc sleep
test.c: int main() { return 0; } I haven't used any flags (I am
I test password recovery, but there are errors. Rspec study recently. code (User Controller)
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Test string: the%20matrix%20 How can I delete the last three chars? Using this code
-------test code(test.html)------------ <!DOCTYPE html> <html> <head> <style> *{padding:0px;margin:0px;} </style> </head> <body style=background-color:orange;> <b onmousemove=tip(this);>A</b>
Test.xml: <?xml version=1.0 encoding=UTF-8?> <node>line1 line2 line3</node> CF code: <cfset xmlfile = ExpandPath(test.xml)> <cffile
Test code: #include <cmath> #include <cstdio> const int N = 4096; const float PI
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.