Add custom error handler callback
This commit is contained in:
parent
595872f81c
commit
e739557f8e
3 changed files with 221 additions and 34 deletions
185
fjord_test.odin
185
fjord_test.odin
|
|
@ -4,6 +4,7 @@ import "core:fmt"
|
|||
import "core:log"
|
||||
import "core:net"
|
||||
import "core:os/os2"
|
||||
import "core:path/slashpath"
|
||||
import "core:strings"
|
||||
import "core:thread"
|
||||
import "core:time"
|
||||
|
|
@ -12,10 +13,26 @@ import "core:testing"
|
|||
|
||||
import http "http"
|
||||
|
||||
Error :: enum {
|
||||
StandardError :: enum {
|
||||
TestError,
|
||||
}
|
||||
|
||||
Error :: union {
|
||||
StandardError,
|
||||
}
|
||||
|
||||
LOCAL_ADDRESS :: net.IP4_Address{127, 0, 0, 1}
|
||||
|
||||
@(private = "file")
|
||||
port_counter := 50000
|
||||
|
||||
@(private = "file")
|
||||
get_test_port :: proc() -> int {
|
||||
port := port_counter
|
||||
port_counter += 10
|
||||
return port
|
||||
}
|
||||
|
||||
first_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||
entity := request.path_variables["entity"]
|
||||
|
||||
|
|
@ -74,8 +91,8 @@ test_router_ok :: proc(t: ^testing.T) {
|
|||
defer log.destroy_console_logger(context.logger)
|
||||
|
||||
endpoint := net.Endpoint {
|
||||
address = net.IP4_Address{127, 0, 0, 1},
|
||||
port = 8080,
|
||||
address = LOCAL_ADDRESS,
|
||||
port = get_test_port(),
|
||||
}
|
||||
THREAD_COUNT :: 8
|
||||
|
||||
|
|
@ -161,12 +178,21 @@ start_concurrent_server :: proc(
|
|||
shutdown_concurrent_server :: proc(
|
||||
server: ^Server($Error_Type),
|
||||
t: ^thread.Thread,
|
||||
endpoint: net.Endpoint,
|
||||
) {
|
||||
server_shutdown(server)
|
||||
|
||||
// Send a dummy request to unblock accept_tcp, todo: fix this immidiate server shutdown
|
||||
url := fmt.tprintf(
|
||||
"http://%d.%d.%d.%d:%d/dummy",
|
||||
endpoint.address.(net.IP4_Address)[0],
|
||||
endpoint.address.(net.IP4_Address)[1],
|
||||
endpoint.address.(net.IP4_Address)[2],
|
||||
endpoint.address.(net.IP4_Address)[3],
|
||||
endpoint.port,
|
||||
)
|
||||
_, _, _, err := os2.process_exec(
|
||||
{command = []string{"curl", "-s", "http://127.0.0.1:8080/dummy"}},
|
||||
{command = []string{"curl", "-s", url}},
|
||||
context.temp_allocator,
|
||||
)
|
||||
assert(err == nil)
|
||||
|
|
@ -175,14 +201,30 @@ shutdown_concurrent_server :: proc(
|
|||
thread.destroy(t)
|
||||
}
|
||||
|
||||
assert_endpoint_response :: proc(url: string, expected_response: string) {
|
||||
assert_endpoint_response :: proc(
|
||||
endpoint: net.Endpoint,
|
||||
path: []string,
|
||||
expected_response: string,
|
||||
) {
|
||||
// Build URL from endpoint and path
|
||||
path_str := slashpath.join(path, context.temp_allocator)
|
||||
url := fmt.tprintf(
|
||||
"http://%d.%d.%d.%d:%d/%s",
|
||||
endpoint.address.(net.IP4_Address)[0],
|
||||
endpoint.address.(net.IP4_Address)[1],
|
||||
endpoint.address.(net.IP4_Address)[2],
|
||||
endpoint.address.(net.IP4_Address)[3],
|
||||
endpoint.port,
|
||||
path_str,
|
||||
)
|
||||
|
||||
state, stdout, stderr, err := os2.process_exec(
|
||||
{command = []string{"curl", "-s", url}},
|
||||
context.temp_allocator,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.errorf("Failed to execute curl: %v", err)
|
||||
log.errorf("Failed to execute curl: %d %s %s", err, stdout, stderr)
|
||||
assert(false, "curl execution failed")
|
||||
return
|
||||
}
|
||||
|
|
@ -207,9 +249,10 @@ test_server_general_ok :: proc(t: ^testing.T) {
|
|||
context.logger = log.create_console_logger(.Info)
|
||||
defer log.destroy_console_logger(context.logger)
|
||||
|
||||
port := get_test_port()
|
||||
endpoint := net.Endpoint {
|
||||
address = net.IP4_Address{127, 0, 0, 1},
|
||||
port = 8080,
|
||||
address = LOCAL_ADDRESS,
|
||||
port = port,
|
||||
}
|
||||
THREAD_COUNT :: 8
|
||||
|
||||
|
|
@ -237,20 +280,138 @@ test_server_general_ok :: proc(t: ^testing.T) {
|
|||
ITERATIONS :: 100
|
||||
for i in 0 ..< ITERATIONS {
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/world",
|
||||
endpoint,
|
||||
{"hello", "world"},
|
||||
"<div>Hello first world</div>",
|
||||
)
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/lonely%20world/only",
|
||||
endpoint,
|
||||
{"hello", "lonely%20world", "only"},
|
||||
"<div>Hello second lonely%20world</div>",
|
||||
)
|
||||
assert_endpoint_response(
|
||||
"http://127.0.0.1:8080/hello/world/and/worlds%20friend",
|
||||
endpoint,
|
||||
{"hello", "world", "and", "worlds%20friend"},
|
||||
"<div>Hello third world and worlds%20friend</div>",
|
||||
)
|
||||
}
|
||||
|
||||
shutdown_concurrent_server(&server, t)
|
||||
shutdown_concurrent_server(&server, t, endpoint)
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_custom_not_found_handler :: proc(t: ^testing.T) {
|
||||
context.logger = log.create_console_logger(.Info)
|
||||
defer log.destroy_console_logger(context.logger)
|
||||
|
||||
port := get_test_port()
|
||||
endpoint := net.Endpoint {
|
||||
address = LOCAL_ADDRESS,
|
||||
port = port,
|
||||
}
|
||||
THREAD_COUNT :: 8
|
||||
|
||||
server: Server(Error)
|
||||
server_init(&server, endpoint, THREAD_COUNT, context.allocator)
|
||||
defer server_destroy(&server)
|
||||
|
||||
custom_not_found_handler :: proc(
|
||||
request: ^http.Request,
|
||||
) -> (
|
||||
http.Response,
|
||||
Error,
|
||||
) {
|
||||
body := "<div>Custom 404</div>"
|
||||
http_response := http.make_response(
|
||||
.NotFound,
|
||||
transmute([]byte)body,
|
||||
.Html,
|
||||
)
|
||||
return http_response, nil
|
||||
}
|
||||
|
||||
server_set_not_found_handler(&server, custom_not_found_handler)
|
||||
|
||||
server_add_route(
|
||||
&server,
|
||||
.GET,
|
||||
{"exists"},
|
||||
proc(request: ^http.Request) -> (http.Response, Error) {
|
||||
body := "<div>This route exists</div>"
|
||||
response := http.make_response(
|
||||
.Ok,
|
||||
transmute([]byte)body,
|
||||
.Html,
|
||||
context.temp_allocator,
|
||||
)
|
||||
return response, nil
|
||||
},
|
||||
)
|
||||
|
||||
t := start_concurrent_server(&server)
|
||||
|
||||
assert_endpoint_response(
|
||||
endpoint,
|
||||
{"does-not-exist"},
|
||||
"<div>Custom 404</div>",
|
||||
)
|
||||
|
||||
assert_endpoint_response(
|
||||
endpoint,
|
||||
{"exists"},
|
||||
"<div>This route exists</div>",
|
||||
)
|
||||
|
||||
shutdown_concurrent_server(&server, t, endpoint)
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_custom_error_handler :: proc(t: ^testing.T) {
|
||||
context.logger = log.create_console_logger(.Info)
|
||||
defer log.destroy_console_logger(context.logger)
|
||||
|
||||
port := get_test_port()
|
||||
endpoint := net.Endpoint {
|
||||
address = LOCAL_ADDRESS,
|
||||
port = port,
|
||||
}
|
||||
THREAD_COUNT :: 8
|
||||
|
||||
server: Server(Error)
|
||||
server_init(&server, endpoint, THREAD_COUNT, context.allocator)
|
||||
defer server_destroy(&server)
|
||||
|
||||
custom_error_handler :: proc(error: Error) -> http.Response {
|
||||
body := "<div>Custom Error Handler</div>"
|
||||
http_response := http.make_response(
|
||||
.BadRequest,
|
||||
transmute([]byte)body,
|
||||
.Html,
|
||||
)
|
||||
return http_response
|
||||
}
|
||||
|
||||
server_set_error_handler(&server, custom_error_handler)
|
||||
|
||||
error_handler :: proc(request: ^http.Request) -> (http.Response, Error) {
|
||||
return http.Response{}, .TestError
|
||||
}
|
||||
|
||||
server_add_route(&server, .GET, {"error"}, error_handler)
|
||||
|
||||
t := start_concurrent_server(&server)
|
||||
|
||||
assert_endpoint_response(
|
||||
endpoint,
|
||||
{"error"},
|
||||
"<div>Custom Error Handler</div>",
|
||||
)
|
||||
|
||||
shutdown_concurrent_server(&server, t, endpoint)
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue