Commits

Michael Richardson committed 1edef45

Add a basic EchoHandler and tests to show that the validation decorator is now working.

Comments (0)

Files changed (4)

tests/test_project/apps/testapp/forms.py

+from django import forms
+
+
+class EchoForm(forms.Form):
+    msg = forms.CharField(max_length=128)
+

tests/test_project/apps/testapp/handlers.py

 from piston.utils import rc, validate
 
 from models import TestModel, ExpressiveTestModel, Comment, InheritedModel
+from forms import EchoForm
+
 
 class EntryHandler(BaseHandler):
     model = TestModel
             return self.model.objects.get(pk=id_)
         else:
             return super(AbstractHandler, self).read(request)
-            
+
+
+class EchoHandler(BaseHandler):
+    allowed_methods = ('GET', )
+
+    @validate(EchoForm, 'GET')
+    def read(self, request):
+        return {'msg': request.GET['msg']}

tests/test_project/apps/testapp/tests.py

   title: test
 """
         self.assertEquals(self.client.get('/api/expressive.yaml', 
-            HTTP_AUTHORIZATION=self.auth_string).content, expected)
+            HTTP_AUTHORIZATION=self.auth_string).content, expected)
+
+
+class ValidationTest(MainTests):
+    def test_basic_validation_fails(self):
+        resp = self.client.get('/api/echo')
+        self.assertEquals(resp.status_code, 400)
+        self.assertEquals(resp.content, 'Bad Request: <ul class="errorlist">'
+            '<li>msg<ul class="errorlist"><li>This field is required.</li>'
+            '</ul></li></ul>')
+
+    def test_basic_validation_succeeds(self):
+        data = {'msg': 'donuts!'}
+        resp = self.client.get('/api/echo', data)
+        self.assertEquals(resp.status_code, 200)
+        self.assertEquals(data, simplejson.loads(resp.content))
+

tests/test_project/apps/testapp/urls.py

 from piston.resource import Resource
 from piston.authentication import HttpBasicAuthentication
 
-from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler
+from test_project.apps.testapp.handlers import EntryHandler, ExpressiveHandler, AbstractHandler, EchoHandler
 
 auth = HttpBasicAuthentication(realm='TestApplication')
 
 entries = Resource(handler=EntryHandler, authentication=auth)
 expressive = Resource(handler=ExpressiveHandler, authentication=auth)
 abstract = Resource(handler=AbstractHandler, authentication=auth)
+echo = Resource(handler=EchoHandler)
+
 
 urlpatterns = patterns('',
     url(r'^entries/$', entries),
     url(r'^entries/(?P<pk>.+)/$', entries),
     url(r'^entries\.(?P<emitter_format>.+)', entries),
     url(r'^entry-(?P<pk>.+)\.(?P<emitter_format>.+)', entries),
-    
+
     url(r'^expressive\.(?P<emitter_format>.+)$', expressive),
 
     url(r'^abstract\.(?P<emitter_format>.+)$', abstract),
     url(r'^abstract/(?P<id_>\d+)\.(?P<emitter_format>.+)$', abstract),
+
+    url(r'^echo$', echo),
 )
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.