diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6807fd5c..2db3154e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,5 +14,5 @@ jobs: run: | curl -ssL https://magic.modular.com | bash source $HOME/.bash_profile - magic run mojo run_tests.mojo + magic run test diff --git a/.gitignore b/.gitignore index 06727c22..b89f8c78 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,7 @@ install_id .magic # Rattler -output \ No newline at end of file +output + +# misc +.vscode \ No newline at end of file diff --git a/bench.mojo b/bench.mojo index 744a1d2d..1c455651 100644 --- a/bench.mojo +++ b/bench.mojo @@ -4,12 +4,6 @@ from lightbug_http.header import Headers, Header from lightbug_http.utils import ByteReader, ByteWriter from lightbug_http.http import HTTPRequest, HTTPResponse, encode from lightbug_http.uri import URI -from tests.utils import ( - TestStruct, - FakeResponder, - new_fake_listener, - FakeServer, -) alias headers = 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""" @@ -148,43 +142,3 @@ fn lightbug_benchmark_header_parse(inout b: Bencher): b.iter[header_parse]() - -fn lightbug_benchmark_server(): - var server_report = benchmark.run[run_fake_server](max_iters=1) - print("Server: ") - server_report.print(benchmark.Unit.ms) - - -fn lightbug_benchmark_misc() -> None: - var direct_set_report = benchmark.run[init_test_and_set_a_direct]( - max_iters=1 - ) - - var recreating_set_report = benchmark.run[init_test_and_set_a_copy]( - max_iters=1 - ) - - print("Direct set: ") - direct_set_report.print(benchmark.Unit.ms) - print("Recreating set: ") - recreating_set_report.print(benchmark.Unit.ms) - - -var GetRequest = HTTPRequest(URI.parse("http://127.0.0.1/path")[URI]) - - -fn run_fake_server(): - var handler = FakeResponder() - var listener = new_fake_listener(2, encode(GetRequest)) - var server = FakeServer(listener, handler) - server.serve() - - -fn init_test_and_set_a_copy() -> None: - var test = TestStruct("a", "b") - _ = test.set_a_copy("c") - - -fn init_test_and_set_a_direct() -> None: - var test = TestStruct("a", "b") - _ = test.set_a_direct("c") diff --git a/mojoproject.toml b/mojoproject.toml index 5f34719d..a6d82a59 100644 --- a/mojoproject.toml +++ b/mojoproject.toml @@ -9,7 +9,7 @@ version = "0.1.4" [tasks] build = { cmd = "rattler-build build --recipe recipes -c https://conda.modular.com/max -c conda-forge --skip-existing=all", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} } publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } } -test = { cmd = "magic run mojo run_tests.mojo" } +test = { cmd = "magic run mojo test -I . tests" } bench = { cmd = "magic run mojo bench.mojo" } bench_server = { cmd = "magic run mojo build bench_server.mojo && ./bench_server ; rm bench_server" } format = { cmd = "magic run mojo format -l 120 lightbug_http" } diff --git a/run_tests.mojo b/run_tests.mojo deleted file mode 100644 index ccfd1244..00000000 --- a/run_tests.mojo +++ /dev/null @@ -1,13 +0,0 @@ -from tests.test_io import test_io -from tests.test_http import test_http -from tests.test_header import test_header -from tests.test_uri import test_uri -from tests.test_client import test_client - - -fn main() raises: - test_io() - test_http() - test_header() - test_uri() - test_client() diff --git a/tests/__init__.mojo b/tests/__init__.mojo deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/test_io.mojo b/tests/lightbug_http/io/test_bytes.mojo similarity index 96% rename from tests/test_io.mojo rename to tests/lightbug_http/io/test_bytes.mojo index 9520659a..79748733 100644 --- a/tests/test_io.mojo +++ b/tests/lightbug_http/io/test_bytes.mojo @@ -3,10 +3,6 @@ from collections import Dict, List from lightbug_http.io.bytes import Bytes, bytes_equal, bytes -def test_io(): - test_string_literal_to_bytes() - - fn test_string_literal_to_bytes() raises: var cases = Dict[StringLiteral, Bytes]() cases[""] = Bytes() diff --git a/tests/test_client.mojo b/tests/lightbug_http/sys/test_client.mojo similarity index 64% rename from tests/test_client.mojo rename to tests/lightbug_http/sys/test_client.mojo index 2108f4dc..2631af61 100644 --- a/tests/test_client.mojo +++ b/tests/lightbug_http/sys/test_client.mojo @@ -1,7 +1,4 @@ import testing -from tests.utils import ( - default_server_conn_string, -) from lightbug_http.sys.client import MojoClient from lightbug_http.http import HTTPRequest, encode from lightbug_http.uri import URI @@ -9,27 +6,8 @@ from lightbug_http.header import Header, Headers from lightbug_http.io.bytes import bytes -def test_client(): - var mojo_client = MojoClient() - print("running 200 test") - test_mojo_client_lightbug_external_req_200(mojo_client) - - print("running 301 test") - test_mojo_client_redirect_external_req_301(mojo_client) - - # Seems like trying to run too many of these at once results in - # a 502 from httpbin - - # print("running 302 test") - # test_mojo_client_redirect_external_req_302(mojo_client) - # print("running 307 test") - # test_mojo_client_redirect_external_req_307(mojo_client) - # print("running 308 test") - # test_mojo_client_redirect_external_req_308(mojo_client) - # test_mojo_client_redirect_external_req_google(mojo_client) - - -fn test_mojo_client_redirect_external_req_google(client: MojoClient) raises: +fn test_mojo_client_redirect_external_req_google() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://google.com"), headers=Headers( @@ -42,7 +20,8 @@ fn test_mojo_client_redirect_external_req_google(client: MojoClient) raises: except e: print(e) -fn test_mojo_client_redirect_external_req_302(client: MojoClient) raises: +fn test_mojo_client_redirect_external_req_302() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://httpbin.org/status/302"), headers=Headers( @@ -55,7 +34,8 @@ fn test_mojo_client_redirect_external_req_302(client: MojoClient) raises: except e: print(e) -fn test_mojo_client_redirect_external_req_308(client: MojoClient) raises: +fn test_mojo_client_redirect_external_req_308() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://httpbin.org/status/308"), headers=Headers( @@ -68,7 +48,8 @@ fn test_mojo_client_redirect_external_req_308(client: MojoClient) raises: except e: print(e) -fn test_mojo_client_redirect_external_req_307(client: MojoClient) raises: +fn test_mojo_client_redirect_external_req_307() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://httpbin.org/status/307"), headers=Headers( @@ -81,7 +62,8 @@ fn test_mojo_client_redirect_external_req_307(client: MojoClient) raises: except e: print(e) -fn test_mojo_client_redirect_external_req_301(client: MojoClient) raises: +fn test_mojo_client_redirect_external_req_301() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://httpbin.org/status/301"), headers=Headers( @@ -95,7 +77,8 @@ fn test_mojo_client_redirect_external_req_301(client: MojoClient) raises: except e: print(e) -fn test_mojo_client_lightbug_external_req_200(client: MojoClient) raises: +fn test_mojo_client_lightbug_external_req_200() raises: + var client = MojoClient() var req = HTTPRequest( uri=URI.parse_raises("http://httpbin.org/status/200"), headers=Headers( diff --git a/tests/test_header.mojo b/tests/lightbug_http/test_header.mojo similarity index 91% rename from tests/test_header.mojo rename to tests/lightbug_http/test_header.mojo index eed820f8..438b2c28 100644 --- a/tests/test_header.mojo +++ b/tests/lightbug_http/test_header.mojo @@ -2,15 +2,8 @@ from testing import assert_equal, assert_true from lightbug_http.utils import ByteReader from lightbug_http.header import Headers, Header from lightbug_http.io.bytes import Bytes, bytes -from lightbug_http.strings import empty_string -from lightbug_http.net import default_buffer_size -def test_header(): - test_parse_request_header() - test_parse_response_header() - test_header_case_insensitive() - def test_header_case_insensitive(): var headers = Headers(Header("Host", "SomeHost")) diff --git a/tests/test_http.mojo b/tests/lightbug_http/test_http.mojo similarity index 91% rename from tests/test_http.mojo rename to tests/lightbug_http/test_http.mojo index f1ac53ca..2a9a1cb3 100644 --- a/tests/test_http.mojo +++ b/tests/lightbug_http/test_http.mojo @@ -5,12 +5,9 @@ from lightbug_http.http import HTTPRequest, HTTPResponse, encode from lightbug_http.header import Header, Headers, HeaderKey from lightbug_http.uri import URI from lightbug_http.strings import to_string -from tests.utils import default_server_conn_string +alias default_server_conn_string = "http://localhost:8080" -def test_http(): - test_encode_http_request() - test_encode_http_response() def test_encode_http_request(): diff --git a/tests/test_net.mojo b/tests/lightbug_http/test_net.mojo similarity index 100% rename from tests/test_net.mojo rename to tests/lightbug_http/test_net.mojo diff --git a/tests/test_uri.mojo b/tests/lightbug_http/test_uri.mojo similarity index 90% rename from tests/test_uri.mojo rename to tests/lightbug_http/test_uri.mojo index 1c4a8a14..885234b8 100644 --- a/tests/test_uri.mojo +++ b/tests/lightbug_http/test_uri.mojo @@ -5,17 +5,6 @@ from lightbug_http.strings import empty_string, to_string from lightbug_http.io.bytes import Bytes -def test_uri(): - test_uri_no_parse_defaults() - test_uri_parse_http_with_port() - test_uri_parse_https_with_port() - test_uri_parse_http_with_path() - test_uri_parse_https_with_path() - test_uri_parse_http_basic() - test_uri_parse_http_basic_www() - test_uri_parse_http_with_query_string() - test_uri_parse_http_with_hash() - def test_uri_no_parse_defaults(): var uri = URI.parse("http://example.com")[URI] diff --git a/testutils/__init__.mojo b/testutils/__init__.mojo new file mode 100644 index 00000000..90f60fdd --- /dev/null +++ b/testutils/__init__.mojo @@ -0,0 +1 @@ +from .utils import * \ No newline at end of file diff --git a/tests/utils.mojo b/testutils/utils.mojo similarity index 98% rename from tests/utils.mojo rename to testutils/utils.mojo index 8cc4bd2e..c2131e4b 100644 --- a/tests/utils.mojo +++ b/testutils/utils.mojo @@ -71,7 +71,7 @@ struct FakeClient(Client): self.req_host = "" self.req_is_tls = False - fn do(self, req: HTTPRequest) raises -> HTTPResponse: + fn do(self, owned req: HTTPRequest) raises -> HTTPResponse: return OK(String(defaultExpectedGetResponse)) fn extract(inout self, req: HTTPRequest) raises -> ReqInfo: