live-studio-audience/cmd/server/main_test.go

147 lines
3.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
)
func TestRunHTTP(t *testing.T) {
cfg := Config{
fsDB: t.TempDir(),
}
if err := func() error {
b, _ := json.Marshal(Question{
Text: "QUESTION TEXT",
Options: []string{"X", "Y"},
})
return os.WriteFile(path.Join(string(cfg.fsDB), "0"), b, os.ModePerm)
}(); err != nil {
t.Fatal(err)
}
h := cfg.NewHandler()
t.Run("requires auth", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != 401 {
t.Error(w.Code)
}
if w.Header().Get("WWW-Authenticate") == "" {
t.Errorf("expected WWW-Authenticate header but got %+v", w.Header())
}
})
t.Run("/", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
if !bytes.Contains(w.Body.Bytes(), []byte("<html>")) {
t.Errorf("%s", w.Body.Bytes())
}
})
t.Run("/api/notfound", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/notfound", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusNotFound {
t.Error(w.Code)
}
})
t.Run("/api/v1/questions", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/v1/questions", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
var result []Question
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint([]Question{{ID: "0", Live: false, Closed: false, Text: "QUESTION TEXT", Options: []string{"X", "Y"}}}) {
t.Error(result)
}
})
t.Run("/api/v1/questions/0", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/v1/questions/0", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
var result Question
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint(Question{ID: "0", Text: "QUESTION TEXT", Options: []string{"X", "Y"}}) {
t.Error(result)
}
})
t.Run("POST /api/v1/questions/0/answers", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, "/api/v1/questions/0/answers", strings.NewReader(`{"Text": "teehee"}`))
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
})
t.Run("GET /api/v1/questions/0/answers", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/v1/questions/0/answers", nil)
r.SetBasicAuth("b", "b")
w := httptest.NewRecorder()
t.Logf("%s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
t.Logf("(%d) %s", w.Code, w.Body.Bytes())
if w.Code != http.StatusOK {
t.Error(w.Code)
}
var result []Answer
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Error(err)
} else if fmt.Sprint(result) != fmt.Sprint([]Answer{{Text: "teehee"}}) {
t.Error(result)
}
})
}
func TestPublic(t *testing.T) {
f, err := public.Open("index.html")
if err != nil {
t.Fatal(err)
}
defer f.Close()
}