Skip to content

Commit 13ec707

Browse files
committed
fix tests
1 parent 045e8ac commit 13ec707

File tree

6 files changed

+26
-180
lines changed

6 files changed

+26
-180
lines changed

lightbug.🔥

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from lightbug_http import *
2-
from lightbug_http.service import TechEmpowerRouter
32

43
fn main() raises:
54
var server = SysServer()
6-
var handler = TechEmpowerRouter()
5+
var handler = Welcome()
76
server.listen_and_serve("0.0.0.0:8080", handler)

lightbug_http/header.mojo

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -313,22 +313,22 @@ struct RequestHeader:
313313

314314
if key_first == bytes("h", pop=False)[0] or key_first == bytes("H", pop=False)[0]:
315315
if compare_case_insensitive(key, bytes("host", pop=False)):
316-
_ = self.set_host_bytes(bytes(value, pop=False))
316+
_ = self.set_host_bytes(bytes(value))
317317
return
318318
elif key_first == bytes("u", pop=False)[0] or key_first == bytes("U", pop=False)[0]:
319319
if compare_case_insensitive(key, bytes("user-agent", pop=False)):
320-
_ = self.set_user_agent_bytes(bytes(value, pop=False))
320+
_ = self.set_user_agent_bytes(bytes(value))
321321
return
322322
elif key_first == bytes("c", pop=False)[0] or key_first == bytes("C", pop=False)[0]:
323323
if compare_case_insensitive(key, bytes("content-type", pop=False)):
324-
_ = self.set_content_type_bytes(bytes(value, pop=False))
324+
_ = self.set_content_type_bytes(bytes(value))
325325
return
326326
if compare_case_insensitive(key, bytes("content-length", pop=False)):
327327
if self.content_length() != -1:
328-
_ = self.set_content_length_bytes(bytes(value))
328+
_ = self.set_content_length(atol(value))
329329
return
330330
if compare_case_insensitive(key, bytes("connection", pop=False)):
331-
if compare_case_insensitive(value, bytes("close", pop=False)):
331+
if compare_case_insensitive(bytes(value), bytes("close", pop=False)):
332332
_ = self.set_connection_close()
333333
else:
334334
_ = self.reset_connection_close()
@@ -346,7 +346,6 @@ struct RequestHeader:
346346

347347
fn read_raw_headers(inout self, buf: Bytes) raises -> Int:
348348
var n = index_byte(buf, bytes(nChar, pop=False)[0])
349-
350349
if n == -1:
351350
self.raw_headers = self.raw_headers[:0]
352351
raise Error("Failed to find a newline in headers")
@@ -627,21 +626,6 @@ struct ResponseHeader:
627626
fn headers(self) -> String:
628627
return String(self.raw_headers)
629628

630-
# fn parse_from_list(inout self, headers: List[String], first_line: String) raises -> None:
631-
# _ = self.parse_first_line(first_line)
632-
633-
# for header in headers:
634-
# var header_str = header[]
635-
# var separator = header_str.find(":")
636-
# if separator == -1:
637-
# raise Error("Invalid header")
638-
639-
# var key = String(header_str)[:separator]
640-
# var value = String(header_str)[separator + 1 :]
641-
642-
# if len(key) > 0:
643-
# self.parse_header(key, value)
644-
645629
fn parse_raw(inout self, inout r: Reader) raises -> Int:
646630
var first_byte = r.peek(1)
647631
if len(first_byte) == 0:
@@ -677,14 +661,14 @@ struct ResponseHeader:
677661
if first_whitespace <= 0:
678662
raise Error("Could not find HTTP version in response line: " + String(b))
679663

680-
_ = self.set_protocol(b[:first_whitespace])
664+
_ = self.set_protocol(b[:first_whitespace+2])
681665

682666
var end_of_status_code = first_whitespace+5 # status code is always 3 digits, this calculation includes null terminator
683667

684668
var status_code = atol(b[first_whitespace+1:end_of_status_code])
685669
_ = self.set_status_code(status_code)
686670

687-
var status_text = b[end_of_status_code + 1 :]
671+
var status_text = b[end_of_status_code :]
688672
if len(status_text) > 1:
689673
_ = self.set_status_message(status_text)
690674

@@ -707,10 +691,10 @@ struct ResponseHeader:
707691

