andreimarcu
9 years ago
4 changed files with 147 additions and 18 deletions
-
35server.go
-
91server_test.go
-
5upload.go
-
34util.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) |
||||
|
} |
@ -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) |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue