-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
98 lines (78 loc) · 2.74 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestFileHandler(t *testing.T) {
// Set up environment variables
os.Setenv("SERVER_PASSWORD", "testpassword")
os.Setenv("STORAGE_FOLDER", "testdata")
defer os.Unsetenv("SERVER_PASSWORD")
defer os.Unsetenv("STORAGE_FOLDER")
// Create the testdata directory if it doesn't exist
if err := os.MkdirAll("testdata", os.ModePerm); err != nil {
t.Fatalf("failed to create testdata directory: %v", err)
}
// Create a temporary database
dbPath := filepath.Join("testdata", "file_metadata.db")
db := NewDatabase(dbPath)
defer db.Close()
defer os.RemoveAll("testdata")
handler := fileHandler(db)
t.Run("PUT request", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPut, "/bucket/testbucket/testfile.txt", bytes.NewBufferString("Hello, World!"))
req.SetBasicAuth("", "testpassword")
req.Header.Set("Content-Type", "text/plain")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusCreated {
t.Errorf("expected status %v, got %v", http.StatusCreated, status)
}
t.Run("GET request", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/bucket/testbucket/testfile.txt", nil)
req.SetBasicAuth("", "testpassword")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("expected status %v, got %v", http.StatusOK, status)
}
expectedBody := "Hello, World!"
body, _ := io.ReadAll(rr.Body)
if string(body) != expectedBody {
t.Errorf("expected body %v, got %v", expectedBody, string(body))
}
})
t.Run("HEAD request", func(t *testing.T) {
req := httptest.NewRequest(http.MethodHead, "/bucket/testbucket/testfile.txt", nil)
req.SetBasicAuth("", "testpassword")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("expected status %v, got %v", http.StatusOK, status)
}
})
t.Run("Directory traversal attack", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/bucket/testbucket/../server.go", nil)
req.SetBasicAuth("", "testpassword")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("expected status %v, got %v", http.StatusBadRequest, status)
}
})
t.Run("DELETE request", func(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, "/bucket/testbucket/testfile.txt", nil)
req.SetBasicAuth("", "testpassword")
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusNoContent {
t.Errorf("expected status %v, got %v", http.StatusNoContent, status)
}
})
})
}