2020-09-30 14:11:51 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2020-10-01 07:47:59 +00:00
|
|
|
"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) {
|
|
|
|
// Load templates
|
|
|
|
if err := loadTemplates(); err != nil {
|
|
|
|
t.Errorf("Failed to load templates: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go Serve(nil, nil, &Options{ListenAddress: "127.0.0.1:8081"})
|
2020-10-01 07:47:59 +00:00
|
|
|
|
|
|
|
// Sleep 1 second to ensure that the web server is running, to avoid fails because the request came too early
|
|
|
|
time.Sleep(1000000000)
|
|
|
|
|
2020-09-30 14:11:51 +00:00
|
|
|
// Test GET request
|
2020-10-01 07:47:59 +00:00
|
|
|
resp, err := http.Get("http://localhost:8081/")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error while getting /:", 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-01 07:47:59 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:8081/demo")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error while getting /demo:", 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-01 07:47:59 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:8081/static/js/viewer.js")
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error while getting /static/js/viewer/js:", 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-01 07:47:59 +00:00
|
|
|
|
2020-10-06 07:32:41 +00:00
|
|
|
resp, err = http.Get("http://localhost:8081/_stats/demo/")
|
2020-10-01 07:47:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error while getting /_stats:", 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)
|
|
|
|
}
|
|
|
|
}
|