Files to uuids only
This commit is contained in:
@@ -6,28 +6,35 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (rest *REST) files(w http.ResponseWriter, r *http.Request) {
|
||||
if len(r.URL.Path) < 2 {
|
||||
rest.respNotFound(w)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
rest.filesUpdate(w, r)
|
||||
case http.MethodPost:
|
||||
rest.filesCreate(w, r)
|
||||
case http.MethodGet:
|
||||
rest.filesGet(w, r)
|
||||
case http.MethodDelete:
|
||||
rest.filesDelete(w, r)
|
||||
default:
|
||||
rest.respNotFound(w)
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
rest.filesCreate(w, r)
|
||||
default:
|
||||
rest.respNotFound(w)
|
||||
}
|
||||
} else {
|
||||
switch r.Method {
|
||||
case http.MethodPut:
|
||||
rest.filesUpdate(w, r)
|
||||
case http.MethodGet:
|
||||
rest.filesGet(w, r)
|
||||
case http.MethodDelete:
|
||||
rest.filesDelete(w, r)
|
||||
default:
|
||||
rest.respNotFound(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rest *REST) filesCreate(w http.ResponseWriter, r *http.Request) {
|
||||
id := uuid.New().String()
|
||||
r.URL.Path = "/" + id
|
||||
localPath := rest.filesPath(r)
|
||||
if stat, err := os.Stat(localPath); !os.IsNotExist(err) || (stat != nil && stat.IsDir()) {
|
||||
rest.respConflict(w)
|
||||
@@ -46,7 +53,7 @@ func (rest *REST) filesCreate(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := io.Copy(f, r.Body); err != nil {
|
||||
rest.respError(w, err)
|
||||
}
|
||||
rest.respOK(w)
|
||||
w.Write([]byte(id))
|
||||
}
|
||||
|
||||
func (rest *REST) filesDelete(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -12,38 +12,23 @@ import (
|
||||
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)
|
||||
content := uuid.New().String()
|
||||
w := testFilesPost(t, rest, scope, content)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||
}
|
||||
s := 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()))
|
||||
}
|
||||
if s2 := string(w.Body.Bytes()); s2 != content {
|
||||
t.Fatalf("want %q, got %q", content, s2)
|
||||
}
|
||||
},
|
||||
"delete": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
||||
s := uuid.New().String()
|
||||
w := testFilesPost(t, rest, s, scope, s)
|
||||
w := testFilesPost(t, rest, scope, uuid.New().String())
|
||||
s := string(w.Body.Bytes())
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||
}
|
||||
@@ -64,8 +49,8 @@ func TestFiles(t *testing.T) {
|
||||
}
|
||||
},
|
||||
"update": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
||||
s := uuid.New().String()
|
||||
w := testFilesPost(t, rest, s, scope, s)
|
||||
w := testFilesPost(t, rest, scope, uuid.New().String())
|
||||
s := string(w.Body.Bytes())
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||
}
|
||||
@@ -105,8 +90,8 @@ func testFilesPut(t *testing.T, rest *REST, id string, scope func(*http.Request)
|
||||
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 testFilesPost(t *testing.T, rest *REST, scope func(*http.Request), body string) *httptest.ResponseRecorder {
|
||||
return testFilesReq(t, rest, "", scope, http.MethodPost, body)
|
||||
}
|
||||
|
||||
func testFilesReq(t *testing.T, rest *REST, id string, scope func(*http.Request), method, body string) *httptest.ResponseRecorder {
|
||||
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -124,7 +123,7 @@ func TestMiddlewareShift(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
rest := &REST{}
|
||||
foo := rest.shift(func(http.ResponseWriter, *http.Request) {})
|
||||
log.Printf("%+v", r.URL)
|
||||
t.Logf("%+v", r.URL)
|
||||
foo(w, r)
|
||||
if r.URL.String() != c.output {
|
||||
t.Fatalf("from %q, want %q, got %q", c.input, c.output, r.URL.String())
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestRESTRouter(t *testing.T) {
|
||||
fmt.Sprintf(`%s/%s?pu`, config.New().FilePrefix, testFilename): {
|
||||
method: http.MethodPut,
|
||||
},
|
||||
fmt.Sprintf(`%s/%s?po`, config.New().FilePrefix, testFilename): {
|
||||
fmt.Sprintf(`%s?po`, config.New().FilePrefix): {
|
||||
method: http.MethodPost,
|
||||
},
|
||||
fmt.Sprintf(`%s/%s?d`, config.New().FilePrefix, testFilename): {
|
||||
|
||||
Reference in New Issue
Block a user