Files to uuids only

master
breel 2020-08-26 20:31:08 -06:00
parent c85465accf
commit bb9b91eef5
7 changed files with 58 additions and 56 deletions

View File

@ -0,0 +1,17 @@
paths:
summary: "Create files"
post:
tags:
- files
parameters:
- $ref: "#/components/parameters/token"
requestBody:
$ref: "#/components/schemas/requestForm"
components:
parameters:
token:
$ref: "../swagger.yaml#/components/parameters/token"
schemas:
requestForm:
$ref: "../swagger.yaml#/components/schemas/requestForm"

View File

@ -6,14 +6,6 @@ paths:
parameters:
- $ref: "#/components/parameters/token"
- $ref: "#/components/parameters/path"
post:
tags:
- files
parameters:
- $ref: "#/components/parameters/token"
- $ref: "#/components/parameters/path"
requestBody:
$ref: "#/components/schemas/requestForm"
put:
tags:
- files
@ -32,9 +24,9 @@ paths:
components:
parameters:
token:
$ref: "./swagger.yaml#/components/parameters/token"
$ref: "../swagger.yaml#/components/parameters/token"
path:
$ref: "./swagger.yaml#/components/parameters/path"
$ref: "../swagger.yaml#/components/parameters/path"
schemas:
requestForm:
$ref: "./swagger.yaml#/components/schemas/requestForm"
$ref: "../swagger.yaml#/components/schemas/requestForm"

View File

@ -17,8 +17,10 @@ servers:
paths:
/version:
$ref: "./version.yaml#/paths"
/files:
$ref: "./files/index.yaml#/paths"
/files/{path}:
$ref: "./files.yaml#/paths"
$ref: "./files/one.yaml#/paths"
/users/register:
$ref: "./users/register.yaml#/paths"
/users/login:

View File

@ -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) {

View File

@ -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 {

View File

@ -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())

View File

@ -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): {