예제 #1
0
    def test_to_json(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        self.assertEqual(
            serializer.to_json(sample_1),
            '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
예제 #2
0
 def test_to_xml2(self):
     serializer = Serializer()
     sample_2 = self.get_sample2()
     self.assertEqual(
         serializer.to_xml(sample_2),
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><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></response>'
     )
예제 #3
0
 def test_to_xml_single(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     self.assertEqual(
         serializer.to_xml(resource),
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object>'
     )
예제 #4
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"}'
     )
예제 #5
0
 def test_to_xml(self):
     serializer = Serializer()
     sample_1 = self.get_sample1()
     self.assertEqual(
         serializer.to_xml(sample_1),
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined></response>'
     )
예제 #6
0
 def test_from_json(self):
     serializer = Serializer()
     
     sample_1 = serializer.from_json('{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
     self.assertEqual(len(sample_1), 3)
     self.assertEqual(sample_1['name'], 'Daniel')
     self.assertEqual(sample_1['age'], 27)
     self.assertEqual(sample_1['date_joined'], u'2010-03-27')
예제 #7
0
    def test_from_json(self):
        serializer = Serializer()

        sample_1 = serializer.from_json(
            '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
        self.assertEqual(len(sample_1), 3)
        self.assertEqual(sample_1['name'], 'Daniel')
        self.assertEqual(sample_1['age'], 27)
        self.assertEqual(sample_1['date_joined'], u'2010-03-27')
예제 #8
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.replace('response', 'request')
     unserialized = serializer.from_xml(serialized)
     self.assertEqual(sample_data, unserialized)
예제 #9
0
    def test_to_jsonp(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        options = {'callback': 'myCallback'}
        self.assertEqual(
            serializer.to_jsonp(sample_1, options),
            'myCallback({"age": 27, "date_joined": "2010-03-27", "name": "Daniel"})'
        )
예제 #10
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.replace('response', 'request')
     unserialized = serializer.from_xml(serialized)
     self.assertEqual(sample_data, unserialized)
예제 #11
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"}}}')
예제 #12
0
 def test_from_xml(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><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'
         })
예제 #13
0
 def test_to_xml_nested(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     data = {
         'stuff': {
             'foo': 'bar',
             'object': resource,
         }
     }
     self.assertEqual(serializer.to_xml(data), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object></stuff></response>')
예제 #14
0
 def test_to_xml_nested(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     data = {
         'stuff': {
             'foo': 'bar',
             'object': resource,
         }
     }
     self.assertEqual(
         serializer.to_xml(data),
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object></stuff></response>'
     )
예제 #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_init(self):
        serializer_1 = Serializer()
        self.assertEqual(serializer_1.formats,
                         ['json', 'jsonp', 'xml', 'yaml', 'html'])
        self.assertEqual(
            serializer_1.content_types, {
                'xml': 'application/xml',
                'yaml': 'text/yaml',
                'json': 'application/json',
                'jsonp': 'text/javascript',
                'html': 'text/html'
            })
        self.assertEqual(serializer_1.supported_formats, [
            'application/json', 'text/javascript', 'application/xml',
            'text/yaml', 'text/html'
        ])

        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',
                'html': 'text/html'
            })
        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'])

        self.assertRaises(ImproperlyConfigured,
                          Serializer,
                          formats=['json', 'xml'],
                          content_types={'json': 'text/json'})
예제 #17
0
class ResourceOptions(object):
    """
    A configuration class for ``Resource``.
    
    Provides sane defaults and the logic needed to augment these settings with
    the internal ``class Meta`` used on ``Resource`` subclasses.
    """
    serializer = Serializer()
    authentication = Authentication()
    authorization = ReadOnlyAuthorization()
    cache = NoCache()
    throttle = BaseThrottle()
    validation = Validation()
    limit = getattr(settings, 'API_LIMIT_PER_PAGE', 20)
    route = None
    default_format = 'application/json'
    filtering = {}
    ordering = []
    object_class = None
    queryset = None
    fields = []
    excludes = []
    include_resource_uri = True
    include_absolute_url = False
    
    # only applies to TOC resource
    resources = []
    
    # only here for compatibility / deprecated
    api_name = None
    resource_name = ''
    
    def __new__(cls, meta=None):
        overrides = {}
        
        # Handle overrides.
        if meta:
            for override_name in dir(meta):
                # No internals please.
                if not override_name.startswith('_'):
                    overrides[override_name] = getattr(meta, override_name)
               
        return object.__new__(type('ResourceOptions', (cls,), overrides))
예제 #18
0
 def test_from_xml(self):
     serializer = Serializer()
     data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><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'})
예제 #19
0
 def test_to_xml(self):
     serializer = Serializer()
     sample_1 = self.get_sample1()
     self.assertEqual(serializer.to_xml(sample_1), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined></response>')
예제 #20
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())
예제 #21
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"}]'
     )
예제 #22
0
 def test_to_xml_multirepr(self):
     serializer = Serializer()
     self.assertEqual(
         serializer.to_xml(self.obj_list),
         '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object><object><updated>2010-03-31T20:05:00</updated><created>2010-03-31T20:05:00</created><title>Another Post</title><is_active type="boolean">True</is_active><slug>another-post</slug><content>The dog ate my cat today. He looks seriously uncomfortable.</content><id>2</id><resource_uri></resource_uri></object><object><updated>2010-04-01T20:05:00</updated><created>2010-04-01T20:05:00</created><title>Recent Volcanic Activity.</title><is_active type="boolean">True</is_active><slug>recent-volcanic-activity</slug><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><id>4</id><resource_uri></resource_uri></object><object><updated>2010-04-02T10:05:00</updated><created>2010-04-02T10:05:00</created><title>Granny\'s Gone</title><is_active type="boolean">True</is_active><slug>grannys-gone</slug><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><id>6</id><resource_uri></resource_uri></object></objects>'
     )
예제 #23
0
 def test_to_json(self):
     serializer = Serializer()
     
     sample_1 = self.get_sample1()
     self.assertEqual(serializer.to_json(sample_1), '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
예제 #24
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)
예제 #25
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')
     
     # Stow.
     old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING', 'iso-8601')
     
     settings.apiserver_DATETIME_FORMATTING = 'iso-8601'
     serializer = Serializer()
     self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16')
     
     settings.apiserver_DATETIME_FORMATTING = 'rfc-2822'
     serializer = Serializer()
     self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), u'16 Dec 2010')
     
     settings.apiserver_DATETIME_FORMATTING = 'random-garbage'
     serializer = Serializer()
     self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16')
     
     # Restore.
     settings.apiserver_DATETIME_FORMATTING = old_format
예제 #26
0
 def test_to_xml_multirepr(self):
     serializer = Serializer()
     self.assertEqual(serializer.to_xml(self.obj_list), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object><object><updated>2010-03-31T20:05:00</updated><created>2010-03-31T20:05:00</created><title>Another Post</title><is_active type="boolean">True</is_active><slug>another-post</slug><content>The dog ate my cat today. He looks seriously uncomfortable.</content><id>2</id><resource_uri></resource_uri></object><object><updated>2010-04-01T20:05:00</updated><created>2010-04-01T20:05:00</created><title>Recent Volcanic Activity.</title><is_active type="boolean">True</is_active><slug>recent-volcanic-activity</slug><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><id>4</id><resource_uri></resource_uri></object><object><updated>2010-04-02T10:05:00</updated><created>2010-04-02T10:05:00</created><title>Granny\'s Gone</title><is_active type="boolean">True</is_active><slug>grannys-gone</slug><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><id>6</id><resource_uri></resource_uri></object></objects>')
예제 #27
0
    def test_to_jsonp(self):
        serializer = Serializer()

        sample_1 = self.get_sample1()
        options = {'callback': 'myCallback'}
        self.assertEqual(serializer.to_jsonp(sample_1, options), 'myCallback({"age": 27, "date_joined": "2010-03-27", "name": "Daniel"})')
예제 #28
0
 def test_to_xml_single(self):
     serializer = Serializer()
     resource = self.obj_list[0]
     self.assertEqual(serializer.to_xml(resource), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object>')
예제 #29
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"}]')
예제 #30
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"}')
예제 #31
0
    def test_format_datetime(self):
        serializer = Serializer()
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            '2010-12-16T02:31:33')

        serializer = Serializer(datetime_formatting='iso-8601')
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            '2010-12-16T02:31:33')

        serializer = Serializer(datetime_formatting='rfc-2822')
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            u'Thu, 16 Dec 2010 02:31:33 -0600')

        serializer = Serializer(datetime_formatting='random-garbage')
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            '2010-12-16T02:31:33')

        # Stow.
        old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING',
                             'iso-8601')

        settings.apiserver_DATETIME_FORMATTING = 'iso-8601'
        serializer = Serializer()
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            '2010-12-16T02:31:33')

        settings.apiserver_DATETIME_FORMATTING = 'rfc-2822'
        serializer = Serializer()
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            u'Thu, 16 Dec 2010 02:31:33 -0600')

        settings.apiserver_DATETIME_FORMATTING = 'random-garbage'
        serializer = Serializer()
        self.assertEqual(
            serializer.format_datetime(
                datetime.datetime(2010, 12, 16, 2, 31, 33)),
            '2010-12-16T02:31:33')

        # Restore.
        settings.apiserver_DATETIME_FORMATTING = old_format
예제 #32
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)
예제 #33
0
    def test_determine_format(self):
        serializer = Serializer()
        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')

        request.GET = {'format': 'jsonp'}
        self.assertEqual(determine_format(request, 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': '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')

        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        self.assertEqual(determine_format(request, 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': 'text/html'}
        self.assertEqual(determine_format(request, serializer), 'text/html')

        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')
예제 #34
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='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')
     
     # Stow.
     old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING', 'iso-8601')
     
     settings.apiserver_DATETIME_FORMATTING = 'iso-8601'
     serializer = Serializer()
     self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33')
     
     settings.apiserver_DATETIME_FORMATTING = 'rfc-2822'
     serializer = Serializer()
     self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), u'02:31:33 -0600')
     
     settings.apiserver_DATETIME_FORMATTING = 'random-garbage'
     serializer = Serializer()
     self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33')
     
     # Restore.
     settings.apiserver_DATETIME_FORMATTING = old_format