-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlshare_test.go
106 lines (84 loc) · 2.78 KB
/
lshare_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
99
100
101
102
103
104
105
106
package lshare
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
// func TestGetLocalIP(t *testing.T) {
// want := "" // local ip would go here
// got, err := GetLocalIP()
// if err != nil {
// t.Fatal(err)
// }
// if got != want {
// t.Errorf("got %v, and %v", got, want)
// }
// }
func TestFileServer(t *testing.T) {
var (
testFile = fileToServe{"/helloworld", "helloworld.txt", []byte("Hello, World!")}
)
t.Run("all requests should be 404'd if no endpoints have been registered", func(t *testing.T) {
server := NewFileServer()
request := httptest.NewRequest(http.MethodGet, testFile.url, nil)
resp, _ := RecordResponse(t, request, server)
AssertStatus404(t, resp)
})
t.Run("registering an endpoint with content serves direct download", func(t *testing.T) {
server := NewFileServer()
server.RegisterEndpoint(testFile.url, testFile.filename, testFile.content)
request := httptest.NewRequest(http.MethodGet, testFile.url, nil)
resp, body := RecordResponse(t, request, server)
AssertFileServed(t, testFile, resp, body)
})
t.Run("deleting an endpoint returns 404 on further requests", func(t *testing.T) {
server := NewFileServer()
server.RegisterEndpoint(testFile.url, testFile.filename, testFile.content)
request := httptest.NewRequest(http.MethodGet, testFile.url, nil)
resp, body := RecordResponse(t, request, server)
AssertFileServed(t, testFile, resp, body)
server.DeleteEndpoint(testFile.url)
resp, _ = RecordResponse(t, request, server)
AssertStatus404(t, resp)
})
}
type fileToServe struct {
url, filename string
content []byte
}
func RecordResponse(t *testing.T, r *http.Request, s *FileServer) (*http.Response, string) {
responseRecorder := httptest.NewRecorder()
s.FileDownloadHandler(responseRecorder, r)
resp := responseRecorder.Result()
return resp, responseRecorder.Body.String()
}
func AssertFileServed(t *testing.T, file fileToServe, resp *http.Response, body string) {
t.Helper()
AssertStatusOK(t, resp)
AssertFilenameEqual(t, resp, file.filename)
want := string(file.content)
if body != want {
t.Errorf("expected body %s, got %s", want, body)
}
}
func AssertStatusOK(t *testing.T, resp *http.Response) {
t.Helper()
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status OK, got %v", resp.Status)
}
}
func AssertStatus404(t *testing.T, resp *http.Response) {
t.Helper()
if resp.StatusCode != http.StatusNotFound {
t.Errorf("expected not found, got %v", resp.Status)
}
}
func AssertFilenameEqual(t *testing.T, got *http.Response, want string) {
t.Helper()
contentDisposition := got.Header.Get("Content-Disposition")
expectedFilename := fmt.Sprintf("attachment; filename=%s", want)
if contentDisposition != expectedFilename {
t.Errorf("expected filename %s, got %s", want, contentDisposition)
}
}