-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from thatstoasty/ptrs
Improve code safety and error handling + performance improvements
- Loading branch information
Showing
22 changed files
with
1,970 additions
and
1,124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,83 @@ | ||
from collections import Dict | ||
from lightbug_http import * | ||
from lightbug_http.client import Client | ||
from lightbug_http.utils import logger | ||
from testing import * | ||
|
||
fn u(s: String) raises -> URI: | ||
return URI.parse_raises("http://127.0.0.1:8080/" + s) | ||
|
||
struct IntegrationTest: | ||
var client: Client | ||
var results: Dict[String, String] | ||
|
||
fn __init__(out self): | ||
self.client = Client() | ||
self.results = Dict[String, String]() | ||
|
||
fn mark_successful(mut self, name: String): | ||
self.results[name] = "✅" | ||
|
||
fn mark_failed(mut self, name: String): | ||
self.results[name] = "❌" | ||
|
||
fn test_redirect(mut self) raises: | ||
print("Testing redirect...") | ||
fn test_redirect(mut self): | ||
alias name = "test_redirect" | ||
logger.info("Testing redirect...") | ||
var h = Headers(Header(HeaderKey.CONNECTION, 'keep-alive')) | ||
var res = self.client.do(HTTPRequest(u("redirect"), headers=h)) | ||
assert_equal(res.status_code, StatusCode.OK) | ||
assert_equal(to_string(res.body_raw), "yay you made it") | ||
assert_equal(res.headers[HeaderKey.CONNECTION], "keep-alive") | ||
try: | ||
var res = self.client.do(HTTPRequest(u("redirect"), headers=h)) | ||
assert_equal(res.status_code, StatusCode.OK) | ||
assert_equal(to_string(res.body_raw), "yay you made it") | ||
var conn = res.headers.get(HeaderKey.CONNECTION) | ||
if conn: | ||
assert_equal(conn.value(), "keep-alive") | ||
self.mark_successful(name) | ||
except e: | ||
logger.error("IntegrationTest.test_redirect has run into an error.") | ||
logger.error(e) | ||
self.mark_failed(name) | ||
return | ||
|
||
fn test_close_connection(mut self) raises: | ||
print("Testing close connection...") | ||
fn test_close_connection(mut self): | ||
alias name = "test_close_connection" | ||
logger.info("Testing close connection...") | ||
var h = Headers(Header(HeaderKey.CONNECTION, 'close')) | ||
var res = self.client.do(HTTPRequest(u("close-connection"), headers=h)) | ||
assert_equal(res.status_code, StatusCode.OK) | ||
assert_equal(to_string(res.body_raw), "connection closed") | ||
assert_equal(res.headers[HeaderKey.CONNECTION], "close") | ||
try: | ||
var res = self.client.do(HTTPRequest(u("close-connection"), headers=h)) | ||
assert_equal(res.status_code, StatusCode.OK) | ||
assert_equal(to_string(res.body_raw), "connection closed") | ||
assert_equal(res.headers[HeaderKey.CONNECTION], "close") | ||
self.mark_successful(name) | ||
except e: | ||
logger.error("IntegrationTest.test_close_connection has run into an error.") | ||
logger.error(e) | ||
self.mark_failed(name) | ||
return | ||
|
||
fn test_server_error(mut self) raises: | ||
print("Testing internal server error...") | ||
var res = self.client.do(HTTPRequest(u("error"))) | ||
assert_equal(res.status_code, StatusCode.INTERNAL_ERROR) | ||
assert_equal(res.status_text, "Internal Server Error") | ||
fn test_server_error(mut self): | ||
alias name = "test_server_error" | ||
logger.info("Testing internal server error...") | ||
try: | ||
var res = self.client.do(HTTPRequest(u("error"))) | ||
assert_equal(res.status_code, StatusCode.INTERNAL_ERROR) | ||
assert_equal(res.status_text, "Internal Server Error") | ||
self.mark_successful(name) | ||
except e: | ||
logger.error("IntegrationTest.test_server_error has run into an error.") | ||
logger.error(e) | ||
self.mark_failed(name) | ||
return | ||
|
||
|
||
fn run_tests(mut self) raises: | ||
fn run_tests(mut self): | ||
logger.info("Running Client Integration Tests...") | ||
self.test_redirect() | ||
self.test_close_connection() | ||
self.test_server_error() | ||
|
||
fn main() raises: | ||
for test in self.results.items(): | ||
print(test[].key + ":", test[].value) | ||
|
||
fn main(): | ||
var test = IntegrationTest() | ||
test.run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.