Commits

Atsushi Odagiri committed 90d1cfc

using Slug header to resource uri like atompub.

Comments (0)

Files changed (2)

src/restfuljson/restfuljson/__init__.py

 from webob import Request, Response, exc
-
+import simplejson as json
 
 class RestfulJsonApplication(object):
     """
     def __call__(self, environ, start_response):
         """
         """
-        res = Response(content_type='application/json')
-        
+        req = self.request = Request(environ)
+        res = self.response = Response(content_type='application/json')
+        method = req.method.lower()
+        if not hasattr(self, method):
+            raise exc.HTTPMethodNotAllowed
+        data = getattr(self, method)()
+        res.body = json.dumps(data)
         return res(environ, start_response)
+
+    def get(self):
+        return []
+
+    def post(self):
+        self.response.status = 201
+
+        self.response.location = self.request.relative_url(self.request.headers['Slug'])
+        return {}

src/restfuljson/tests/__init__.py

 from restfuljson import RestfulJsonApplication
 from webtest import TestApp
+import simplejson as json
 
 def create_app():
     app = RestfulJsonApplication()
     app = TestApp(app)
     return app
 
-def test_failure():
+def test_collection_get():
     app = create_app()
+    
     res = app.get('/')
+    
     assert res.content_type == 'application/json'
+    assert res.status_int == 200
+    data = res.json
+    assert len(data) == 0
 
+def test_collection_post():
+    
+    app = create_app()
+    person = dict(
+        name="aodag",
+        email="aodagx@gmail.com"
+        )
+    res = app.post('/', params=json.dumps(person), 
+                   headers={
+            "CONTENT-TYPE":'application/json',
+            "Slug":"person1",
+            })
+    
+    assert res.content_type == 'application/json'
+    assert res.status_int == 201
+    data = res.json
+    print res.location
+    assert res.location == 'http://localhost/person1'
+    assert data is not None