Пример #1
0
 def test_to_json_single(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     self.assertEqual(
         serializer.to_json(resource),
         '{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": 1, "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}'
     )
Пример #2
0
 def test_to_json_decimal_list_dict(self):
     serializer = Serializer()
     resource = self.another_obj_list[0]
     self.assertEqual(
         serializer.to_json(resource),
         '{"aliases": ["Mr. Smith", "John Doe"], "content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": 1, "is_active": true, "meta": {"threat": "high"}, "owed": "102.57", "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}'
     )
Пример #3
0
    def test_init(self):
        serializer_1 = Serializer()
        self.assertEqual(serializer_1.formats,
                         ['json', 'xml', 'yaml', 'plist'])
        self.assertEqual(
            serializer_1.content_types, {
                'xml': 'application/xml',
                'yaml': 'text/yaml',
                'json': 'application/json',
                'jsonp': 'text/javascript',
                'plist': 'application/x-plist'
            })
        self.assertEqual(serializer_1.supported_formats, [
            'application/json', 'application/xml', 'text/yaml',
            'application/x-plist'
        ])

        serializer_2 = Serializer(formats=['json', 'xml'])
        self.assertEqual(serializer_2.formats, ['json', 'xml'])
        self.assertEqual(
            serializer_2.content_types, {
                'xml': 'application/xml',
                'yaml': 'text/yaml',
                'json': 'application/json',
                'jsonp': 'text/javascript',
                'plist': 'application/x-plist'
            })
        self.assertEqual(serializer_2.supported_formats,
                         ['application/json', 'application/xml'])

        serializer_3 = Serializer(formats=['json', 'xml'],
                                  content_types={
                                      'json': 'text/json',
                                      'xml': 'application/xml'
                                  })
        self.assertEqual(serializer_3.formats, ['json', 'xml'])
        self.assertEqual(serializer_3.content_types, {
            'xml': 'application/xml',
            'json': 'text/json'
        })
        self.assertEqual(serializer_3.supported_formats,
                         ['text/json', 'application/xml'])

        serializer_4 = Serializer(formats=['plist', 'json'],
                                  content_types={
                                      'plist': 'application/x-plist',
                                      'json': 'application/json'
                                  })
        self.assertEqual(serializer_4.formats, ['plist', 'json'])
        self.assertEqual(serializer_4.content_types, {
            'plist': 'application/x-plist',
            'json': 'application/json'
        })
        self.assertEqual(serializer_4.supported_formats,
                         ['application/x-plist', 'application/json'])

        self.assertRaises(ImproperlyConfigured,
                          Serializer,
                          formats=['json', 'xml'],
                          content_types={'json': 'text/json'})
Пример #4
0
    def test_to_jsonp(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        options = {'callback': 'myCallback'}
        serialized = serializer.to_jsonp(sample_1, options=options)
        serialized_json = serializer.to_json(sample_1)
        self.assertEqual('myCallback(%s)' % serialized_json, serialized)
Пример #5
0
    def test_to_json(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        self.assertEqual(
            serializer.to_json(sample_1),
            u'{"age": 27, "date_joined": "2010-03-27", "name": "Daniel", "snowman": "☃"}'
        )
Пример #6
0
 def test_to_xml_multirepr(self):
     serializer = Serializer()
     binary_xml = serializer.to_xml(self.obj_list)
     unicode_xml = binary_xml.decode('utf-8')
     self.assertEqual(
         unicode_xml,
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object><object><content>The dog ate my cat today. He looks seriously uncomfortable.</content><created>2010-03-31T20:05:00</created><id type="integer">2</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>another-post</slug><title>Another Post</title><updated>2010-03-31T20:05:00</updated></object><object><content>My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.</content><created>2010-04-01T20:05:00</created><id type="integer">4</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>recent-volcanic-activity</slug><title>Recent Volcanic Activity.</title><updated>2010-04-01T20:05:00</updated></object><object><content>Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!</content><created>2010-04-02T10:05:00</created><id type="integer">6</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>grannys-gone</slug><title>Granny\'s Gone</title><updated>2010-04-02T10:05:00</updated></object></objects>'
     )
Пример #7
0
 def test_round_trip_xml(self):
     serializer = Serializer()
     sample_data = self.get_sample2()
     serialized = serializer.to_xml(sample_data)
     # "response" tags need to be changed to "request" to deserialize properly.
     # A string substitution works here.
     serialized = serialized.decode('utf-8').replace('response', 'request')
     unserialized = serializer.from_xml(serialized)
     self.assertEqual(sample_data, unserialized)
Пример #8
0
 def test_to_xml2(self):
     serializer = Serializer()
     sample_2 = self.get_sample2()
     binary_xml = serializer.to_xml(sample_2)
     unicode_xml = binary_xml.decode('utf-8')
     self.assertEqual(
         unicode_xml,
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><false type="boolean">False</false><somehash type="hash"><foo>bar</foo><pi type="float">3.14</pi></somehash><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somestring>hello</somestring><true type="boolean">True</true></response>'
     )
Пример #9
0
 def test_to_xml_single(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     binary_xml = serializer.to_xml(resource)
     unicode_xml = binary_xml.decode('utf-8')
     self.assertEqual(
         unicode_xml,
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object>'
     )
Пример #10
0
    def test_from_json(self):
        serializer = Serializer()

        sample_1 = serializer.from_json(
            u'{"age": 27, "date_joined": "2010-03-27", "name": "Daniel", "snowman": "☃"}'
        )
        self.assertEqual(len(sample_1), 4)
        self.assertEqual(sample_1['name'], 'Daniel')
        self.assertEqual(sample_1['age'], 27)
        self.assertEqual(sample_1['date_joined'], u'2010-03-27')
        self.assertEqual(sample_1['snowman'], u'☃')
Пример #11
0
    def test_invalid_jsonp_characters(self):
        """
        The newline characters \u2028 and \u2029 need to be escaped
        in JSONP.
        """
        serializer = Serializer()

        jsonp = serializer.to_jsonp({'foo': u'Hello \u2028\u2029world!'},
                                    {'callback': 'callback'})
        self.assertEqual(jsonp,
                         u'callback({"foo": "Hello \\u2028\\u2029world!"})')
Пример #12
0
    def test_from_plist(self):
        serializer = Serializer()

        sample_1 = serializer.from_plist(
            b'bplist00bybiplist1.0\x00\xd4\x01\x02\x03\x04\x05\x06\x07\x08WsnowmanSageTname[date_joineda&\x03\x10\x1bf\x00D\x00a\x00n\x00i\x00e\x00lZ2010-03-27\x15\x1e&*/;>@M\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00X'
        )
        self.assertEqual(len(sample_1), 4)
        self.assertEqual(sample_1['name'], 'Daniel')
        self.assertEqual(sample_1['age'], 27)
        self.assertEqual(sample_1['date_joined'], '2010-03-27')
        self.assertEqual(sample_1['snowman'], u'☃')
Пример #13
0
 def test_from_xml(self):
     serializer = Serializer()
     data = u'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><snowman>☃</snowman><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined><rocksdahouse type="boolean">True</rocksdahouse></request>'
     self.assertEqual(
         serializer.from_xml(data), {
             'rocksdahouse': True,
             'age': 27,
             'name': 'Daniel',
             'date_joined': '2010-03-27',
             'snowman': u'☃'
         })
Пример #14
0
    def __init__(self, serializer=None):
        """
        Sets up a fresh ``TestApiClient`` instance.

        If you are employing a custom serializer, you can pass the class to the
        ``serializer=`` kwarg.
        """
        self.client = Client()
        self.serializer = serializer

        if not self.serializer:
            self.serializer = Serializer()
Пример #15
0
 def test_to_json_nested(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     data = {
         'stuff': {
             'foo': 'bar',
             'object': resource,
         }
     }
     self.assertEqual(
         serializer.to_json(data),
         '{"stuff": {"foo": "bar", "object": {"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": 1, "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}}}'
     )
Пример #16
0
 def test_to_xml(self):
     serializer = Serializer()
     sample_1 = self.get_sample1()
     # This needs a little explanation.
     # From http://lxml.de/parsing.html, what comes out of ``tostring``
     # (despite encoding as UTF-8) is a bytestring. This is because that's
     # what other libraries expect (& will do the decode). We decode here
     # so we can make extra special sure it looks right.
     binary_xml = serializer.to_xml(sample_1)
     unicode_xml = binary_xml.decode('utf-8')
     self.assertEqual(
         unicode_xml,
         u'<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><date_joined>2010-03-27</date_joined><name>Daniel</name><snowman>☃</snowman></response>'
     )
Пример #17
0
 def test_to_xml_nested(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     data = {
         'stuff': {
             'foo': 'bar',
             'object': resource,
         }
     }
     binary_xml = serializer.to_xml(data)
     unicode_xml = binary_xml.decode('utf-8')
     self.assertEqual(
         unicode_xml,
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><created>2010-03-30T20:05:00</created><id type="integer">1</id><is_active type="boolean">True</is_active><resource_uri></resource_uri><slug>first-post</slug><title>First Post!</title><updated>2010-03-30T20:05:00</updated></object></stuff></response>'
     )
Пример #18
0
 def test_unsafe_xml(self):
     """
     Entity expansion can be used to cause large memory usage after
     deserialization for little memory usage from the attacker.
     See https://pypi.python.org/pypi/defusedxml for more information.
     """
     serializer = Serializer()
     data = """<!DOCTYPE bomb [<!ENTITY a "evil chars">]>
     <bomb>&a;</bomb>
     """
     self.assertRaises(BadRequest, serializer.from_xml, data)
Пример #19
0
    def test_default_formats_setting(self):
        with self.settings(BOXME_DEFAULT_FORMATS=('json', 'xml')):
            # Confirm that the setting will override the default values:
            s = Serializer()
            self.assertEqual(list(s.formats), ['json', 'xml'])
            self.assertEqual(list(s.supported_formats),
                             ['application/json', 'application/xml'])
            self.assertEqual(
                s.content_types, {
                    'xml': 'application/xml',
                    'yaml': 'text/yaml',
                    'json': 'application/json',
                    'jsonp': 'text/javascript',
                    'plist': 'application/x-plist'
                })

            # Confirm that subclasses which set their own formats list won't be overriden:
            class JSONSerializer(Serializer):
                formats = ['json']

            js = JSONSerializer()
            self.assertEqual(list(js.formats), ['json'])
            self.assertEqual(list(js.supported_formats), ['application/json'])
Пример #20
0
 def test_round_trip_yaml(self):
     serializer = Serializer()
     sample_data = self.get_sample2()
     serialized = serializer.to_yaml(sample_data)
     unserialized = serializer.from_yaml(serialized)
     self.assertEqual(sample_data, unserialized)
Пример #21
0
 def test__to_simple__time(self):
     serializer = Serializer()
     self.assertEqual(serializer.to_simple(datetime.time(2, 31, 33), None),
                      '02:31:33')
Пример #22
0
    def test_format_date(self):
        serializer = Serializer()
        self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)),
                         '2010-12-16')

        serializer = Serializer(datetime_formatting='iso-8601')
        self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)),
                         '2010-12-16')

        serializer = Serializer(datetime_formatting='rfc-2822')
        self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)),
                         u'16 Dec 2010')

        serializer = Serializer(datetime_formatting='random-garbage')
        self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)),
                         '2010-12-16')

        with self.settings(BOXME_DATETIME_FORMATTING='iso-8601'):
            serializer = Serializer()
            self.assertEqual(
                serializer.format_date(datetime.date(2010, 12, 16)),
                '2010-12-16')

        with self.settings(BOXME_DATETIME_FORMATTING='rfc-2822'):
            serializer = Serializer()
            self.assertEqual(
                serializer.format_date(datetime.date(2010, 12, 16)),
                u'16 Dec 2010')

        with self.settings(BOXME_DATETIME_FORMATTING='random-garbage'):
            serializer = Serializer()
            self.assertEqual(
                serializer.format_date(datetime.date(2010, 12, 16)),
                '2010-12-16')
