ghostream/web/web_test.go

70 lines
2.0 KiB
Go
Raw Normal View History

2020-09-30 14:11:51 +00:00
package web
import (
"net/http"
"testing"
"time"
2020-10-17 10:38:18 +00:00
"gitlab.crans.org/nounous/ghostream/messaging"
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-17 10:38:18 +00:00
// Init streams messaging
streams := messaging.New()
2020-10-17 10:38:18 +00:00
// Create a disabled web server
go Serve(streams, 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
2020-10-17 10:38:18 +00:00
go Serve(streams, nil, nil, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
2020-10-09 21:57:39 +00:00
// 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-17 10:38:18 +00:00
// Test viewer statistics 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)
}
}