59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestConsoleIsEmbeddedAndDoesNotRequireCredentials(t *testing.T) {
|
|
server := New("127.0.0.1:0", nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/console/", nil)
|
|
w := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
if got := w.Header().Get("Content-Type"); !strings.Contains(got, "text/html") {
|
|
t.Fatalf("content type = %q, want HTML", got)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "Orbit Console") {
|
|
t.Fatal("embedded console page is missing its title")
|
|
}
|
|
}
|
|
|
|
func TestConsoleRedirectsWithoutTouchingAPIAuthentication(t *testing.T) {
|
|
server := New("127.0.0.1:0", nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/console", nil)
|
|
w := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(w, r)
|
|
if w.Code != http.StatusPermanentRedirect || w.Header().Get("Location") != "/console/" {
|
|
t.Fatalf("redirect = %d %q", w.Code, w.Header().Get("Location"))
|
|
}
|
|
}
|
|
|
|
func TestConsoleConfigReadsOnlyDefaultQueue(t *testing.T) {
|
|
t.Setenv("ORBIT_CONSOLE_QUEUE_ID", "queue-default")
|
|
server := New("127.0.0.1:0", nil)
|
|
r := httptest.NewRequest(http.MethodGet, "/console/config", nil)
|
|
w := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want 200", w.Code)
|
|
}
|
|
if got := w.Header().Get("Cache-Control"); got != "no-store" {
|
|
t.Fatalf("cache control = %q, want no-store", got)
|
|
}
|
|
var payload struct {
|
|
DefaultQueueID string `json:"default_queue_id"`
|
|
}
|
|
if err := json.NewDecoder(w.Body).Decode(&payload); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if payload.DefaultQueueID != "queue-default" {
|
|
t.Fatalf("default queue = %q, want queue-default", payload.DefaultQueueID)
|
|
}
|
|
}
|