Esempio n. 1
0
    def fetch_from_db(self):
        """
        Fetch a user's session from the database.

        @return: Native Python object, or None if none found
        """

        sql = \
        """
        SELECT values FROM session
        WHERE guid = %s AND active = TRUE;
        """ % (db.cstr(self._guid))
        
        try:
            self.connect_db()
            self._cursor.execute(sql)
            self._record = self._cursor.fetchone()
        except: #self._conn.error.OperationalError, ex:
            return {'SESSION-ERROR':'DATABASE UNAVAILABLE!'}

        if self._record is None:
            return {}
        else:
            try:
                return json.decode(self._record['values'])
            except ValueError, ex:
                raise "Unable to json.decode session", ex
Esempio n. 2
0
 def test_decode_by_json(self):
     encoded = json.encode(self.col)
     decoded = json.decode(encoded)
     self.assertEquals(decoded['age'], self.col.age)
     self.assertEquals(decoded['location'], self.col.location)
     self.assertEquals(decoded['name'], self.col.name)
     self.assertEquals(type(decoded), type({}))
Esempio n. 3
0
    def test_expose_json_via_kwargs(self):
        @webservice.expose(transport='json')
        def helper(foo):
            return self.payload

        response = json.decode(helper(self.controller))
        self.assertEquals(response['data'], self.payload)
        self.assertEquals(response['success'], True)
Esempio n. 4
0
    def test_expose_default(self):
        @webservice.expose()
        def helper(foo):
            return self.payload

        response = json.decode(helper(self.controller))
        self.assertEquals(response['data'], self.payload)
        self.assertEquals(response['success'], True)
Esempio n. 5
0
    def test_expose_json_via_get(self):
        # Simulate a GET arg of 'transport'
        self.controller.env.form['transport'] = 'json'

        @webservice.expose()
        def helper(foo):
            return self.payload

        response = json.decode(helper(self.controller))
        self.assertEquals(response['data'], self.payload)
        self.assertEquals(response['success'], True)
Esempio n. 6
0
    def fetch_from_cache(self):
        """
        Fetch a user's session from cache.  If the session isn't found,
        this method will return None

        @return: Native Python object, or None if not found
        """

        values = self._cache.get(self.mkey())
        if values is None:
            return None
        else:
            return json.decode(values)
Esempio n. 7
0
    def test_expose_json_via_x_header(self):
        @webservice.expose(x_header=True)
        def helper(foo):
            return self.payload

        HEADER_FOUND = False
        html_body = helper(self.controller)
        for header in self.controller.env.headers:
            if header[0] == 'X-JSON':
                HEADER_FOUND = True
                response = json.decode(header[1])
                break

        self.assertEquals(HEADER_FOUND, True)
        self.assertEquals(response['data'], self.payload)
        self.assertEquals(response['success'], True)
Esempio n. 8
0
    def decode(self, data):
        """
        Decode the session using the specified transport (cPickle by
        default).

        @param data: Session data to be decoded
        @type data: str
        @return: Dict
        """
        
        # Detect already decoded data
        if data is None:
            return None # TODO: Is this the right thing to do?
        elif isinstance(data, dict):
            return data

        # Decode using the desired transport
        data = str2unicode(data)
        if self._transport == CPICKLE:
            return cPickle.loads(data)
        else:
            return json.decode(data)
Esempio n. 9
0
 def test_json_encoding(self):
     encoded = json.encode(self.human)
     decoded = json.decode(encoded)
     self.assertEquals(decoded['foot'], self.human.foot)
     self.assertEquals(decoded['head'], self.human.head)
Esempio n. 10
0
 def decode(msg):
     try:
         return json.decode(msg)
     except ValueError, er:
         raise InvalidMessageEncodingError(str(msg))