708692
if key_first == bytes("c", pop=False)[0] or key_first == bytes("C", pop=False)[0]:
709693
if compare_case_insensitive(key, bytes("content-type", pop=False)):
710-
_ = self.set_content_type_bytes(bytes(value, pop=False))
694+
_ = self.set_content_type_bytes(bytes(value))
711695
return
712696
if compare_case_insensitive(key, bytes("content-encoding", pop=False)):
713-
_ = self.set_content_encoding_bytes(bytes(value, pop=False))
697+
_ = self.set_content_encoding_bytes(bytes(value))
714698
return
715699
if compare_case_insensitive(key, bytes("content-length", pop=False)):
716700
if self.content_length() != -1:
@@ -719,22 +703,22 @@ struct ResponseHeader:
719703
_ = self.set_content_length_bytes(bytes(content_length))
720704
return
721705
if compare_case_insensitive(key, bytes("connection", pop=False)):
722-
if compare_case_insensitive(value, bytes("close", pop=False)):
706+
if compare_case_insensitive(bytes(value), bytes("close", pop=False)):
723707
_ = self.set_connection_close()
724708
else:
725709
_ = self.reset_connection_close()
726710
return
727711
elif key_first == bytes("s", pop=False)[0] or key_first == bytes("S", pop=False)[0]:
728712
if compare_case_insensitive(key, bytes("server", pop=False)):
729-
_ = self.set_server_bytes(bytes(value, pop=False))
713+
_ = self.set_server_bytes(bytes(value))
730714
return
731715
elif key_first == bytes("t", pop=False)[0] or key_first == bytes("T", pop=False)[0]:
732716
if compare_case_insensitive(key, bytes("transfer-encoding", pop=False)):
733717
if not compare_case_insensitive(value, bytes("identity", pop=False)):
734718
_ = self.set_content_length(-1)
735719
return
736720
if compare_case_insensitive(key, bytes("trailer", pop=False)):
737-
_ = self.set_trailer_bytes(bytes(value, pop=False))
721+
_ = self.set_trailer_bytes(bytes(value))
738722

739723
fn read_raw_headers(inout self, buf: Bytes) raises -> Int:
740724
var n = index_byte(buf, bytes(nChar, pop=False)[0])

lightbug_http/sys/server.mojo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,6 @@ struct SysServer:
235235

236236
_ = conn.write(res_encoded)
237237

238-
if not self.tcp_keep_alive:
239-
conn.close()
240-
return
238+
if not self.tcp_keep_alive:
239+
conn.close()
240+
return

tests/test_client.mojo

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ from lightbug_http.io.bytes import bytes
1414

1515
def test_client():
1616
var mojo_client = MojoClient()
17-
# test_mojo_client_lightbug(mojo_client)
17+
var py_client = PythonClient()
1818
test_mojo_client_lightbug_external_req(mojo_client)
19-
20-
# var py_client = PythonClient()
21-
# test_python_client_lightbug(py_client) - this is broken for now due to issue with passing a tuple to self.socket.connect()
19+
test_python_client_lightbug(py_client)
2220

2321

2422
fn test_mojo_client_lightbug(client: MojoClient) raises:

tests/test_header.mojo

Lines changed: 7 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -7,98 +7,12 @@ from lightbug_http.strings import empty_string
77
from lightbug_http.net import default_buffer_size
88

99
def 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

9813
def test_parse_request_header():
9914
var test = MojoTest("test_parse_request_header")
100-
var headers_str = bytes('''GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: end-of-message\r\n''')
101-
15+
var headers_str = bytes('''GET /index.html HTTP/1.1\r\nHost: example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: 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\nServer: example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Encoding: gzip\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: 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()))

tests/test_http.mojo

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ def test_split_http_string():
2121
List("GET /index.html HTTP/1.1",
2222
"Host: www.example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: end-of-message",
2323
"Hello, World!")
24-
# cases["GET /index.html HTTP/1.1\r\n\r\nHello, World!\0"] = List("GET /index.html HTTP/1.1", "", "Hello, World!")
25-
# cases["GET /index.html HTTP/1.1\r\nHost: www.example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: end-of-message\r\n\r\n"] =
26-
# List("GET /index.html HTTP/1.1",
27-
# "Host: www.example.com\r\nUser-Agent: Mozilla/5.0\r\nContent-Type: text/html\r\nContent-Length: 1234\r\nConnection: close\r\nTrailer: end-of-message", "")
28-
# cases["GET /index.html HTTP/1.1\r\n\r\n"] = List("GET /index.html HTTP/1.1", "", "")
29-
24+
3025
for c in cases.items():
3126
var buf = bytes((c[].key))
3227
request_first_line, request_headers, request_body = split_http_string(buf)

0 commit comments

Comments
 (0)