Пример #23
0
 def test_to_json_multirepr(self):
     serializer = Serializer()
     self.assertEqual(
         serializer.to_json(self.obj_list),
         '[{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": 1, "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31T20:05:00", "id": 2, "is_active": true, "resource_uri": "", "slug": "another-post", "title": "Another Post", "updated": "2010-03-31T20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01T20:05:00", "id": 4, "is_active": true, "resource_uri": "", "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01T20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02T10:05:00", "id": 6, "is_active": true, "resource_uri": "", "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02T10:05:00"}]'
     )
Пример #24
0
    def test_format_time(self):
        serializer = Serializer()
        self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                         '02:31:33')

        serializer = Serializer(datetime_formatting='iso-8601')
        self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                         '02:31:33')

        serializer = Serializer(datetime_formatting='iso-8601-strict')
        self.assertEqual(serializer.format_time(datetime.time(2, 31, 33, 10)),
                         '02:31:33')

        serializer = Serializer(datetime_formatting='rfc-2822')
        self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                         u'02:31:33 -0600')

        serializer = Serializer(datetime_formatting='random-garbage')
        self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                         '02:31:33')

        with self.settings(BOXME_DATETIME_FORMATTING='iso-8601'):
            serializer = Serializer()
            self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                             '02:31:33')

        with self.settings(BOXME_DATETIME_FORMATTING='iso-8601-strict'):
            serializer = Serializer()
            self.assertEqual(
                serializer.format_time(datetime.time(2, 31, 33, 10)),
                '02:31:33')

        with self.settings(BOXME_DATETIME_FORMATTING='rfc-2822'):
            serializer = Serializer()
            self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                             u'02:31:33 -0600')

        with self.settings(BOXME_DATETIME_FORMATTING='random-garbage'):
            serializer = Serializer()
            self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)),
                             '02:31:33')
