@@ -7,98 +7,12 @@ from lightbug_http.strings import empty_string
77from lightbug_http.net import default_buffer_size
88
99def test_header ():
10- # test_parse_request_first_line_happy_path()
11- # test_parse_request_first_line_error()
12- # test_parse_response_first_line_happy_path()
13- # test_parse_response_first_line_no_message()
1410 test_parse_request_header()
15- test_parse_request_header_empty()
1611 test_parse_response_header()
17- test_parse_response_header_empty()
18-
19- # def test_parse_request_first_line_happy_path():
20- # var test = MojoTest("test_parse_request_first_line_happy_path")
21- # var cases = Dict[String, List[StringLiteral]]()
22-
23- # # Well-formed request lines
24- # cases["GET /index.html HTTP/1.1\n"] = List("GET", "/index.html", "HTTP/1.1")
25- # cases["POST /index.html HTTP/1.1"] = List("POST", "/index.html", "HTTP/1.1")
26- # cases["GET / HTTP/1.1"] = List("GET", "/", "HTTP/1.1")
27-
28- # # Not quite well-formed, but we can fall back to default values
29- # cases["GET "] = List("GET", "/", "HTTP/1.1")
30- # cases["GET /"] = List("GET", "/", "HTTP/1.1")
31- # cases["GET /index.html"] = List("GET", "/index.html", "HTTP/1.1")
32-
33- # for c in cases.items():
34- # var header = RequestHeader()
35- # var b = Bytes(c[].key.as_bytes_slice())
36- # var buf = buffer.new_buffer(b^)
37- # var reader = Reader(buf^)
38- # _ = header.parse_raw(reader)
39- # test.assert_equal(String(header.method()), c[].value[0])
40- # test.assert_equal(String(header.request_uri()), c[].value[1])
41- # test.assert_equal(header.protocol_str(), c[].value[2])
42-
43- # def test_parse_request_first_line_error():
44- # var test = MojoTest("test_parse_request_first_line_error")
45- # var cases = Dict[String, String]()
46-
47- # cases["G"] = "Cannot find HTTP request method in the request"
48- # cases[""] = "Cannot find HTTP request method in the request"
49- # cases["GET"] = "Cannot find HTTP request method in the request" # This is misleading, update
50- # cases["GET /index.html HTTP"] = "Invalid protocol"
51-
52- # for c in cases.items():
53- # var header = RequestHeader(c[].key)
54- # var b = Bytes(capacity=default_buffer_size)
55- # var buf = buffer.new_buffer(b^)
56- # var reader = Reader(buf^)
57- # try:
58- # _ = header.parse_raw(reader)
59- # except e:
60- # test.assert_equal(String(e.__str__()), c[].value)
61-
62- # def test_parse_response_first_line_happy_path():
63- # var test = MojoTest("test_parse_response_first_line_happy_path")
64- # var cases = Dict[String, List[StringLiteral]]()
65-
66- # # Well-formed status (response) lines
67- # cases["HTTP/1.1 200 OK"] = List("HTTP/1.1", "200", "OK")
68- # cases["HTTP/1.1 404 Not Found"] = List("HTTP/1.1", "404", "Not Found")
69- # cases["HTTP/1.1 500 Internal Server Error"] = List("HTTP/1.1", "500", "Internal Server Error")
70-
71- # # Trailing whitespace in status message is allowed
72- # cases["HTTP/1.1 200 OK "] = List("HTTP/1.1", "200", "OK ")
73-
74- # for c in cases.items():
75- # var header = ResponseHeader(empty_string.as_bytes_slice())
76- # header.parse_raw(c[].key)
77- # test.assert_equal(String(header.protocol()), c[].value[0])
78- # test.assert_equal(header.status_code().__str__(), c[].value[1])
79- # # also behaving weirdly with "OK" with byte slice, had to switch to string for now
80- # test.assert_equal(header.status_message_str(), c[].value[2])
81-
82- # # Status lines without a message are perfectly valid
83- # def test_parse_response_first_line_no_message():
84- # var test = MojoTest("test_parse_response_first_line_no_message")
85- # var cases = Dict[String, List[StringLiteral]]()
86-
87- # # Well-formed status (response) lines
88- # cases["HTTP/1.1 200"] = List("HTTP/1.1", "200")
89-
90- # # Not quite well-formed, but we can fall back to default values
91- # cases["HTTP/1.1 200 "] = List("HTTP/1.1", "200")
92-
93- # for c in cases.items():
94- # var header = ResponseHeader(bytes(""))
95- # header.parse_raw(c[].key)
96- # test.assert_equal(String(header.status_message()), Bytes(String("").as_bytes())) # Empty string
9712
9813def test_parse_request_header ():
9914 var test = MojoTest(" test_parse_request_header" )
100- var headers_str = bytes (''' GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n ''' )
101-
15+ var headers_str = bytes (''' GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n\r\n ''' )
10216 var header = RequestHeader()
10317 var b = Bytes(headers_str)
10418 var buf = buffer.new_buffer(b^ )
@@ -107,47 +21,20 @@ def test_parse_request_header():
10721 test.assert_equal(String(header.request_uri()), " /index.html" )
10822 test.assert_equal(String(header.protocol()), " HTTP/1.1" )
10923 test.assert_equal(header.no_http_1_1, False )
110- test.assert_equal(String(header.host()), " example.com" )
24+ test.assert_equal(String(header.host()), String( " example.com" ) )
11125 test.assert_equal(String(header.user_agent()), " Mozilla/5.0" )
11226 test.assert_equal(String(header.content_type()), " text/html" )
11327 test.assert_equal(header.content_length(), 1234 )
11428 test.assert_equal(header.connection_close(), True )
115- # test.assert_equal(String(header.trailer()), "end-of-message")
11629
117- def test_parse_request_header_empty ():
118- var test = MojoTest(" test_parse_request_header_empty " )
119- var headers_str = Bytes( )
120- var header = RequestHeader(headers_str )
121- var b = Bytes(capacity = default_buffer_size )
30+ def test_parse_response_header ():
31+ var test = MojoTest(" test_parse_response_header " )
32+ var headers_str = bytes ( ''' HTTP/1.1 200 OK \r\n Server: example.com \r\n User-Agent: Mozilla/5.0 \r\n Content-Type: text/html \r\n Content-Encoding: gzip \r\n Content-Length: 1234 \r\n Connection: close \r\n Trailer: end-of-message \r\n\r\n ''' )
33+ var header = ResponseHeader( )
34+ var b = Bytes(headers_str )
12235 var buf = buffer.new_buffer(b^ )
12336 var reader = Reader(buf^ )
12437 _ = header.parse_raw(reader)
125- _ = header.parse_raw(reader)
126- test.assert_equal(String(header.request_uri()), " /index.html" )
127- test.assert_equal(String(header.protocol()), " HTTP/1.1" )
128- test.assert_equal(header.no_http_1_1, False )
129- test.assert_equal(String(header.host()), String(empty_string.as_bytes_slice()))
130- test.assert_equal(String(header.user_agent()), String(empty_string.as_bytes_slice()))
131- test.assert_equal(String(header.content_type()), String(empty_string.as_bytes_slice()))
132- test.assert_equal(header.content_length(), - 2 )
133- test.assert_equal(header.connection_close(), False )
134- test.assert_equal(String(header.trailer()), String(empty_string.as_bytes_slice()))
135-
136-
137- def test_parse_response_header ():
138- var test = MojoTest(" test_parse_response_header" )
139- var headers_str = bytes ('''
140- Server: example.com\r\n
141- User-Agent: Mozilla/5.0\r\n
142- Content-Type: text/html\r\n
143- Content-Encoding: gzip\r\n
144- Content-Length: 1234\r\n
145- Connection: close\r\n
146- Trailer: end-of-message\r\n
147- ''' )
148-
149- var header = ResponseHeader(headers_str)
150- header.parse_raw(" HTTP/1.1 200 OK" )
15138 test.assert_equal(String(header.protocol()), " HTTP/1.1" )
15239 test.assert_equal(header.no_http_1_1, False )
15340 test.assert_equal(header.status_code(), 200 )
@@ -158,20 +45,3 @@ def test_parse_response_header():
15845 test.assert_equal(header.content_length(), 1234 )
15946 test.assert_equal(header.connection_close(), True )
16047 test.assert_equal(header.trailer_str(), " end-of-message" )
161-
162- def test_parse_response_header_empty ():
163- var test = MojoTest(" test_parse_response_header_empty" )
164- var headers_str = Bytes()
165-
166- var header = ResponseHeader(headers_str)
167- header.parse_raw(" HTTP/1.1 200 OK" )
168- test.assert_equal(String(header.protocol()), " HTTP/1.1" )
169- test.assert_equal(header.no_http_1_1, False )
170- test.assert_equal(header.status_code(), 200 )
171- test.assert_equal(String(header.status_message()), " OK" )
172- test.assert_equal(String(header.server()), String(empty_string.as_bytes_slice()))
173- test.assert_equal(String(header.content_type()), String(empty_string.as_bytes_slice()))
174- test.assert_equal(String(header.content_encoding()), String(empty_string.as_bytes_slice()))
175- test.assert_equal(header.content_length(), - 2 )
176- test.assert_equal(header.connection_close(), False )
177- test.assert_equal(String(header.trailer()), String(empty_string.as_bytes_slice()))
0 commit comments