119 lines
3.4 KiB
Go
119 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestFiles(t *testing.T) {
|
|
cases := map[string]func(*testing.T, *REST, func(*http.Request)){
|
|
"create-get": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
s := uuid.New().String()
|
|
w := testFilesPost(t, rest, s, scope, s)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
w = testFilesGet(t, rest, s, scope)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
if s2 := string(w.Body.Bytes()); s2 != s {
|
|
t.Fatalf("want %q, got %q", s, s2)
|
|
}
|
|
},
|
|
"create-collision": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
s := uuid.New().String()
|
|
for i := 0; i < 2; i++ {
|
|
w := testFilesPost(t, rest, s, scope, s)
|
|
ok := false
|
|
switch i {
|
|
case 0:
|
|
ok = w.Code == http.StatusOK
|
|
default:
|
|
ok = w.Code == http.StatusConflict
|
|
}
|
|
if !ok {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
}
|
|
},
|
|
"delete": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
s := uuid.New().String()
|
|
w := testFilesPost(t, rest, s, scope, s)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
w = testFilesDelete(t, rest, s, scope)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
w = testFilesGet(t, rest, s, scope)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
},
|
|
"404": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
s := uuid.New().String()
|
|
w := testFilesGet(t, rest, s, scope)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
},
|
|
"update": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
s := uuid.New().String()
|
|
w := testFilesPost(t, rest, s, scope, s)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
w = testFilesPut(t, rest, s, scope, s+"new")
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
w = testFilesGet(t, rest, s, scope)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
}
|
|
if v := string(w.Body.Bytes()); v != s+"new" {
|
|
t.Fatal(v)
|
|
}
|
|
},
|
|
}
|
|
|
|
for name, foo := range cases {
|
|
bar := foo
|
|
t.Run(name, func(t *testing.T) {
|
|
rest, scope, clean := testREST(t)
|
|
bar(t, rest, scope)
|
|
defer clean()
|
|
})
|
|
}
|
|
}
|
|
|
|
func testFilesDelete(t *testing.T, rest *REST, id string, scope func(*http.Request)) *httptest.ResponseRecorder {
|
|
return testFilesReq(t, rest, id, scope, http.MethodDelete, "")
|
|
}
|
|
|
|
func testFilesGet(t *testing.T, rest *REST, id string, scope func(*http.Request)) *httptest.ResponseRecorder {
|
|
return testFilesReq(t, rest, id, scope, http.MethodGet, "")
|
|
}
|
|
|
|
func testFilesPut(t *testing.T, rest *REST, id string, scope func(*http.Request), body string) *httptest.ResponseRecorder {
|
|
return testFilesReq(t, rest, id, scope, http.MethodPut, body)
|
|
}
|
|
|
|
func testFilesPost(t *testing.T, rest *REST, id string, scope func(*http.Request), body string) *httptest.ResponseRecorder {
|
|
return testFilesReq(t, rest, id, scope, http.MethodPost, body)
|
|
}
|
|
|
|
func testFilesReq(t *testing.T, rest *REST, id string, scope func(*http.Request), method, body string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, "/"+id, strings.NewReader(body))
|
|
scope(r)
|
|
w := httptest.NewRecorder()
|
|
rest.files(w, r)
|
|
return w
|
|
}
|