Static file upload via optional direct link or multipart form
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/dndex/config"
|
||||
"local/dndex/storage"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -59,7 +61,7 @@ func TestFiles(t *testing.T) {
|
||||
}
|
||||
f.Write([]byte("hello, world"))
|
||||
f.Close()
|
||||
r := httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, path.Base(f.Name())), nil)
|
||||
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s?direct=true", path.Join(config.New().FilePrefix, path.Base(f.Name()))), nil)
|
||||
w := httptest.NewRecorder()
|
||||
t.Logf("URL = %q", r.URL.String())
|
||||
handler.ServeHTTP(w, r)
|
||||
@@ -75,4 +77,85 @@ func TestFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("post file: direct link", func(t *testing.T) {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "*.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Write([]byte("hello, world"))
|
||||
f.Close()
|
||||
name := path.Base(f.Name())
|
||||
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hi"))
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
r := httptest.NewRequest(http.MethodPost, fmt.Sprintf("%s?direct=true", path.Join(config.New().FilePrefix, name)), strings.NewReader(s.URL))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
|
||||
r = httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, name), nil)
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
if body := string(w.Body.Bytes()); body != "hi" {
|
||||
t.Fatal(body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("post file: bad direct link", func(t *testing.T) {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "*.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Write([]byte("hello, world"))
|
||||
f.Close()
|
||||
|
||||
r := httptest.NewRequest(http.MethodPost, path.Join(config.New().FilePrefix, path.Base(f.Name())), strings.NewReader(`bad link teehee`))
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code == http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("post file: form file", func(t *testing.T) {
|
||||
f, err := ioutil.TempFile(os.TempDir(), "*.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
name := path.Base(f.Name())
|
||||
b := bytes.NewBuffer(nil)
|
||||
writer := multipart.NewWriter(b)
|
||||
w2, _ := writer.CreateFormFile("file", name)
|
||||
w2.Write([]byte("hello, world"))
|
||||
writer.Close()
|
||||
|
||||
r := httptest.NewRequest(http.MethodPost, path.Join(config.New().FilePrefix, name), b)
|
||||
r.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
|
||||
r = httptest.NewRequest(http.MethodGet, path.Join(config.New().FilePrefix, name), nil)
|
||||
w = httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
if body := string(w.Body.Bytes()); body != "hello, world" {
|
||||
t.Fatal(body)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user