Пример #25
0
 def test_from_xml2(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somehash type="hash"><pi type="float">3.14</pi><foo>bar</foo></somehash><false type="boolean">False</false><true type="boolean">True</true><somestring>hello</somestring></request>'
     self.assertEqual(serializer.from_xml(data), self.get_sample2())
Пример #26
0
    def test_to_plist(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        self.assertTrue(
            serializer.to_plist(sample_1).startswith(b'bplist00bybiplist1.0'))
Пример #27
0
 def test_malformed_xml(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><somelist type="list"><valueNO CARRIER'
     self.assertRaises(BadRequest, serializer.from_xml, data)
Пример #28
0
 def test_from_broken_json(self):
     serializer = Serializer()
     data = '{"foo": "bar",NO CARRIER'
     self.assertRaises(BadRequest, serializer.from_json, data)
Пример #29
0
    def test_determine_format(self):
        serializer = Serializer()
        full_serializer = Serializer(
            formats=['json', 'jsonp', 'xml', 'yaml', 'plist'])
        request = HttpRequest()

        # Default.
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Test forcing the ``format`` parameter.
        request.GET = {'format': 'json'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Disabled by default.
        request.GET = {'format': 'jsonp'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Explicitly enabled.
        request.GET = {'format': 'jsonp'}
        self.assertEqual(determine_format(request, full_serializer),
                         'text/javascript')

        request.GET = {'format': 'xml'}
        self.assertEqual(determine_format(request, serializer),
                         'application/xml')

        request.GET = {'format': 'yaml'}
        self.assertEqual(determine_format(request, serializer), 'text/yaml')

        request.GET = {'format': 'plist'}
        self.assertEqual(determine_format(request, serializer),
                         'application/x-plist')

        request.GET = {'format': 'foo'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Test the ``Accept`` header.
        request.META = {'HTTP_ACCEPT': 'application/json'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Again, disabled by default.
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # Again, explicitly enabled.
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        self.assertEqual(determine_format(request, full_serializer),
                         'text/javascript')

        request.META = {'HTTP_ACCEPT': 'application/xml'}
        self.assertEqual(determine_format(request, serializer),
                         'application/xml')

        request.META = {'HTTP_ACCEPT': 'text/yaml'}
        self.assertEqual(determine_format(request, serializer), 'text/yaml')

        request.META = {'HTTP_ACCEPT': 'application/x-plist'}
        self.assertEqual(determine_format(request, serializer),
                         'application/x-plist')

        # unsupported text/html
        request.META = {'HTTP_ACCEPT': 'text/html'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        # unsupported binary
        request.META = {'HTTP_ACCEPT': 'application/octet-stream'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        request.META = {'HTTP_ACCEPT': '*/*'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        request.META = {
            'HTTP_ACCEPT': 'application/json,application/xml;q=0.9,*/*;q=0.8'
        }
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        request.META = {
            'HTTP_ACCEPT':
            'text/plain,application/xml,application/json;q=0.9,*/*;q=0.8'
        }
        self.assertEqual(determine_format(request, serializer),
                         'application/xml')

        request.META = {'HTTP_ACCEPT': 'application/json; charset=UTF-8'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        request.META = {'HTTP_ACCEPT': 'text/javascript,application/json'}
        self.assertEqual(determine_format(request, serializer),
                         'application/json')

        request.META = {'HTTP_ACCEPT': 'bogon'}
        self.assertRaises(BadRequest, determine_format, request, serializer)

        # typical browser (firefox, chrome)
        request.META = {
            'HTTP_ACCEPT':
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
        }
        self.assertEqual(determine_format(request, serializer),
                         'application/xml')
Пример #30
0
 def test_unsafe_yaml(self):
     serializer = Serializer()
     evil_data = UnsafeObject()
     serialized = yaml.dump(evil_data)
     self.assertRaises(yaml.constructor.ConstructorError,
                       serializer.from_yaml, serialized)