1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 00:22:19 +00:00
ghostream/web/web_test.go

64 lines
1.9 KiB
Go
Raw Normal View History

2020-09-30 14:11:51 +00:00
package web
import (
"net/http"
"testing"
"time"
2020-09-30 14:11:51 +00:00
)
// TestHTTPServe tries to serve a real HTTP server and load some pages
func TestHTTPServe(t *testing.T) {
2020-10-09 21:57:39 +00:00
go Serve(nil, nil, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})
// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
time.Sleep(500 * time.Millisecond)
2020-10-09 21:57:39 +00:00
// Test GET request, should fail
resp, err := http.Get("http://localhost:8081/")
2020-10-09 21:57:39 +00:00
if err == nil {
t.Error("Web server did init with Enabled=false")
}
// Now let's really start the web server
go Serve(nil, nil, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
time.Sleep(500 * time.Millisecond)
// Test home page
resp, err = http.Get("http://localhost:8081/")
if err != nil {
2020-10-09 21:57:39 +00:00
t.Errorf("Error while getting /: %s", err)
}
2020-09-30 14:11:51 +00:00
if resp.StatusCode != http.StatusOK {
t.Errorf("Viewer page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
}
2020-10-09 21:57:39 +00:00
// Test viewer page
resp, err = http.Get("http://localhost:8081/demo")
if err != nil {
2020-10-09 21:57:39 +00:00
t.Errorf("Error while getting /demo: %s", err)
}
2020-09-30 14:11:51 +00:00
if resp.StatusCode != http.StatusOK {
t.Errorf("Viewer page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
}
2020-10-09 21:57:39 +00:00
// Test viewer static file
resp, err = http.Get("http://localhost:8081/static/js/viewer.js")
if err != nil {
2020-10-09 21:57:39 +00:00
t.Errorf("Error while getting /static/js/viewer/js: %s", err)
}
2020-09-30 14:11:51 +00:00
if resp.StatusCode != http.StatusOK {
t.Errorf("Viewer page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
}
2020-10-09 21:57:39 +00:00
// Test viewer statistic endpoint
2020-10-06 07:32:41 +00:00
resp, err = http.Get("http://localhost:8081/_stats/demo/")
if err != nil {
2020-10-09 21:57:39 +00:00
t.Errorf("Error while getting /_stats: %s", err)
}
2020-09-30 14:11:51 +00:00
if resp.StatusCode != http.StatusOK {
t.Errorf("Viewer page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
}
}