forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttphandler_test.go
132 lines (113 loc) · 3.48 KB
/
httphandler_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package desync
import (
"io/ioutil"
"net/http/httptest"
"net/url"
"os"
"testing"
)
func TestHTTPHandlerReadWrite(t *testing.T) {
// Setup a temporary store used as upstream store that the HTTP server
// pulls from
store, err := ioutil.TempDir("", "store")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(store)
upstream, err := NewLocalStore(store, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Start a read-write capable server and a read-only server
rw := httptest.NewServer(NewHTTPHandler(upstream, true, false, false))
defer rw.Close()
ro := httptest.NewServer(NewHTTPHandler(upstream, false, false, false))
defer ro.Close()
// Initialize HTTP chunks stores, one RW and the other RO
rwStoreURL, _ := url.Parse(rw.URL)
rwStore, err := NewRemoteHTTPStore(rwStoreURL, StoreOptions{})
if err != nil {
t.Fatal(err)
}
roStoreURL, _ := url.Parse(ro.URL)
roStore, err := NewRemoteHTTPStore(roStoreURL, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Make up some data and store it in the RW store
dataIn := []byte("some data")
chunkIn := NewChunkFromUncompressed(dataIn)
id := chunkIn.ID()
if err := rwStore.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Check it's in the store
if !rwStore.HasChunk(id) {
t.Fatal("chunk not found in store")
}
// Let's try to send some data to the RO store, that should fail
if err := roStore.StoreChunk(chunkIn); err == nil {
t.Fatal("expected error writing to read-only chunkstore")
}
}
func TestHTTPHandlerCompression(t *testing.T) {
// Setup a temporary store used as upstream store that the HTTP server
// pulls from
store, err := ioutil.TempDir("", "store")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(store)
upstream, err := NewLocalStore(store, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Start a server that uses compression, and one that serves uncompressed chunks
co := httptest.NewServer(NewHTTPHandler(upstream, true, false, false))
defer co.Close()
un := httptest.NewServer(NewHTTPHandler(upstream, true, false, true))
defer un.Close()
// Initialize HTTP chunks stores, one RW and the other RO. Also make one that's
// trying to get compressed data from a HTTP store that serves only uncompressed.
coStoreURL, _ := url.Parse(co.URL)
coStore, err := NewRemoteHTTPStore(coStoreURL, StoreOptions{})
if err != nil {
t.Fatal(err)
}
unStoreURL, _ := url.Parse(un.URL)
unStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{Uncompressed: true})
if err != nil {
t.Fatal(err)
}
invalidStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{})
if err != nil {
t.Fatal(err)
}
// Make up some data and store it in the RW store
dataIn := []byte("some data")
chunkIn := NewChunkFromUncompressed(dataIn)
id := chunkIn.ID()
// Try to get compressed chunks from a store that only serves uncompressed chunks
if _, err := invalidStore.GetChunk(id); err == nil {
t.Fatal("expected failure trying to get compressed chunks from uncompressed http store")
}
if err := coStore.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Check it's in the store when looking for compressed chunks
if !coStore.HasChunk(id) {
t.Fatal("chunk not found in store")
}
// It's also visible when looking for uncompressed data
if !unStore.HasChunk(id) {
t.Fatal("chunk not found in store")
}
// Send it uncompressed
if err := unStore.StoreChunk(chunkIn); err != nil {
t.Fatal(err)
}
// Try to get the uncompressed chunk
if _, err := unStore.GetChunk(id); err != nil {
t.Fatal(err)
}
}