title: How to set a cookie in a django test case?
body: <p>I'm struggling to figure this one out, sessions work when i run my application normally but i can't figure out how to set data in the session in my test case.</p>
  
  <p>The docs say in a test case you have to save the session to apply the changes before making the request. <a href="https://docs.djangoproject.com/en/1.2/topics/testing/#persistent-state" rel="nofollow">https://docs.djangoproject.com/en/1.2/topics/testing/#persistent-state</a></p>
  
  <p>e.g.</p>
  
  <pre><code>from django.test import TestCase
  
  class TestLogin(TestCase):
  
      def test_processuser(self):
          redirect = '/processuser/'
          session = self.client.session
          session["id"] = '1234'
          session.save()
          response = self.client.get(redirect)
  </code></pre>
  
  <p>However the session object returned from self.client.session is just a normal python dict?</p>
  
  <p>Diging into the code the Client.session call is this:</p>
  
  <pre><code>def _session(self):
      """
      Obtains the current session variables.
      """
      if 'django.contrib.sessions' in settings.INSTALLED_APPS:
          engine = import_module(settings.SESSION_ENGINE)
          cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
          if cookie:
              return engine.SessionStore(cookie.value)
      return {}
  session = property(_session)
  </code></pre>
  
  <p><code>cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)</code> returns <code>None</code> so it just returns a dict in stead of a session store. </p>
  
  <p>It looks like i have to do some more preparation in the test client before i save a session? Not really got much experience in this any help would be appreciated.</p>
  
  <p>Django 1.2.5
  Python 2.6.5</p>
  
  <p>Cheers,</p>
  
  <p>Asim.</p>