2015-09-28 12:30:21 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-28 16:02:03 -04:00
|
|
|
"encoding/json"
|
2015-09-28 12:30:21 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2015-09-28 12:35:11 -04:00
|
|
|
"path"
|
2015-09-28 16:02:03 -04:00
|
|
|
"strconv"
|
2015-09-28 12:30:21 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/zenazn/goji"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
|
|
|
Config.siteURL = "http://linx.example.org/"
|
2015-09-28 12:35:11 -04:00
|
|
|
Config.filesDir = path.Join(os.TempDir(), randomString(10))
|
2015-09-28 12:30:21 -04:00
|
|
|
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)
|
|
|
|
|
2015-09-30 23:37:00 -04:00
|
|
|
if !strings.Contains(w.Body.String(), "Click or Drop file") {
|
|
|
|
t.Fatal("String 'Click or Drop file' not found in index response")
|
2015-09-28 12:30:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 16:02:03 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-10-04 12:47:20 -04:00
|
|
|
if !strings.Contains(w.Body.String(), "Empty file") {
|
|
|
|
t.Fatal("Response doesn't contain'Empty file'")
|
2015-09-28 16:02:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutJSONUpload(t *testing.T) {
|
|
|
|
type RespJSON struct {
|
2015-09-29 00:32:16 -04:00
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
2015-09-28 16:02:03 -04:00
|
|
|
}
|
|
|
|
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 {
|
2015-09-29 00:32:16 -04:00
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
2015-09-28 16:02:03 -04:00
|
|
|
}
|
|
|
|
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 {
|
2015-09-29 00:32:16 -04:00
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
2015-09-28 16:02:03 -04:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 00:32:16 -04:00
|
|
|
func TestPutAndDelete(t *testing.T) {
|
|
|
|
type RespJSON struct {
|
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
|
|
|
}
|
|
|
|
var myjson RespJSON
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
|
|
|
|
req.Header.Set("X-Delete-Key", myjson.Delete_Key)
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
2015-10-04 02:25:00 -04:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's actually gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
// Make sure torrent is also gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPutAndSpecificDelete(t *testing.T) {
|
|
|
|
type RespJSON struct {
|
|
|
|
Filename string
|
|
|
|
Url string
|
|
|
|
Delete_Key string
|
|
|
|
Expiry string
|
|
|
|
Size string
|
|
|
|
}
|
|
|
|
var myjson RespJSON
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
req.Header.Set("X-Delete-Key", "supersecret")
|
|
|
|
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(w.Body.String()), &myjson)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete it
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
|
|
|
|
req.Header.Set("X-Delete-Key", "supersecret")
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
2015-10-04 02:25:00 -04:00
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's actually gone
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 01:58:14 -04:00
|
|
|
|
|
|
|
// Make sure torrent is gone too
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
|
|
|
|
goji.DefaultMux.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 404 {
|
|
|
|
t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
|
|
|
|
}
|
2015-09-29 00:32:16 -04:00
|
|
|
}
|
|
|
|
|
2015-09-28 12:30:21 -04:00
|
|
|
func TestShutdown(t *testing.T) {
|
|
|
|
os.RemoveAll(Config.filesDir)
|
|
|
|
os.RemoveAll(Config.metaDir)
|
|
|
|
}
|