47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package server
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"local/dndex/config"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
func TestRESTStatic(t *testing.T) {
|
|
os.Args = []string{"a"}
|
|
d, err := ioutil.TempDir(os.TempDir(), "static*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Setenv("STATIC_ROOT", d)
|
|
if err := ioutil.WriteFile(path.Join(d, "index.html"), []byte("Hello, world"), os.ModePerm); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rest, _, clean := testREST(t)
|
|
defer clean()
|
|
|
|
t.Run("assert nonstatic OK", func(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, path.Join("/", config.New().APIPrefix, "version"), nil)
|
|
w := httptest.NewRecorder()
|
|
rest.router.ServeHTTP(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code)
|
|
}
|
|
})
|
|
|
|
t.Run("assert static OK", func(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
w := httptest.NewRecorder()
|
|
rest.router.ServeHTTP(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if s := string(w.Body.Bytes()); s != "Hello, world" {
|
|
t.Fatal(s)
|
|
}
|
|
})
|
|
}
|