Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cope with bad JSON

  • Loading branch information...
commit 7495a2ba0db09104bd4ef33cea1cd575161ad93e 1 parent aa3945f
Andy McKay authored December 31, 2012
7  metrics/management/commands/migrate.py
@@ -49,10 +49,11 @@ def handle(self, *args, **options):
49 49
                 uuid = 'global_stats:%s' % row[0]
50 50
                 if not Metric.objects.filter(uuid=uuid).exists():
51 51
                     try:
52  
-                        Metric.objects.create(uuid=uuid, date=row[3],
53  
-                                              name=fix_name(row[1]),
54  
-                                              value=row[2])
  52
+                        obj = Metric.objects.create(uuid=uuid, date=row[3],
  53
+                                                    name=fix_name(row[1]),
  54
+                                                    value=row[2])
55 55
                         count['imported'] += 1
  56
+                        obj.index()
56 57
                     except:
57 58
                         count['errors'] += 1
58 59
                         if options['raise']:
10  metrics/resources.py
... ...
@@ -1,12 +1,13 @@
1 1
 import json
2 2
 
3 3
 from django.core.exceptions import ObjectDoesNotExist
  4
+from django import http
4 5
 
5 6
 from elasticutils import get_es
6 7
 from tastypie import fields
7  
-from tastypie.exceptions import NotFound
8  
-from tastypie.resources import Resource, ModelResource
9 8
 from tastypie.authorization import Authorization
  9
+from tastypie.exceptions import ImmediateHttpResponse, NotFound
  10
+from tastypie.resources import Resource, ModelResource
10 11
 
11 12
 from models import Metric
12 13
 
@@ -32,7 +33,10 @@ class Meta:
32 33
 
33 34
     def obj_get_list(self, request, **kwargs):
34 35
         es = get_es()
35  
-        query = json.loads(request.body)
  36
+        try:
  37
+            query = json.loads(request.body)
  38
+        except ValueError:
  39
+            raise ImmediateHttpResponse(response=http.HttpResponseBadRequest())
36 40
         result = es.search(query, 'monolith')
37 41
         return [Hit(**data.get('_source', data.get('fields', {})))
38 42
                 for data in result['hits']['hits']]
21  metrics/tests.py
@@ -23,16 +23,20 @@ def get(self, uri, format='json', data='',
23 23
         content_type = self.get_content_type(format)
24 24
         kwargs['content_type'] = content_type
25 25
 
26  
-        if data is not None:
27  
-            data = self.serializer.serialize(data, format=content_type)
28  
-
29 26
         req = {
30  
-            'CONTENT_LENGTH': len(data),
  27
+            'CONTENT_LENGTH': 0,
31 28
             'CONTENT_TYPE': 'application/json',
32 29
             'PATH_INFO': unquote(parsed[2]),
33 30
             'REQUEST_METHOD': 'GET',
34  
-            'wsgi.input': FakePayload(data),
35 31
         }
  32
+
  33
+        if data is not None:
  34
+            data = self.serializer.serialize(data, format=content_type)
  35
+            req.update({
  36
+                'CONTENT_LENGTH': len(data),
  37
+                'wsgi.input': FakePayload(data)
  38
+            })
  39
+
36 40
         response = self.client.request(**req)
37 41
         return response
38 42
 
@@ -54,7 +58,6 @@ def test_unindex(self, delete):
54 58
         assert delete.called
55 59
 
56 60
 
57  
-
58 61
 class TestIndexResource(ResourceTestCase):
59 62
     fixtures = ['metrics.json']
60 63
     url = '/es/index/1/'
@@ -85,6 +88,10 @@ def test_get_no_json(self, search):
85 88
         eq_(res.status_code, 200)
86 89
         eq_(len(json.loads(res.content)['objects']), 0)
87 90
 
  91
+    def test_get_bad_json(self, search):
  92
+        res = self.api_client.get(self.url, format='json', data=None)
  93
+        eq_(res.status_code, 400)
  94
+
88 95
     def test_get_filter(self, search):
89 96
         data = {'filter': {'term': {'name': 'addons.downloads.count.total'}}}
90 97
         search.return_value = {u'hits': {u'hits': [
@@ -113,5 +120,3 @@ def test_get_fields(self, search):
113 120
         result = data['objects'][0]
114 121
         eq_(result['date'], None)
115 122
         eq_(result['value'], 2681)
116  
-
117  
-

0 notes on commit 7495a2b

Please sign in to comment.
Something went wrong with that request. Please try again.