-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
69 lines (56 loc) · 1.21 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
package streisand
import (
"bytes"
"io/ioutil"
"net/http/httptest"
"strings"
"testing"
)
func TestServer(t *testing.T) {
s, err := NewServer(ServerConfig{
DataDir: t.TempDir(),
CacheDir: t.TempDir(),
WithFsync: true,
GetPeers: nil,
})
if err != nil {
t.Fatal(err)
}
// upload file
w := httptest.NewRecorder()
s.ServeHTTP(w, httptest.NewRequest("POST", "/upload",
strings.NewReader("test")))
resp := w.Result()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
hash, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(hash, []byte("9f86d081884c7d659a2feaa0c55"+
"ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")) != 0 {
t.Fatal("upload returned invalid hash")
}
// download it
w = httptest.NewRecorder()
s.ServeHTTP(w, httptest.NewRequest("GET",
"/blob/"+string(hash),
strings.NewReader("test")))
resp = w.Result()
if resp.StatusCode != 200 {
t.Fatal(resp.Status)
}
blob, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(blob, []byte("test")) != 0 {
t.Fatal("server didn't return uploaded file")
}
err = s.Close()
if err != nil {
t.Fatal(err)
}
}
// TODO: add benchmarks for simultaneous up-/downloading