-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathhttphandler_test.go
105 lines (81 loc) · 3.17 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
package desync
import (
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func TestHTTPHandlerReadWrite(t *testing.T) {
store := t.TempDir()
upstream, err := NewLocalStore(store, StoreOptions{})
require.NoError(t, err)
// Start a read-write capable server and a read-only server
rw := httptest.NewServer(NewHTTPHandler(upstream, true, false, []converter{Compressor{}}, ""))
defer rw.Close()
ro := httptest.NewServer(NewHTTPHandler(upstream, false, false, []converter{Compressor{}}, ""))
defer ro.Close()
// Initialize HTTP chunks stores, one RW and the other RO
rwStoreURL, _ := url.Parse(rw.URL)
rwStore, err := NewRemoteHTTPStore(rwStoreURL, StoreOptions{})
require.NoError(t, err)
roStoreURL, _ := url.Parse(ro.URL)
roStore, err := NewRemoteHTTPStore(roStoreURL, StoreOptions{})
require.NoError(t, err)
// Make up some data and store it in the RW store
dataIn := []byte("some data")
chunkIn := NewChunk(dataIn)
id := chunkIn.ID()
// Write a chunk
err = rwStore.StoreChunk(chunkIn)
require.NoError(t, err)
// Check it's in the store
hasChunk, err := rwStore.HasChunk(id)
require.NoError(t, err)
require.True(t, hasChunk)
// Let's try to send some data to the RO store, that should fail
err = roStore.StoreChunk(chunkIn)
require.Error(t, err, "expected error writing to read-only chunkstore")
}
func TestHTTPHandlerCompression(t *testing.T) {
store := t.TempDir()
upstream, err := NewLocalStore(store, StoreOptions{})
require.NoError(t, err)
// Start a server that uses compression, and one that serves uncompressed chunks
co := httptest.NewServer(NewHTTPHandler(upstream, true, false, []converter{Compressor{}}, ""))
defer co.Close()
un := httptest.NewServer(NewHTTPHandler(upstream, true, false, nil, ""))
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{})
require.NoError(t, err)
unStoreURL, _ := url.Parse(un.URL)
unStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{Uncompressed: true})
require.NoError(t, err)
invalidStore, err := NewRemoteHTTPStore(unStoreURL, StoreOptions{})
require.NoError(t, err)
// Make up some data and store it in the RW store
dataIn := []byte("some data")
chunkIn := NewChunk(dataIn)
id := chunkIn.ID()
// Try to get compressed chunks from a store that only serves uncompressed chunks
_, err = invalidStore.GetChunk(id)
require.Error(t, err, "expected failure trying to get compressed chunks from uncompressed http store")
err = coStore.StoreChunk(chunkIn)
require.NoError(t, err)
// Check it's in the store when looking for compressed chunks
coExists, err := coStore.HasChunk(id)
require.NoError(t, err)
require.True(t, coExists)
// It's also visible when looking for uncompressed data
unExists, err := unStore.HasChunk(id)
require.NoError(t, err)
require.True(t, unExists)
// Send it uncompressed
err = unStore.StoreChunk(chunkIn)
require.NoError(t, err)
// Try to get the uncompressed chunk
_, err = unStore.GetChunk(id)
require.NoError(t, err)
}