def assertValidPlistResponse(self, resp):
        """
        Given a ``HttpResponse`` coming back from using the ``client``, assert
        that you get back:

        * An HTTP 200
        * The correct content-type (``application/x-plist``)
        * The content is valid binary plist data
        """
        self.assertHttpOK(resp)
        self.assertTrue(resp['Content-Type'].startswith('application/x-plist'))
        self.assertValidPlist(force_str(resp.content))
    def assertValidYAMLResponse(self, resp):
        """
        Given a ``HttpResponse`` coming back from using the ``client``, assert
        that you get back:

        * An HTTP 200
        * The correct content-type (``text/yaml``)
        * The content is valid YAML
        """
        self.assertHttpOK(resp)
        self.assertTrue(resp['Content-Type'].startswith('text/yaml'))
        self.assertValidYAML(force_str(resp.content))
示例#3
0
    def to_etree(self, data, options=None, name=None, depth=0):
        """
        Given some data, converts that data to an ``etree.Element`` suitable
        for use in the XML output.
        """
        if isinstance(data, (list, tuple)):
            element = Element(name or 'objects')
            if name:
                element = Element(name)
                element.set('type', 'list')
            else:
                element = Element('objects')
            for item in data:
                element.append(self.to_etree(item, options, depth=depth + 1))
                element[:] = sorted(element, key=lambda x: x.tag)
        elif isinstance(data, dict):
            if depth == 0:
                element = Element(name or 'response')
            else:
                element = Element(name or 'object')
                element.set('type', 'hash')
            for (key, value) in data.items():
                element.append(
                    self.to_etree(value, options, name=key, depth=depth + 1))
                element[:] = sorted(element, key=lambda x: x.tag)
        elif isinstance(data, Bundle):
            element = Element(name or 'object')
            for field_name, field_object in data.data.items():
                element.append(
                    self.to_etree(field_object,
                                  options,
                                  name=field_name,
                                  depth=depth + 1))
                element[:] = sorted(element, key=lambda x: x.tag)
        else:
            element = Element(name or 'value')
            simple_data = self.to_simple(data, options)
            data_type = get_type_string(simple_data)

            if data_type != 'string':
                element.set('type', get_type_string(simple_data))

            if data_type != 'null':
                if isinstance(simple_data, str):
                    element.text = simple_data
                else:
                    element.text = force_str(simple_data)

        return element
    def deserialize(self, content, format='application/json'):
        """
        Given some data and a format, calls the correct method to deserialize
        the data and returns the result.
        """
        method = None

        format = format.split(';')[0]

        method = self._from_methods.get(format)

        if method is None:
            raise UnsupportedDeserializationFormat(format)

        if isinstance(content, six.binary_type):
            content = force_str(content)

        return method(content)
示例#5
0
    def to_simple(self, data, options):
        """
        For a piece of data, attempts to recognize it and provide a simplified
        form of something complex.

        This brings complex Python data structures down to native types of the
        serialization format(s).
        """
        if data is None:
            return None

        data_type = type(data)

        stype = _STR

        for dt in data_type.__mro__:
            try:
                stype = _SIMPLETYPES[dt]
                break
            except KeyError:
                pass

        if stype == _NUM:
            return data
        if stype == _DICT:
            to_simple = self.to_simple
            return {key: to_simple(val, options) for key, val in data.items()}
        if stype == _STR:
            return force_str(data)
        if stype == _LIST:
            to_simple = self.to_simple
            return [to_simple(item, options) for item in data]
        if stype == _BUNDLE:
            to_simple = self.to_simple
            return {
                key: to_simple(val, options)
                for key, val in data.data.items()
            }
        if stype == _DATETIME:
            return self.format_datetime(data)
        if stype == _DATE:
            return self.format_date(data)
        if stype == _TIME:
            return self.format_time(data)
示例#6
0
 def test_unicode(self):
     access = ApiAccess(identifier="testing", accessed=0)
     self.assertEqual(force_str(access), 'testing @ 0')