Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions httpreaderat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (ra *readerAtFixture) TestRangeSupportInitial() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rnge := r.Header.Get("Range")
ra.Equal(rnge, "bytes=0-0")
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
w.Header().Set("Content-Range", fmt.Sprintf(" bytes 0-0/%d", 1))
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte{17})
Expand All @@ -67,6 +69,9 @@ func (ra *readerAtFixture) TestRangeSupportInitial() {
reader, err := ra.reader()
ra.Nil(err)
ra.NotNil(reader)
ra.Equal("application/zip", reader.ContentType())
ra.Equal("Wed, 21 Oct 2015 07:28:00 GMT", reader.LastModified())
ra.Equal(int64(1), reader.Size())
}

func (ra *readerAtFixture) TestRangeSupportInitialEmptyResponse() {
Expand Down Expand Up @@ -150,3 +155,168 @@ func (ra *readerAtFixture) TestParallelFiles() {
wg.Wait()

}

func (ra *readerAtFixture) TestReadAtEOFClamp() {
content := []byte("hello")
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Range") {
case "bytes=0-0":
w.Header().Set("Content-Range", "bytes 0-0/5")
w.WriteHeader(http.StatusPartialContent)
w.Write(content[:1])
case "bytes=3-4":
w.Header().Set("Content-Range", "bytes 3-4/5")
w.WriteHeader(http.StatusPartialContent)
w.Write(content[3:])
default:
ra.FailNow("unexpected range", r.Header.Get("Range"))
}
}))

reader, err := ra.reader()
ra.NoError(err)

buf := make([]byte, 4)
n, err := reader.ReadAt(buf, 3)
ra.Equal(2, n)
ra.ErrorIs(err, io.EOF)
ra.Equal("lo", string(buf[:n]))
}

func (ra *readerAtFixture) TestReadAtPastEOF() {
content := []byte("hello")
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ra.Equal("bytes=0-0", r.Header.Get("Range"))
w.Header().Set("Content-Range", "bytes 0-0/5")
w.WriteHeader(http.StatusPartialContent)
w.Write(content[:1])
}))

reader, err := ra.reader()
ra.NoError(err)

n, err := reader.ReadAt(make([]byte, 1), int64(len(content)))
ra.Equal(0, n)
ra.ErrorIs(err, io.EOF)
}

func (ra *readerAtFixture) TestValidationFailure() {
etag := `"v1"`
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Range") {
case "bytes=0-0":
w.Header().Set("Content-Range", "bytes 0-0/5")
w.Header().Set("ETag", etag)
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("h"))
case "bytes=1-1":
w.Header().Set("Content-Range", "bytes 1-1/5")
w.Header().Set("ETag", `"v2"`)
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("e"))
default:
ra.FailNow("unexpected range", r.Header.Get("Range"))
}
}))

reader, err := ra.reader()
ra.NoError(err)

n, err := reader.ReadAt(make([]byte, 1), 1)
ra.Equal(0, n)
ra.ErrorIs(err, httpreaderat.ErrValidationFailed)
}

func (ra *readerAtFixture) TestMissingContentRange() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("h"))
}))

reader, err := ra.reader()
ra.EqualError(err, "no content-range header in partial response")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestMalformedContentRange() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Range", "banana")
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("h"))
}))

reader, err := ra.reader()
ra.EqualError(err, "http request: unsupported unit")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestDifferentRangeThanRequested() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Range", "bytes 1-1/5")
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("h"))
}))

reader, err := ra.reader()
ra.EqualError(err, "received different range than requested (req=0-0, resp=1-1)")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestHTTPRequestFailure() {
req, err := http.NewRequest("GET", "http://127.0.0.1:1/file.zip", nil)
ra.NoError(err)

reader, err := httpreaderat.New(&http.Client{}, req, nil)
ra.Nil(reader)
ra.Error(err)
}

func (ra *readerAtFixture) TestServerStopsSupportingRangeRequests() {
requests := 0
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests++
if requests == 1 {
w.Header().Set("Content-Range", "bytes 0-0/5")
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte("h"))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello"))
}))

req, err := http.NewRequest("GET", ra.server.URL+"/file.zip", nil)
ra.NoError(err)

reader, err := httpreaderat.New(nil, req, httpreaderat.NewStoreMemory())
ra.NoError(err)

n, err := reader.ReadAt(make([]byte, 1), 1)
ra.Equal(0, n)
ra.EqualError(err, "server suddenly stopped supporting range requests")
}

func (ra *readerAtFixture) TestFallbackStoreWhenRangeNotSupported() {
content := []byte("hello")
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT")
w.WriteHeader(http.StatusOK)
w.Write(content)
}))

req, err := http.NewRequest("GET", ra.server.URL+"/file.zip", nil)
ra.NoError(err)

reader, err := httpreaderat.New(nil, req, httpreaderat.NewStoreMemory())
ra.NoError(err)
ra.Equal("text/plain", reader.ContentType())
ra.Equal("Wed, 21 Oct 2015 07:28:00 GMT", reader.LastModified())
ra.Equal(int64(len(content)), reader.Size())

buf := make([]byte, len(content))
n, err := reader.ReadAt(buf, 0)
ra.NoError(err)
ra.Equal(len(content), n)
ra.Equal(content, buf)
}
53 changes: 53 additions & 0 deletions meta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package httpreaderat

import (
"net/http"
"testing"
)

func TestValidateIgnoresContentTypeChanges(t *testing.T) {
ra := &HTTPReaderAt{
meta: meta{
size: 5,
contentType: "text/plain",
},
}

resp := &http.Response{
StatusCode: http.StatusPartialContent,
Header: http.Header{
"Content-Range": []string{"bytes 0-0/5"},
"Content-Type": []string{"application/octet-stream"},
},
}

if err := ra.validate(resp); err != nil {
t.Fatalf("validate() error = %v, want nil", err)
}
}

func TestGetMetaFromStatusOK(t *testing.T) {
resp := &http.Response{
StatusCode: http.StatusOK,
ContentLength: 42,
Header: http.Header{
"Content-Type": []string{"application/zip"},
"Last-Modified": []string{"Wed, 21 Oct 2015 07:28:00 GMT"},
"Etag": []string{`"v1"`},
},
}

got := getMeta(resp)
if got.size != 42 {
t.Fatalf("size = %d, want 42", got.size)
}
if got.contentType != "application/zip" {
t.Fatalf("contentType = %q, want %q", got.contentType, "application/zip")
}
if got.lastModified != "Wed, 21 Oct 2015 07:28:00 GMT" {
t.Fatalf("lastModified = %q", got.lastModified)
}
if got.etag != `"v1"` {
t.Fatalf("etag = %q, want %q", got.etag, `"v1"`)
}
}
Loading
Loading