From 8c50d4322f3891eb08c07bf195cf8959cc724206 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Mon, 28 Sep 2015 12:30:21 -0400 Subject: [PATCH] Added support for testing, removed uuid requirement --- server.go | 35 ++++++++++--------- server_test.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ upload.go | 5 ++- util.go | 34 +++++++++++++++++++ 4 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 server_test.go create mode 100644 util.go diff --git a/server.go b/server.go index 59b2105..7abdaa1 100644 --- a/server.go +++ b/server.go @@ -23,21 +23,7 @@ var Config struct { siteURL string } -func main() { - flag.StringVar(&Config.bind, "b", "127.0.0.1:8080", - "host to bind to (default: 127.0.0.1:8080)") - flag.StringVar(&Config.filesDir, "filespath", "files/", - "path to files directory") - flag.StringVar(&Config.metaDir, "metapath", "meta/", - "path to metadata directory") - flag.BoolVar(&Config.noLogs, "nologs", false, - "remove stdout output for each request") - flag.StringVar(&Config.siteName, "sitename", "linx", - "name of the site") - flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/", - "site base url (including trailing slash)") - flag.Parse() - +func setup() { if Config.noLogs { goji.Abandon(middleware.Logger) } @@ -82,6 +68,25 @@ func main() { goji.Get(selifRe, fileServeHandler) goji.NotFound(notFoundHandler) +} + +func main() { + flag.StringVar(&Config.bind, "b", "127.0.0.1:8080", + "host to bind to (default: 127.0.0.1:8080)") + flag.StringVar(&Config.filesDir, "filespath", "files/", + "path to files directory") + flag.StringVar(&Config.metaDir, "metapath", "meta/", + "path to metadata directory") + flag.BoolVar(&Config.noLogs, "nologs", false, + "remove stdout output for each request") + flag.StringVar(&Config.siteName, "sitename", "linx", + "name of the site") + flag.StringVar(&Config.siteURL, "siteurl", "http://"+Config.bind+"/", + "site base url (including trailing slash)") + flag.Parse() + + setup() + listener, err := net.Listen("tcp", Config.bind) if err != nil { log.Fatal("Could not bind: ", err) diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..c95e680 --- /dev/null +++ b/server_test.go @@ -0,0 +1,91 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + + "github.com/zenazn/goji" +) + +var a = 0 + +func TestSetup(t *testing.T) { + Config.siteURL = "http://linx.example.org/" + Config.filesDir = "/tmp/" + randomString(10) + Config.metaDir = Config.filesDir + "_meta" + Config.noLogs = true + Config.siteName = "linx" + setup() +} + +func TestIndex(t *testing.T) { + w := httptest.NewRecorder() + + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + + goji.DefaultMux.ServeHTTP(w, req) + + if !strings.Contains(w.Body.String(), "file-uploader") { + t.Error("String 'file-uploader' not found in index response") + } +} + +func TestNotFound(t *testing.T) { + w := httptest.NewRecorder() + + req, err := http.NewRequest("GET", "/url/should/not/exist", nil) + if err != nil { + t.Fatal(err) + } + + goji.DefaultMux.ServeHTTP(w, req) + + if w.Code != 404 { + t.Fatalf("Expected 404, got %d", w.Code) + } +} + +func TestFileNotFound(t *testing.T) { + w := httptest.NewRecorder() + + filename := randomString(10) + + req, err := http.NewRequest("GET", "/selif/"+filename, nil) + if err != nil { + t.Fatal(err) + } + + goji.DefaultMux.ServeHTTP(w, req) + + if w.Code != 404 { + t.Fatalf("Expected 404, got %d", w.Code) + } +} + +func TestDisplayNotFound(t *testing.T) { + w := httptest.NewRecorder() + + filename := randomString(10) + + req, err := http.NewRequest("GET", "/"+filename, nil) + if err != nil { + t.Fatal(err) + } + + goji.DefaultMux.ServeHTTP(w, req) + + if w.Code != 404 { + t.Fatalf("Expected 404, got %d", w.Code) + } +} + +func TestShutdown(t *testing.T) { + os.RemoveAll(Config.filesDir) + os.RemoveAll(Config.metaDir) +} diff --git a/upload.go b/upload.go index 33d0f70..a5ac710 100644 --- a/upload.go +++ b/upload.go @@ -11,7 +11,6 @@ import ( "strconv" "strings" - "github.com/pborman/uuid" "github.com/zenazn/goji/web" ) @@ -159,7 +158,7 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { // If no delete key specified, pick a random one. if upReq.deletionKey == "" { - upload.DeleteKey = uuid.New()[:30] + upload.DeleteKey = randomString(30) } else { upload.DeleteKey = upReq.deletionKey } @@ -178,7 +177,7 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { } func generateBarename() string { - return uuid.New()[:8] + return randomString(8) } func generateJSONresponse(upload Upload) []byte { diff --git a/util.go b/util.go new file mode 100644 index 0000000..6ebb5d0 --- /dev/null +++ b/util.go @@ -0,0 +1,34 @@ +package main + +import ( + "math/rand" + "time" +) + +// from http://stackoverflow.com/a/31832326 +var src = rand.NewSource(time.Now().UnixNano()) + +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +const ( + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = src.Int63(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- + } + + return string(b) +}