"""Tests for http parsing."""
import unittest2

from hanzo.httptools.messaging import RequestMessage, ResponseMessage

get_request = "\r\n".join( [
        "GET / HTTP/1.1",
        "Host: example.org",
        "",
        "",
        ])
get_response = "\r\n".join( [
        "HTTP/1.1 200 OK",
        "Host: example.org",
        "Content-Length: 5",
        "",
        "tests",
        ])

class GetChar(unittest2.TestCase):
    """Test basic GET request parsing. Single character at a time."""
    def runTest(self):
        """Attempts to parse the contents of get_request and
        get_response."""
        p = RequestMessage()
        for t in get_request:
            text = p.feed(t)
            self.assertEqual(text, '')

        self.assertTrue(p.headers_complete())
        self.assertTrue(p.complete())

        self.assertEqual(get_request, p.get_decoded_message())

        p = ResponseMessage(p)
        for char in get_response:
            text = p.feed(char)
            self.assertEqual(text, '')

        self.assertTrue(p.headers_complete())
        self.assertTrue(p.complete())
        self.assertEqual(get_response, p.get_decoded_message())


head_request = "\r\n".join( [
    "HEAD / HTTP/1.1",
    "Host: example.org",
    "",
    "",
])
head_response = "\r\n".join( [
    "HTTP/1.1 200 OK",
    "Host: example.org",
    "Content-Length: 5",
    "",
    "",
])


class HeadTest(unittest2.TestCase):

    def runTest(self):
        p = RequestMessage()
        text=p.feed(head_request)

        self.assertEqual(text, '')
        self.assertTrue(p.complete())
        self.assertEqual(head_request, p.get_decoded_message())
        
        p = ResponseMessage(p)
        text = p.feed(head_response)

        self.assertEqual(text, '')
        self.assertTrue(p.complete())
        self.assertEqual(head_response, p.get_decoded_message())

post_request = "\r\n".join( [
    "POST / HTTP/1.1",
    "Host: example.org",
    "Transfer-Encoding: chunked",
    "",
    "8",
    "abcdefgh",
    "0",
    "",
    "",
])
post_response = "\r\n".join( [
    "HTTP/1.1 100 Continue",
    "Host: example.org",
    "",
    "HTTP/1.0 204 No Content",
    "Date: now!",
    "",
    "",
])


class PostTest(unittest2.TestCase):

    def runTest(self):
        p = RequestMessage()
        text = p.feed(post_request)

        self.assertEqual(text, '')
        self.assertTrue(p.complete())

        p = ResponseMessage(p)
        text = p.feed(post_response)
        
        self.assertEqual(text, '')
        self.assertTrue(p.complete())


if __name__ == '__main__':
    unittest2.main()



@@ -119,39 +119,32 @@
 %0Aget_request
- = %22%5Cr%5Cn%22.join(
+_lines =
  %5B%0A        %22
@@ -226,22 +226,20 @@
    %5D
-)
 %0Aget_re
-sponse
+quest
  = %22
@@ -245,24 +245,63 @@
 %22%5Cr%5Cn%22.join(
+get_request_lines)%0Aget_response_lines =
  %5B%0A        %22
@@ -414,16 +414,62 @@
        %5D
+%0Aget_response = %22%5Cr%5Cn%22.join(get_response_lines
 )%0A%0Aclass
@@ -1240,16 +1240,968 @@
 age())%0A%0A
+class GetLines(unittest2.TestCase):%0A    %22%22%22Test basic GET request parsing. Single line at a time.%22%22%22%0A    def runTest(self):%0A        %22%22%22Attempts to parse get_request_lines, i.e. get_request line%0A        at a time.%22%22%22%0A%0A        p = RequestMessage()%0A        for line in get_request_lines:%0A            text = p.feed(line)%0A            self.assertEqual(text, %22%22)%0A            text = p.feed(%22%5Cr%5Cn%22)%0A            self.assertEqual(text, %22%22)%0A%0A        self.assertTrue(p.headers_complete())%0A        self.assertTrue(p.complete())%0A%0A        self.assertEqual(get_request, p.get_decoded_message())%0A%0A        p = ResponseMessage(p)%0A        for line in get_response_lines:%0A            text = p.feed(line)%0A            self.assertEqual(text, %22%22)%0A            text = p.feed(%22%5Cr%5Cn%22)%0A            self.assertEqual(text, %22%22)%0A%0A        self.assertTrue(p.headers_complete())%0A        self.assertTrue(p.complete())%0A        %0A        self.assertEqual(get_response, p.get_decoded_message())%0A
 %0Ahead_re

Add line at a time unit test.