Browse Source

Added support for testing, removed uuid requirement

pull/12/head
andreimarcu 9 years ago
parent
commit
8c50d4322f
  1. 35
      server.go
  2. 91
      server_test.go
  3. 5
      upload.go
  4. 34
      util.go

35
server.go

@ -23,21 +23,7 @@ var Config struct {
siteURL string 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 { if Config.noLogs {
goji.Abandon(middleware.Logger) goji.Abandon(middleware.Logger)
} }
@ -82,6 +68,25 @@ func main() {
goji.Get(selifRe, fileServeHandler) goji.Get(selifRe, fileServeHandler)
goji.NotFound(notFoundHandler) 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) listener, err := net.Listen("tcp", Config.bind)
if err != nil { if err != nil {
log.Fatal("Could not bind: ", err) log.Fatal("Could not bind: ", err)

91
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)
}

5
upload.go

@ -11,7 +11,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/pborman/uuid"
"github.com/zenazn/goji/web" "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 no delete key specified, pick a random one.
if upReq.deletionKey == "" { if upReq.deletionKey == "" {
upload.DeleteKey = uuid.New()[:30]
upload.DeleteKey = randomString(30)
} else { } else {
upload.DeleteKey = upReq.deletionKey upload.DeleteKey = upReq.deletionKey
} }
@ -178,7 +177,7 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
} }
func generateBarename() string { func generateBarename() string {
return uuid.New()[:8]
return randomString(8)
} }
func generateJSONresponse(upload Upload) []byte { func generateJSONresponse(upload Upload) []byte {

34
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<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func randomString(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 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)
}
Loading…
Cancel
Save