2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-04-17 14:39:14 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ranger
|
|
|
|
|
|
|
|
import (
|
2018-06-19 16:59:09 +01:00
|
|
|
"context"
|
2018-04-17 14:39:14 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
var (
|
|
|
|
ctx = context.Background() // test context
|
|
|
|
)
|
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
func TestHTTPRanger(t *testing.T) {
|
|
|
|
var content string
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeContent(w, r, "test", time.Now(), strings.NewReader(content))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
data string
|
|
|
|
size, offset, length int64
|
|
|
|
substr string
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{"", 0, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 0, 7, "abcdef", "ranger error: range beyond end"},
|
|
|
|
{"abcdef", 6, -1, 7, "abcde", "ranger error: negative offset"},
|
|
|
|
{"abcdef", 6, 0, -1, "abcde", "ranger error: negative length"},
|
|
|
|
} {
|
2018-12-11 09:38:56 +00:00
|
|
|
tag := fmt.Sprintf("#%d. %+v", i, tt)
|
|
|
|
|
2018-04-17 14:39:14 +01:00
|
|
|
content = tt.data
|
2019-06-04 12:36:27 +01:00
|
|
|
rr, err := HTTPRanger(ctx, ts.URL)
|
2018-12-11 09:38:56 +00:00
|
|
|
if assert.NoError(t, err, tag) {
|
|
|
|
assert.Equal(t, tt.size, rr.Size(), tag)
|
2018-05-14 21:34:08 +01:00
|
|
|
}
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := rr.Range(context.Background(), tt.offset, tt.length)
|
2018-05-14 21:34:08 +01:00
|
|
|
if tt.errString != "" {
|
2018-12-11 09:38:56 +00:00
|
|
|
assert.EqualError(t, err, tt.errString, tag)
|
2018-05-14 21:34:08 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-12-11 09:38:56 +00:00
|
|
|
assert.NoError(t, err, tag)
|
2018-06-18 17:46:49 +01:00
|
|
|
data, err := ioutil.ReadAll(r)
|
2018-12-11 09:38:56 +00:00
|
|
|
if assert.NoError(t, err, tag) {
|
|
|
|
assert.Equal(t, []byte(tt.substr), data, tag)
|
2018-05-14 21:34:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 18:31:27 +01:00
|
|
|
|
|
|
|
func TestHTTPRangerURLError(t *testing.T) {
|
2019-06-04 12:36:27 +01:00
|
|
|
rr, err := HTTPRanger(ctx, "")
|
2018-07-27 18:31:27 +01:00
|
|
|
assert.Nil(t, rr)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPRangeStatusCodeOk(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
http.ServeContent(w, r, "test", time.Now(), strings.NewReader(""))
|
|
|
|
}))
|
2019-06-04 12:36:27 +01:00
|
|
|
rr, err := HTTPRanger(ctx, ts.URL)
|
2018-07-27 18:31:27 +01:00
|
|
|
assert.Nil(t, rr)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:34:08 +01:00
|
|
|
func TestHTTPRangerSize(t *testing.T) {
|
|
|
|
var content string
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeContent(w, r, "test", time.Now(), strings.NewReader(content))
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
data string
|
|
|
|
size, offset, length int64
|
|
|
|
substr string
|
|
|
|
errString string
|
|
|
|
}{
|
|
|
|
{"", 0, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 0, "", ""},
|
|
|
|
{"abcdef", 6, 3, 0, "", ""},
|
|
|
|
{"abcdef", 6, 0, 6, "abcdef", ""},
|
|
|
|
{"abcdef", 6, 0, 5, "abcde", ""},
|
|
|
|
{"abcdef", 6, 0, 4, "abcd", ""},
|
|
|
|
{"abcdef", 6, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 2, 4, "cdef", ""},
|
|
|
|
{"abcdefg", 7, 1, 4, "bcde", ""},
|
|
|
|
{"abcdef", 6, 0, 7, "abcdef", "ranger error: range beyond end"},
|
|
|
|
{"abcdef", 6, -1, 7, "abcde", "ranger error: negative offset"},
|
|
|
|
{"abcdef", 6, 0, -1, "abcde", "ranger error: negative length"},
|
|
|
|
} {
|
2018-12-11 09:38:56 +00:00
|
|
|
tag := fmt.Sprintf("#%d. %+v", i, tt)
|
|
|
|
|
2018-05-14 21:34:08 +01:00
|
|
|
content = tt.data
|
2018-06-18 17:46:49 +01:00
|
|
|
rr := HTTPRangerSize(ts.URL, tt.size)
|
2018-12-11 09:38:56 +00:00
|
|
|
assert.Equal(t, tt.size, rr.Size(), tag)
|
2018-06-19 16:59:09 +01:00
|
|
|
r, err := rr.Range(context.Background(), tt.offset, tt.length)
|
2018-04-17 14:39:14 +01:00
|
|
|
if tt.errString != "" {
|
2018-12-11 09:38:56 +00:00
|
|
|
assert.EqualError(t, err, tt.errString, tag)
|
2018-04-17 14:39:14 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-12-11 09:38:56 +00:00
|
|
|
assert.NoError(t, err, tag)
|
2018-06-18 17:46:49 +01:00
|
|
|
data, err := ioutil.ReadAll(r)
|
2018-12-11 09:38:56 +00:00
|
|
|
if assert.NoError(t, err, tag) {
|
|
|
|
assert.Equal(t, []byte(tt.substr), data, tag)
|
2018-04-17 14:39:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|