Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a5efc78e66
Fetching contributors…

Cannot retrieve contributors at this time

file 117 lines (93 sloc) 3.187 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
import unittest
import socket

import cherrypy
from cherrypy.test import webtest

# testing that redis-py is available and that we have a redis server running
try:
    import redis

    host, port = '127.0.0.1', 6379
    for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM):
        af, socktype, proto, canonname, sa = res
        s = None
        try:
            s = socket.socket(af, socktype, proto)
            s.settimeout(1.0)
            s.connect((host, port))
            s.close()
        except socket.error:
            if s:
                s.close()
            raise
        break

except ImportError:

  class RedisSessionTest(unittest.TestCase):
      def test_nothing(self):
          self.fail("redis-py not available")

except socket.error:

  class RedisSessionTest(unittest.TestCase):
      def test_nothing(self):
          self.fail("redis not reachable")

# all good to go we can test
else:

    import cherrys
    cherrypy.lib.sessions.RedisSession = cherrys.RedisSession

    class RedisSessionTest(webtest.WebCase):

        interactive = False

        @classmethod
        def setUpClass(cls):
            # configuring CP instead of webCase (getPage - openUrl on port 8000)
            cherrypy.config.update({
                'server.socket_port' : 8000,
                'log.screen' : False,
                'tools.sessions.on' : True,
                'tools.sessions.storage_type' : 'redis'
            })
            cherrypy.tree.mount(App())
            cherrypy.engine.start()

        def test_server_working(self):
            self.getPage('/')
            self.assertStatus(200)

        def test_deleting_non_existing_key_fails(self):
            self.getPage('/delete')
            self.assertStatus(500)

        def test_deleting_stored_data(self):
            self.getPage('/store')
            self.assertStatus(200)
            # first getPage call sets a cookie
            # second getPage call needs to be aware of the cookie
            self.getPage('/delete', self.cookies)
            self.assertStatus(200)

        def test_storing_data(self):
            self.getPage('/store')
            self.assertStatus(200)
            self.assertBody('redis')

        def test_retrieving_stored_data(self):
            self.getPage('/retrieve')
            self.assertStatus(500)
            self.getPage('/store', self.cookies)
            self.assertStatus(200)
            self.assertBody('redis')
            self.getPage('/retrieve', self.cookies)
            self.assertStatus(200)

        @classmethod
        def tearDownClass(cls):
            cherrypy.engine.exit()

class App(object):
    """ A basic application to test sessions """

    @cherrypy.expose
    def index(self):
        return 'index'

    @cherrypy.expose
    def store(self):
        cherrypy.session['data'] = 'redis'
        return cherrypy.session['data']

    @cherrypy.expose
    def delete(self):
        del cherrypy.session['data']
        return 'deleted'

    @cherrypy.expose
    def retrieve(self):
        return cherrypy.session['data']

if __name__ == '__main__':
  unittest.main()
Something went wrong with that request. Please try again.