forked from htcondor/osdf-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_http_test.go
263 lines (225 loc) · 7.78 KB
/
handle_http_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package stashcp
import (
"bytes"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestIsPort calls main.hasPort with a hostname, checking
// for a valid return value.
func TestIsPort(t *testing.T) {
if HasPort("blah.not.port:") {
t.Fatal("Failed to parse port when : at end")
}
if !HasPort("host:1") {
t.Fatal("Failed to parse with port = 1")
}
if HasPort("https://example.com") {
t.Fatal("Failed when scheme is specified")
}
}
// TestNewTransferDetails checks the creation of transfer details
func TestNewTransferDetails(t *testing.T) {
// Case 1: cache with http
testCache := Cache{
AuthEndpoint: "cache.edu:8443",
Endpoint: "cache.edu:8000",
Resource: "Cache",
}
transfers := NewTransferDetails(testCache, false)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "cache.edu:8000", transfers[0].Url.Host)
assert.Equal(t, "http", transfers[0].Url.Scheme)
assert.Equal(t, true, transfers[0].Proxy)
assert.Equal(t, "cache.edu:8000", transfers[1].Url.Host)
assert.Equal(t, "http", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
// Case 2: cache with https
transfers = NewTransferDetails(testCache, true)
assert.Equal(t, 1, len(transfers))
assert.Equal(t, "cache.edu:8443", transfers[0].Url.Host)
assert.Equal(t, "https", transfers[0].Url.Scheme)
assert.Equal(t, false, transfers[0].Proxy)
testCache.Endpoint = "cache.edu"
// Case 3: cache without port with http
transfers = NewTransferDetails(testCache, false)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "cache.edu:8000", transfers[0].Url.Host)
assert.Equal(t, "http", transfers[0].Url.Scheme)
assert.Equal(t, true, transfers[0].Proxy)
assert.Equal(t, "cache.edu:8000", transfers[1].Url.Host)
assert.Equal(t, "http", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
// Case 4. cache without port with https
testCache.AuthEndpoint = "cache.edu"
transfers = NewTransferDetails(testCache, true)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "cache.edu:8444", transfers[0].Url.Host)
assert.Equal(t, "https", transfers[0].Url.Scheme)
assert.Equal(t, false, transfers[0].Proxy)
assert.Equal(t, "cache.edu:8443", transfers[1].Url.Host)
assert.Equal(t, "https", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
}
func TestNewTransferDetailsEnv(t *testing.T) {
testCache := Cache{
AuthEndpoint: "cache.edu:8443",
Endpoint: "cache.edu:8000",
Resource: "Cache",
}
os.Setenv("OSG_DISABLE_PROXY_FALLBACK", "")
transfers := NewTransferDetails(testCache, false)
assert.Equal(t, 1, len(transfers))
assert.Equal(t, true, transfers[0].Proxy)
transfers = NewTransferDetails(testCache, true)
assert.Equal(t, 1, len(transfers))
assert.Equal(t, "https", transfers[0].Url.Scheme)
assert.Equal(t, false, transfers[0].Proxy)
os.Unsetenv("OSG_DISABLE_PROXY_FALLBACK")
}
func TestSlowTransfers(t *testing.T) {
channel := make(chan bool)
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Don't send any response
<-channel
}))
defer svr.CloseClientConnections()
defer svr.Close()
testCache := Cache{
AuthEndpoint: svr.URL,
Endpoint: svr.URL,
Resource: "Cache",
}
transfers := NewTransferDetails(testCache, false)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, svr.URL, transfers[0].Url.String())
finishedChannel := make(chan bool)
var err error
// Do a quick timeout
go func() {
_, err = DownloadHTTP(transfers[0], filepath.Join(t.TempDir(), "test.txt"), "")
finishedChannel <- true
}()
select {
case <-finishedChannel:
if err == nil {
t.Fatal("Download should have failed")
}
case <-time.After(time.Second * 12):
t.Fatal("Download should have failed")
}
// Close the channel to allow the download to complete
channel <- true
// Make sure the errors are correct
assert.NotNil(t, err)
assert.IsType(t, &ConnectionSetupError{}, err, err.Error())
}
// Test connection error
func TestConnectionError(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("dialClosedPort: Listen failed: %v", err)
}
addr := l.Addr().String()
l.Close()
_, err = DownloadHTTP(TransferDetails{Url: url.URL{Host: addr, Scheme: "http"}, Proxy: false}, filepath.Join(t.TempDir(), "test.txt"), "")
assert.IsType(t, &ConnectionSetupError{}, err)
}
func TestUploadZeroLengthFile(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//t.Logf("%s", dump)
assert.Equal(t, "PUT", r.Method, "Not PUT Method")
assert.Equal(t, int64(0), r.ContentLength, "ContentLength should be 0")
}))
defer ts.Close()
reader := bytes.NewReader([]byte{})
request, err := http.NewRequest("PUT", ts.URL, reader)
if err != nil {
assert.NoError(t, err)
}
request.Header.Set("Authorization", "Bearer test")
errorChan := make(chan error, 1)
responseChan := make(chan *http.Response)
go doPut(request, responseChan, errorChan)
select {
case err := <-errorChan:
assert.NoError(t, err)
case response := <-responseChan:
assert.Equal(t, http.StatusOK, response.StatusCode)
case <-time.After(time.Second * 2):
assert.Fail(t, "Timeout while waiting for response")
}
}
func TestFailedUpload(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//t.Logf("%s", dump)
assert.Equal(t, "PUT", r.Method, "Not PUT Method")
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("Error"))
assert.NoError(t, err)
}))
defer ts.Close()
reader := strings.NewReader("test")
request, err := http.NewRequest("PUT", ts.URL, reader)
if err != nil {
assert.NoError(t, err)
}
request.Header.Set("Authorization", "Bearer test")
errorChan := make(chan error, 1)
responseChan := make(chan *http.Response)
go doPut(request, responseChan, errorChan)
select {
case err := <-errorChan:
assert.Error(t, err)
case response := <-responseChan:
assert.Equal(t, http.StatusInternalServerError, response.StatusCode)
case <-time.After(time.Second * 2):
assert.Fail(t, "Timeout while waiting for response")
}
}
func TestFullUpload(t *testing.T) {
testFileContent := "test file content"
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//t.Logf("%s", dump)
assert.Equal(t, "PUT", r.Method, "Not PUT Method")
_, err := w.Write([]byte(":)"))
assert.NoError(t, err)
}))
defer ts.Close()
// Create the temporary file to upload
tempFile, err := os.CreateTemp(t.TempDir(), "test")
assert.NoError(t, err, "Error creating temp file")
defer os.Remove(tempFile.Name())
_, err = tempFile.WriteString(testFileContent)
assert.NoError(t, err, "Error writing to temp file")
tempFile.Close()
// Create the namespace (only the write back host is read)
testURL, err := url.Parse(ts.URL)
assert.NoError(t, err, "Error parsing test URL")
testNamespace := Namespace{
WriteBackHost: "https://" + testURL.Host,
}
// Upload the file
uploadURL, err := url.Parse("stash:///test/stuff/blah.txt")
assert.NoError(t, err, "Error parsing upload URL")
// Set the upload client to trust the server
UploadClient = ts.Client()
uploaded, err := UploadFile(tempFile.Name(), uploadURL, "Bearer test", testNamespace)
assert.NoError(t, err, "Error uploading file")
assert.Equal(t, int64(len(testFileContent)), uploaded, "Uploaded file size does not match")
// Upload an osdf file
uploadURL, err = url.Parse("osdf:///test/stuff/blah.txt")
assert.NoError(t, err, "Error parsing upload URL")
// Set the upload client to trust the server
UploadClient = ts.Client()
uploaded, err = UploadFile(tempFile.Name(), uploadURL, "Bearer test", testNamespace)
assert.NoError(t, err, "Error uploading file")
assert.Equal(t, int64(len(testFileContent)), uploaded, "Uploaded file size does not match")
}