Commits

Hong Minhee committed 2d82b9b

Added `data` property into `vlastic.http.Request`.

Comments (0)

Files changed (3)

test/http_test.py

     """Unit tests for Request class."""
 
     def setUp(self):
-        self.method = b"GET"
-        self.path = b"/?a=1&b=2"
+        self.setup_message(b"GET", b"/?a=1&b=2")
+
+    def setup_message(self, method, path, body=b""):
+        self.method = method
+        self.path = path
         self.protocol = b"HTTP/1.1"
         self.start_line = b" ".join((self.method, self.path, self.protocol))
         self.headers = [
             (b"Accept-Encoding", b"gzip, deflate"),
             (b"Host", b"kldp.org")
         ]
+        self.body = body
         headerstr = b"\r\n".join(n + b": " + v for n, v in self.headers)
-        self.full_message = self.start_line + b"\r\n" + headerstr + b"\r\n\r\n"
+        self.full_message = self.start_line + b"\r\n" + headerstr \
+                          + b"\r\n\r\n" + body
 
     def test_init_full_message(self):
         req = Request(self.full_message)
         self.assertEquals("a=1&b=2", str(req.query))
         self.assertEquals(self.headers, req.headers)
 
+    def test_post(self):
+        self.setup_message(b"POST", b"/", b"a=1&b=2")
+        req = Request(self.start_line, self.headers, self.body)
+        self.assertEquals("POST", req.method)
+        self.assertEquals("POST", req.verb)
+        self.assertEquals("/", str(req.path))
+        self.assertTrue(isinstance(req.query, QueryString))
+        self.assertEquals("", str(req.query))
+        self.assertTrue(isinstance(req.data, QueryString))
+        self.assertEquals("a=1&b=2", str(req.data))
+        self.assertEquals(self.headers, req.headers)
+
     def test_accept(self):
         req = Request(self.start_line, self.headers)
         self.assertTrue(isinstance(req.accept, NegotiativeHeader))
                 " is unsupported."
             )
 
-    def test_match_querystring(self):
+    def test_match_get_querystring(self):
         resource = TestResource2()
         response = resource(Request("GET /?a=1&b=2&c=3&d=4 HTTP/1.1\r\n\r\n"))
         self.assertEquals(
     verb = property(lambda self: self.method)
 
     @property
+    def data(self):
+        if not hasattr(self, "_data"):
+            self._data = url.QueryString(b"".join(self.body))
+        return self._data
+
+    @property
     def accept(self):
         """Returns a NegotiativeHeader instance of 'Accept' header.