118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunHTTP(t *testing.T) {
|
|
cfg := Config{
|
|
fsDB: t.TempDir(),
|
|
}
|
|
|
|
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.StatusNotFound {
|
|
t.Error(w.Code)
|
|
}
|
|
t.Errorf("not impl: %s", w.Body.Bytes())
|
|
})
|
|
|
|
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.StatusNotFound {
|
|
t.Error(w.Code)
|
|
}
|
|
t.Errorf("not impl: %s", w.Body.Bytes())
|
|
})
|
|
|
|
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.StatusNotFound {
|
|
t.Error(w.Code)
|
|
}
|
|
t.Errorf("not impl: %s", w.Body.Bytes())
|
|
})
|
|
|
|
t.Run("POST /api/v1/questions/0/answers", func(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodPost, "/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.StatusNotFound {
|
|
t.Error(w.Code)
|
|
}
|
|
t.Errorf("not impl: %s", w.Body.Bytes())
|
|
})
|
|
}
|
|
|
|
func TestPublic(t *testing.T) {
|
|
f, err := public.Open("index.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
}
|