2023-07-16 15:42:30 +03:00
|
|
|
import net
|
2021-08-24 19:21:24 +03:00
|
|
|
import net.http
|
2021-08-19 11:19:49 +03:00
|
|
|
import time
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_server_stop() {
|
2021-08-24 19:21:24 +03:00
|
|
|
mut server := &http.Server{
|
2021-08-19 11:19:49 +03:00
|
|
|
accept_timeout: 1 * time.second
|
|
|
|
}
|
2022-11-05 10:46:40 +03:00
|
|
|
t := spawn server.listen_and_serve()
|
2021-08-19 11:19:49 +03:00
|
|
|
time.sleep(250 * time.millisecond)
|
|
|
|
mut watch := time.new_stopwatch()
|
|
|
|
server.stop()
|
2021-08-24 19:21:24 +03:00
|
|
|
assert server.status() == .stopped
|
2021-08-19 11:19:49 +03:00
|
|
|
assert watch.elapsed() < 100 * time.millisecond
|
2022-10-16 09:28:57 +03:00
|
|
|
t.wait()
|
2021-08-19 11:19:49 +03:00
|
|
|
assert watch.elapsed() < 999 * time.millisecond
|
|
|
|
}
|
2021-08-24 19:21:24 +03:00
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_server_close() {
|
2021-08-24 19:21:24 +03:00
|
|
|
mut server := &http.Server{
|
|
|
|
accept_timeout: 1 * time.second
|
|
|
|
handler: MyHttpHandler{}
|
|
|
|
}
|
2022-11-05 10:46:40 +03:00
|
|
|
t := spawn server.listen_and_serve()
|
2021-08-24 19:21:24 +03:00
|
|
|
time.sleep(250 * time.millisecond)
|
|
|
|
mut watch := time.new_stopwatch()
|
|
|
|
server.close()
|
|
|
|
assert server.status() == .closed
|
|
|
|
assert watch.elapsed() < 100 * time.millisecond
|
2022-10-16 09:28:57 +03:00
|
|
|
t.wait()
|
2021-08-24 19:21:24 +03:00
|
|
|
assert watch.elapsed() < 999 * time.millisecond
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:42:30 +03:00
|
|
|
fn test_server_custom_listener() {
|
|
|
|
listener := net.listen_tcp(.ip6, ':8081')!
|
|
|
|
mut server := &http.Server{
|
|
|
|
accept_timeout: 1 * time.second
|
|
|
|
listener: listener
|
|
|
|
}
|
|
|
|
t := spawn server.listen_and_serve()
|
|
|
|
time.sleep(250 * time.millisecond)
|
|
|
|
mut watch := time.new_stopwatch()
|
|
|
|
server.close()
|
|
|
|
assert server.status() == .closed
|
|
|
|
assert watch.elapsed() < 100 * time.millisecond
|
|
|
|
t.wait()
|
|
|
|
assert watch.elapsed() < 999 * time.millisecond
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:21:24 +03:00
|
|
|
struct MyHttpHandler {
|
|
|
|
mut:
|
|
|
|
counter int
|
|
|
|
oks int
|
|
|
|
not_founds int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut handler MyHttpHandler) handle(req http.Request) http.Response {
|
|
|
|
handler.counter++
|
|
|
|
// eprintln('$time.now() | counter: $handler.counter | $req.method $req.url\n$req.header\n$req.data - 200 OK\n')
|
|
|
|
mut r := http.Response{
|
2022-11-15 16:53:13 +03:00
|
|
|
body: req.data + ', ${req.url}'
|
2021-08-24 19:21:24 +03:00
|
|
|
header: req.header
|
|
|
|
}
|
|
|
|
match req.url.all_before('?') {
|
|
|
|
'/endpoint', '/another/endpoint' {
|
|
|
|
r.set_status(.ok)
|
|
|
|
handler.oks++
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r.set_status(.not_found)
|
|
|
|
handler.not_founds++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.set_version(req.version)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
const cport = 8198
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_server_custom_handler() {
|
2021-08-24 19:21:24 +03:00
|
|
|
mut handler := MyHttpHandler{}
|
|
|
|
mut server := &http.Server{
|
|
|
|
accept_timeout: 1 * time.second
|
|
|
|
handler: handler
|
|
|
|
port: cport
|
|
|
|
}
|
2022-11-05 10:46:40 +03:00
|
|
|
t := spawn server.listen_and_serve()
|
2021-08-24 19:21:24 +03:00
|
|
|
for server.status() != .running {
|
|
|
|
time.sleep(10 * time.millisecond)
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
x := http.fetch(url: 'http://localhost:${cport}/endpoint?abc=xyz', data: 'my data')!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'my data, /endpoint?abc=xyz'
|
2021-08-24 19:21:24 +03:00
|
|
|
assert x.status_code == 200
|
2022-11-20 17:21:50 +03:00
|
|
|
assert x.status_msg == 'OK'
|
2021-08-24 19:21:24 +03:00
|
|
|
assert x.http_version == '1.1'
|
2022-11-15 16:53:13 +03:00
|
|
|
y := http.fetch(url: 'http://localhost:${cport}/another/endpoint', data: 'abcde')!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert y.body == 'abcde, /another/endpoint'
|
2021-08-24 19:21:24 +03:00
|
|
|
assert y.status_code == 200
|
2022-11-20 17:21:50 +03:00
|
|
|
assert x.status_msg == 'OK'
|
2021-08-24 19:21:24 +03:00
|
|
|
assert y.status() == .ok
|
|
|
|
assert y.http_version == '1.1'
|
|
|
|
//
|
2022-11-15 16:53:13 +03:00
|
|
|
http.fetch(url: 'http://localhost:${cport}/something/else')!
|
2021-08-24 19:21:24 +03:00
|
|
|
server.stop()
|
2022-10-16 09:28:57 +03:00
|
|
|
t.wait()
|
2021-08-24 19:21:24 +03:00
|
|
|
assert handler.counter == 3
|
|
|
|
assert handler.oks == 2
|
|
|
|
assert handler.not_founds == 1
|
|
|
|
}
|