42 lines
1022 B
Go
42 lines
1022 B
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"yoresee_dropbox/internal/store"
|
|
)
|
|
|
|
func TestUpload(t *testing.T) {
|
|
dir := t.TempDir()
|
|
workspace := filepath.Join(dir, "workspace")
|
|
os.MkdirAll(filepath.Join(workspace, "inbox"), 0755)
|
|
|
|
s, _ := store.Open(filepath.Join(dir, "test.db"))
|
|
defer s.Close()
|
|
|
|
srv := &Server{store: s, accessToken: "token", sessions: make(map[string]int64), workspace: workspace}
|
|
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
part, _ := writer.CreateFormFile("file", "test.txt")
|
|
part.Write([]byte("hello"))
|
|
writer.Close()
|
|
|
|
req := httptest.NewRequest("POST", "/api/upload", body)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
req.AddCookie(&http.Cookie{Name: "ydropbox_session", Value: "valid"})
|
|
srv.sessions["valid"] = 1
|
|
|
|
w := httptest.NewRecorder()
|
|
srv.handleUpload(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Errorf("Status = %d, want %d", w.Code, http.StatusCreated)
|
|
}
|
|
}
|