86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestServerRoutes(t *testing.T) {
|
|
server := NewServer(t.TempDir())
|
|
if err := server.Routes(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ensureAndWrite(server.diskMediaPath("id"), []byte("hi"))
|
|
ensureAndWrite(server.diskMediaPath("delid"), []byte("hi"))
|
|
ensureAndWrite(path.Join(server.root, "index.html"), []byte("mom"))
|
|
|
|
cases := map[string]struct {
|
|
path string
|
|
method string
|
|
want string
|
|
}{
|
|
"v0: /: get": {
|
|
path: "/",
|
|
method: http.MethodGet,
|
|
want: "mom",
|
|
},
|
|
"v0: search: get": {
|
|
path: "/api/v0/search",
|
|
method: http.MethodGet,
|
|
},
|
|
"v0: tree: get": {
|
|
path: "/api/v0/tree",
|
|
method: http.MethodGet,
|
|
want: "{}",
|
|
},
|
|
"v0: media: post": {
|
|
path: "/api/v0/media",
|
|
method: http.MethodPost,
|
|
want: `{"data":{"filePath":"/api/v0/media/`,
|
|
},
|
|
"v0: media id: del": {
|
|
path: "/api/v0/media/delid",
|
|
method: http.MethodDelete,
|
|
},
|
|
"v0: media id: get": {
|
|
path: "/api/v0/media/id",
|
|
method: http.MethodGet,
|
|
want: "hi",
|
|
},
|
|
"v0: files: post": {
|
|
path: "/api/v0/files",
|
|
method: http.MethodPost,
|
|
},
|
|
"v0: files id: get": {
|
|
path: "/api/v0/files/id",
|
|
method: http.MethodGet,
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
r := httptest.NewRequest(c.method, c.path, nil)
|
|
w := httptest.NewRecorder()
|
|
server.ServeHTTP(w, r)
|
|
if w.Code == http.StatusNotFound {
|
|
t.Fatal(w)
|
|
}
|
|
if !bytes.HasPrefix(w.Body.Bytes(), []byte("not impl")) {
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w)
|
|
}
|
|
if len(c.want) > 0 && !strings.Contains(string(w.Body.Bytes()), c.want) {
|
|
t.Fatal(w)
|
|
}
|
|
t.Logf("%s %s => %s", c.method, c.path, w.Body.Bytes())
|
|
}
|
|
})
|
|
}
|
|
}
|