Added tests for uploads
This commit is contained in:
parent
ca0754725b
commit
5f78fe6619
|
@ -58,7 +58,7 @@ func setup() {
|
||||||
goji.Get("/", indexHandler)
|
goji.Get("/", indexHandler)
|
||||||
|
|
||||||
goji.Post("/upload", uploadPostHandler)
|
goji.Post("/upload", uploadPostHandler)
|
||||||
goji.Post("/upload/", http.RedirectHandler("/upload", 301))
|
goji.Post("/upload/", uploadPostHandler)
|
||||||
goji.Put("/upload", uploadPutHandler)
|
goji.Put("/upload", uploadPutHandler)
|
||||||
goji.Put("/upload/:name", uploadPutHandler)
|
goji.Put("/upload/:name", uploadPutHandler)
|
||||||
|
|
||||||
|
|
253
server_test.go
253
server_test.go
|
@ -1,10 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -14,6 +16,7 @@ import (
|
||||||
func TestSetup(t *testing.T) {
|
func TestSetup(t *testing.T) {
|
||||||
Config.siteURL = "http://linx.example.org/"
|
Config.siteURL = "http://linx.example.org/"
|
||||||
Config.filesDir = path.Join(os.TempDir(), randomString(10))
|
Config.filesDir = path.Join(os.TempDir(), randomString(10))
|
||||||
|
t.Log(os.TempDir())
|
||||||
Config.metaDir = Config.filesDir + "_meta"
|
Config.metaDir = Config.filesDir + "_meta"
|
||||||
Config.noLogs = true
|
Config.noLogs = true
|
||||||
Config.siteName = "linx"
|
Config.siteName = "linx"
|
||||||
|
@ -31,7 +34,7 @@ func TestIndex(t *testing.T) {
|
||||||
goji.DefaultMux.ServeHTTP(w, req)
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
if !strings.Contains(w.Body.String(), "file-uploader") {
|
if !strings.Contains(w.Body.String(), "file-uploader") {
|
||||||
t.Error("String 'file-uploader' not found in index response")
|
t.Fatal("String 'file-uploader' not found in index response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +87,254 @@ func TestDisplayNotFound(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPostBodyUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
params := req.URL.Query()
|
||||||
|
params.Add("qqfile", filename)
|
||||||
|
req.URL.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != 301 {
|
||||||
|
t.Fatalf("Status code is not 301, but %d", w.Code)
|
||||||
|
}
|
||||||
|
if w.Header().Get("Location") != "/"+filename {
|
||||||
|
t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostEmptyBodyUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "/upload", strings.NewReader(""))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
params := req.URL.Query()
|
||||||
|
params.Add("qqfile", filename)
|
||||||
|
req.URL.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code == 301 {
|
||||||
|
t.Fatal("Status code is 301")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
|
||||||
|
t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostBodyRandomizeUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/octet-stream")
|
||||||
|
params := req.URL.Query()
|
||||||
|
params.Add("qqfile", filename)
|
||||||
|
params.Add("randomize", "true")
|
||||||
|
req.URL.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Code != 301 {
|
||||||
|
t.Fatalf("Status code is not 301, but %d", w.Code)
|
||||||
|
}
|
||||||
|
if w.Header().Get("Location") == "/"+filename {
|
||||||
|
t.Fatalf("Was redirected to %s instead of something random", filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostBodyExpireUpload(t *testing.T) {
|
||||||
|
// Dependant on json info on display url to check expiry
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Body.String() != Config.siteURL+filename {
|
||||||
|
t.Fatal("Response was not expected URL")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if w.Code != 301 {
|
||||||
|
// t.Fatalf("Status code is not 301, but %d", w.Code)
|
||||||
|
// }
|
||||||
|
// if w.Header().Get("Location") != "/"+filename {
|
||||||
|
// t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutRandomizedUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("X-Randomized-Barename", "yes")
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if w.Body.String() == Config.siteURL+filename {
|
||||||
|
t.Fatal("Filename was not random")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutEmptyUpload(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("X-Randomized-Barename", "yes")
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
|
||||||
|
t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutJSONUpload(t *testing.T) {
|
||||||
|
type RespJSON struct {
|
||||||
|
Filename string `json: filename`
|
||||||
|
Url string `json: url`
|
||||||
|
DeleteKey string `json: delete_key`
|
||||||
|
Expiry string `json: expiry`
|
||||||
|
Size string `json: size`
|
||||||
|
}
|
||||||
|
var myjson RespJSON
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if myjson.Filename != filename {
|
||||||
|
t.Fatal("Filename was not provided one but " + myjson.Filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutRandomizedJSONUpload(t *testing.T) {
|
||||||
|
type RespJSON struct {
|
||||||
|
Filename string `json: filename`
|
||||||
|
Url string `json: url`
|
||||||
|
DeleteKey string `json: delete_key`
|
||||||
|
Expiry string `json: expiry`
|
||||||
|
Size string `json: size`
|
||||||
|
}
|
||||||
|
var myjson RespJSON
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
req.Header.Set("X-Randomized-Barename", "yes")
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if myjson.Filename == filename {
|
||||||
|
t.Fatal("Filename was not random ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutExpireJSONUpload(t *testing.T) {
|
||||||
|
type RespJSON struct {
|
||||||
|
Filename string `json: filename`
|
||||||
|
Url string `json: url`
|
||||||
|
DeleteKey string `json: delete_key`
|
||||||
|
Expiry string `json: expiry`
|
||||||
|
Size string `json: size`
|
||||||
|
}
|
||||||
|
var myjson RespJSON
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
filename := randomString(10) + ".ext"
|
||||||
|
|
||||||
|
req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
req.Header.Set("X-File-Expiry", "600")
|
||||||
|
|
||||||
|
goji.DefaultMux.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expiry, err := strconv.Atoi(myjson.Expiry)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Expiry was not an integer")
|
||||||
|
}
|
||||||
|
if expiry < 1 {
|
||||||
|
t.Fatal("Expiry was not set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestShutdown(t *testing.T) {
|
func TestShutdown(t *testing.T) {
|
||||||
os.RemoveAll(Config.filesDir)
|
os.RemoveAll(Config.filesDir)
|
||||||
os.RemoveAll(Config.metaDir)
|
os.RemoveAll(Config.metaDir)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -118,6 +119,9 @@ func uploadHeaderProcess(r *http.Request, upReq *UploadRequest) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
||||||
|
|
||||||
|
// if UploadRequest.src
|
||||||
|
|
||||||
// Determine the appropriate filename, then write to disk
|
// Determine the appropriate filename, then write to disk
|
||||||
barename, extension := barePlusExt(upReq.filename)
|
barename, extension := barePlusExt(upReq.filename)
|
||||||
|
|
||||||
|
@ -169,7 +173,9 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
} else if bytes == 0 {
|
} else if bytes == 0 {
|
||||||
return
|
os.Remove(path.Join(Config.filesDir, upload.Filename))
|
||||||
|
os.Remove(path.Join(Config.metaDir, upload.Filename))
|
||||||
|
return upload, errors.New("Empty file")
|
||||||
}
|
}
|
||||||
|
|
||||||
upload.Size = bytes
|
upload.Size = bytes
|
||||||
|
|
Loading…
Reference in New Issue